text
stringlengths 0
601k
|
---|
module Hyps : sig type t = coinductive_hyps val empty : t val add : type_expr -> mode -> t -> t val guard : t -> t val poison : t -> t val safe : type_expr -> mode -> t -> bool val unsafe : type_expr -> mode -> t -> bool type t = coinductive_hyps let empty = { safe = TypeMap . empty ; unsafe = TypeMap . empty ; poison = TypeMap . empty ; } let of_opt = function | Some ms -> ms | None -> ModeSet . empty let merge map1 map2 = TypeMap . merge ( fun _k ms1 ms2 -> Some ( ModeSet . union ( of_opt ms1 ) ( of_opt ms2 ) ) ) map1 map2 let guard { safe ; unsafe ; poison ; } = { safe = merge safe unsafe ; unsafe = TypeMap . empty ; poison ; } let poison { safe ; unsafe ; poison ; } = { safe ; unsafe = TypeMap . empty ; poison = merge poison unsafe ; } let add ty m hyps = let m_map = TypeMap . singleton ty ( ModeSet . singleton m ) in { hyps with unsafe = merge m_map hyps . unsafe ; } let find ty map = try TypeMap . find ty map with Not_found -> ModeSet . empty let safe ty m hyps = match ModeSet . max_elt_opt ( find ty hyps . safe ) with | None -> false | Some best_safe -> rank best_safe >= rank m let unsafe ty m { safe = _ ; unsafe ; poison } = let in_map s = ModeSet . mem m ( find ty s ) in List . exists in_map [ unsafe ; poison ] end |
let worst_case ty = let add ctx tvar = TVarMap . add tvar Deepsep ctx in List . fold_left add TVarMap . empty ( free_variables ty ) |
let check_type : Env . t -> type_expr -> mode -> context = fun env ty m -> let rec check_type hyps ty m = let ty = Ctype . repr ty in if Hyps . safe ty m hyps then empty else if Hyps . unsafe ty m hyps then worst_case ty else let hyps = Hyps . add ty m hyps in match ( ty . desc , m ) with | ( Tlink _ , _ ) -> assert false | ( Tsubst ( _ ) , _ ) -> assert false | ( _ , Ind ) -> empty | ( Tvar ( alpha ) , m ) -> TVarMap . singleton { text = alpha ; id = ty . Types . id } m | ( Tarrow _ , Sep ) | ( Ttuple _ , Sep ) | ( Tvariant ( _ ) , Sep ) | ( Tobject ( _ , _ ) , Sep ) | ( ( Tnil | Tfield _ ) , Sep ) | ( Tpackage ( _ , _ , _ ) , Sep ) -> empty | ( Tarrow _ , Deepsep ) | ( Ttuple _ , Deepsep ) | ( Tvariant ( _ ) , Deepsep ) | ( Tobject ( _ , _ ) , Deepsep ) | ( ( Tnil | Tfield _ ) , Deepsep ) | ( Tpackage ( _ , _ , _ ) , Deepsep ) -> let tys = immediate_subtypes ty in let on_subtype context ty = context ++ check_type ( Hyps . guard hyps ) ty Deepsep in List . fold_left on_subtype empty tys | ( Tpoly ( pty , _ ) , m ) -> check_type hyps pty m | ( Tunivar ( _ ) , _ ) -> empty | ( Tconstr ( path , tys , _ ) , m ) -> let msig = ( Env . find_type path env ) . type_separability in let on_param context ( ty , m_param ) = let hyps = match m_param with | Ind -> Hyps . guard hyps | Sep -> hyps | Deepsep -> Hyps . poison hyps in context ++ check_type hyps ty ( compose m m_param ) in List . fold_left on_param empty ( List . combine tys msig ) in check_type Hyps . empty ty m |
let best_msig decl = List . map ( fun _ -> Ind ) decl . type_params |
let worst_msig decl = List . map ( fun _ -> Deepsep ) decl . type_params |
let msig_of_external_type decl = match decl . type_immediate with | Always | Always_on_64bits -> best_msig decl | Unknown -> worst_msig decl |
let msig_of_context : decl_loc : Location . t -> parameters : type_expr list -> context -> Sep . signature = fun ~ decl_loc ~ parameters context -> let handle_equation ( acc , context ) param_instance = let param_instance = Ctype . repr param_instance in let get context var = try TVarMap . find var context with Not_found -> Ind in let set_ind context var = TVarMap . add var Ind context in let is_ind context var = match get context var with | Ind -> true | Sep | Deepsep -> false in match param_instance . desc with | Tvar text -> let var = { text ; id = param_instance . Types . id } in ( get context var ) :: acc , ( set_ind context var ) | _ -> let instance_exis = free_variables param_instance in if List . for_all ( is_ind context ) instance_exis then Ind :: acc , context else Deepsep :: acc , List . fold_left set_ind context instance_exis in let mode_signature , context = let ( mode_signature_rev , ctx ) = List . fold_left handle_equation ( [ ] , context ) parameters in ( List . rev mode_signature_rev , ctx ) in let check_existential evar mode = if rank mode > rank Ind then raise ( Error ( decl_loc , Non_separable_evar evar . text ) ) in TVarMap . iter check_existential context ; mode_signature |
let check_def : Env . t -> type_definition -> Sep . signature = fun env def -> let boxed = not def . type_unboxed . unboxed in match structure def with | Abstract -> assert boxed ; msig_of_external_type def | Synonym type_expr -> check_type env type_expr Sep |> msig_of_context ~ decl_loc : def . type_loc ~ parameters : def . type_params | Open | Algebraic ( Zero | Several | One ( Zero | Several ) ) -> assert boxed ; best_msig def | Algebraic ( One ( One constructor ) ) -> if boxed then best_msig def else check_type env constructor . argument_type Sep |> msig_of_context ~ decl_loc : def . type_loc ~ parameters : constructor . result_type_parameter_instances |
let compute_decl env decl = if Config . flat_float_array then check_def env decl else try check_def env decl with | Error _ -> best_msig decl |
let property : ( prop , unit ) Typedecl_properties . property = let open Typedecl_properties in let eq ts1 ts2 = List . length ts1 = List . length ts2 && List . for_all2 Sep . eq ts1 ts2 in let merge ~ prop : _ ~ new_prop = new_prop in let default decl = best_msig decl in let compute env decl ( ) = compute_decl env decl in let update_decl decl type_separability = { decl with type_separability } in let check _env _id _decl ( ) = ( ) in { eq ; merge ; default ; compute ; update_decl ; check ; } |
let update_decls env decls = Typedecl_properties . compute_property_noreq property env decls |
type t = | Unavailable | This of type_expr | Only_on_64_bits of type_expr |
let rec get_unboxed_type_representation env ty fuel = if fuel < 0 then Unavailable else let ty = Ctype . repr ( Ctype . expand_head_opt env ty ) in match ty . desc with | Tconstr ( p , args , _ ) -> begin match Env . find_type p env with | exception Not_found -> This ty | { type_immediate = Always ; _ } -> This Predef . type_int | { type_immediate = Always_on_64bits ; _ } -> Only_on_64_bits Predef . type_int | { type_unboxed = { unboxed = false } } -> This ty | { type_params ; type_kind = Type_record ( [ { ld_type = ty2 ; _ } ] , _ ) | Type_variant [ { cd_args = Cstr_tuple [ ty2 ] ; _ } ] | Type_variant [ { cd_args = Cstr_record [ { ld_type = ty2 ; _ } ] ; _ } ] } -> let ty2 = match ty2 . desc with Tpoly ( t , _ ) -> t | _ -> ty2 in get_unboxed_type_representation env ( Ctype . apply env type_params ty2 args ) ( fuel - 1 ) | { type_kind = Type_abstract } -> Unavailable | _ -> assert false end | _ -> This ty |
let get_unboxed_type_representation env ty = get_unboxed_type_representation env ty 100 ; ; |
type surface_variance = bool * bool * bool |
let get_variance ty visited = try TypeMap . find ty ! visited with Not_found -> Variance . null |
let compute_variance env visited vari ty = let rec compute_variance_rec vari ty = let ty = Ctype . repr ty in let vari ' = get_variance ty visited in if Variance . subset vari vari ' then ( ) else let vari = Variance . union vari vari ' in visited := TypeMap . add ty vari ! visited ; let compute_same = compute_variance_rec vari in match ty . desc with Tarrow ( _ , ty1 , ty2 , _ ) -> let open Variance in let v = conjugate vari in let v1 = if mem May_pos v || mem May_neg v then set May_weak true v else v in compute_variance_rec v1 ty1 ; compute_same ty2 | Ttuple tl -> List . iter compute_same tl | Tconstr ( path , tl , _ ) -> let open Variance in if tl = [ ] then ( ) else begin try let decl = Env . find_type path env in let cvari f = mem f vari in List . iter2 ( fun ty v -> let cv f = mem f v in let strict = cvari Inv && cv Inj || ( cvari Pos || cvari Neg ) && cv Inv in if strict then compute_variance_rec full ty else let p1 = inter v vari and n1 = inter v ( conjugate vari ) in let v1 = union ( inter covariant ( union p1 ( conjugate p1 ) ) ) ( inter ( conjugate covariant ) ( union n1 ( conjugate n1 ) ) ) and weak = cvari May_weak && ( cv May_pos || cv May_neg ) || ( cvari May_pos || cvari May_neg ) && cv May_weak in let v2 = set May_weak weak v1 in compute_variance_rec v2 ty ) tl decl . type_variance with Not_found -> List . iter ( compute_variance_rec may_inv ) tl end | Tobject ( ty , _ ) -> compute_same ty | Tfield ( _ , _ , ty1 , ty2 ) -> compute_same ty1 ; compute_same ty2 | Tsubst ty -> compute_same ty | Tvariant row -> let row = Btype . row_repr row in List . iter ( fun ( _ , f ) -> match Btype . row_field_repr f with Rpresent ( Some ty ) -> compute_same ty | Reither ( _ , tyl , _ , _ ) -> let open Variance in let upper = List . fold_left ( fun s f -> set f true s ) null [ May_pos ; May_neg ; May_weak ] in let v = inter vari upper in List . iter ( compute_variance_rec v ) tyl | _ -> ( ) ) row . row_fields ; compute_same row . row_more | Tpoly ( ty , _ ) -> compute_same ty | Tvar _ | Tnil | Tlink _ | Tunivar _ -> ( ) | Tpackage ( _ , _ , tyl ) -> let v = Variance . ( if mem Pos vari || mem Neg vari then full else may_inv ) in List . iter ( compute_variance_rec v ) tyl in compute_variance_rec vari ty |
let make p n i = let open Variance in set May_pos p ( set May_neg n ( set May_weak n ( set Inj i null ) ) ) |
let compute_variance_type env ~ check ( required , loc ) decl tyl = let required = List . map ( fun ( c , n , i ) -> if c || n then ( c , n , i ) else ( true , true , i ) ) required in let params = List . map Btype . repr decl . type_params in let tvl = ref TypeMap . empty in let open Variance in List . iter ( fun ( cn , ty ) -> compute_variance env tvl ( if cn then full else covariant ) ty ) tyl ; if check then begin let pos = ref 0 in List . iter2 ( fun ty ( c , n , i ) -> incr pos ; let var = get_variance ty tvl in let ( co , cn ) = get_upper var and ij = mem Inj var in if Btype . is_Tvar ty && ( co && not c || cn && not n || not ij && i ) then raise ( Error ( loc , Bad_variance ( Variance_not_satisfied ! pos , ( co , cn , ij ) , ( c , n , i ) ) ) ) ) params required ; let args = Btype . newgenty ( Ttuple params ) in let fvl = Ctype . free_variables args in let fvl = List . filter ( fun v -> not ( List . memq v params ) ) fvl in if fvl = [ ] then ( ) else let tvl2 = ref TypeMap . empty in List . iter2 ( fun ty ( p , n , _ ) -> if Btype . is_Tvar ty then ( ) else let v = if p then if n then full else covariant else conjugate covariant in compute_variance env tvl2 v ty ) params required ; let visited = ref TypeSet . empty in let rec check ty = let ty = Ctype . repr ty in if TypeSet . mem ty ! visited then ( ) else let visited ' = TypeSet . add ty ! visited in visited := visited ' ; let v1 = get_variance ty tvl in let snap = Btype . snapshot ( ) in let v2 = TypeMap . fold ( fun t vt v -> if Ctype . equal env false [ ty ] [ t ] then union vt v else v ) ! tvl2 null in Btype . backtrack snap ; let ( c1 , n1 ) = get_upper v1 and ( c2 , n2 , _ , i2 ) = get_lower v2 in if c1 && not c2 || n1 && not n2 then if List . memq ty fvl then let code = if not i2 then No_variable else if c2 || n2 then Variance_not_reflected else Variance_not_deducible in raise ( Error ( loc , Bad_variance ( code , ( c1 , n1 , false ) , ( c2 , n2 , false ) ) ) ) else Btype . iter_type_expr check ty in List . iter ( fun ( _ , ty ) -> check ty ) tyl ; end ; List . map2 ( fun ty ( p , n , i ) -> let v = get_variance ty tvl in let tr = decl . type_private in let concr = decl . type_kind <> Type_abstract in let ( p , n ) = if tr = Private || not ( Btype . is_Tvar ty ) then ( p , n ) else ( false , false ) and i = concr || i && tr = Private in let v = union v ( make p n i ) in let v = if not concr then v else if mem Pos v && mem Neg v then full else if Btype . is_Tvar ty then v else union v ( if p then if n then full else covariant else conjugate covariant ) in if decl . type_kind = Type_abstract && tr = Public then v else set May_weak ( mem May_neg v ) v ) params required |
let add_false = List . map ( fun ty -> false , ty ) |
let constrained vars ty = match ty . desc with | Tvar _ -> List . exists ( fun tl -> List . memq ty tl ) vars | _ -> true |
let for_constr = function | Types . Cstr_tuple l -> add_false l | Types . Cstr_record l -> List . map ( fun { Types . ld_mutable ; ld_type } -> ( ld_mutable = Mutable , ld_type ) ) l |
let compute_variance_gadt env ~ check ( required , loc as rloc ) decl ( tl , ret_type_opt ) = match ret_type_opt with | None -> compute_variance_type env ~ check rloc { decl with type_private = Private } ( for_constr tl ) | Some ret_type -> match Ctype . repr ret_type with | { desc = Tconstr ( _ , tyl , _ ) } -> let tyl = List . map Ctype . repr tyl in let fvl = List . map ( Ctype . free_variables ? env : None ) tyl in let _ = List . fold_left2 ( fun ( fv1 , fv2 ) ty ( c , n , _ ) -> match fv2 with [ ] -> assert false | fv :: fv2 -> if ( c || n ) && constrained ( fv1 @ fv2 ) ty then raise ( Error ( loc , Varying_anonymous ) ) ; ( fv :: fv1 , fv2 ) ) ( [ ] , fvl ) tyl required in compute_variance_type env ~ check rloc { decl with type_params = tyl ; type_private = Private } ( for_constr tl ) | _ -> assert false |
let compute_variance_extension env ~ check decl ext rloc = compute_variance_gadt env ~ check rloc { decl with type_params = ext . ext_type_params } ( ext . ext_args , ext . ext_ret_type ) |
let compute_variance_decl env ~ check decl ( required , _ as rloc ) = if ( decl . type_kind = Type_abstract || decl . type_kind = Type_open ) && decl . type_manifest = None then List . map ( fun ( c , n , i ) -> make ( not n ) ( not c ) ( decl . type_kind <> Type_abstract || i ) ) required else let mn = match decl . type_manifest with None -> [ ] | Some ty -> [ false , ty ] in match decl . type_kind with Type_abstract | Type_open -> compute_variance_type env ~ check rloc decl mn | Type_variant tll -> if List . for_all ( fun c -> c . Types . cd_res = None ) tll then compute_variance_type env ~ check rloc decl ( mn @ List . flatten ( List . map ( fun c -> for_constr c . Types . cd_args ) tll ) ) else begin let mn = List . map ( fun ( _ , ty ) -> ( Types . Cstr_tuple [ ty ] , None ) ) mn in let tll = mn @ List . map ( fun c -> c . Types . cd_args , c . Types . cd_res ) tll in match List . map ( compute_variance_gadt env ~ check rloc decl ) tll with | vari :: rem -> let varl = List . fold_left ( List . map2 Variance . union ) vari rem in List . map Variance . ( fun v -> if mem Pos v && mem Neg v then full else v ) varl | _ -> assert false end | Type_record ( ftl , _ ) -> compute_variance_type env ~ check rloc decl ( mn @ List . map ( fun { Types . ld_mutable ; ld_type } -> ( ld_mutable = Mutable , ld_type ) ) ftl ) |
let is_hash id = let s = Ident . name id in String . length s > 0 && s . [ 0 ] = ' ' # |
let check_variance_extension env decl ext rloc = ignore ( compute_variance_extension env ~ check : true decl ext . Typedtree . ext_type rloc ) |
let compute_decl env ~ check decl req = compute_variance_decl env ~ check decl ( req , decl . type_loc ) |
let check_decl env decl req = ignore ( compute_variance_decl env ~ check : true decl ( req , decl . type_loc ) ) |
type prop = Variance . t list |
type req = surface_variance list |
let property : ( prop , req ) Typedecl_properties . property = let open Typedecl_properties in let eq li1 li2 = try List . for_all2 Variance . eq li1 li2 with _ -> false in let merge ~ prop ~ new_prop = List . map2 Variance . union prop new_prop in let default decl = List . map ( fun _ -> Variance . null ) decl . type_params in let compute env decl req = compute_decl env ~ check : false decl req in let update_decl decl variance = { decl with type_variance = variance } in let check env id decl req = if is_hash id then ( ) else check_decl env decl req in { eq ; merge ; default ; compute ; update_decl ; check ; } |
let transl_variance : Asttypes . variance -> _ = function | Covariant -> ( true , false , false ) | Contravariant -> ( false , true , false ) | Invariant -> ( false , false , false ) |
let variance_of_params ptype_params = List . map transl_variance ( List . map snd ptype_params ) |
let variance_of_sdecl sdecl = variance_of_params sdecl . Parsetree . ptype_params |
let update_decls env sdecls decls = let required = List . map variance_of_sdecl sdecls in Typedecl_properties . compute_property property env decls required |
let update_class_decls env cldecls = let decls , required = List . fold_right ( fun ( obj_id , obj_abbr , _cl_abbr , _clty , _cltydef , ci ) ( decls , req ) -> ( obj_id , obj_abbr ) :: decls , variance_of_params ci . Typedtree . ci_params :: req ) cldecls ( [ ] , [ ] ) in let decls = Typedecl_properties . compute_property property env decls required in List . map2 ( fun ( _ , decl ) ( _ , _ , cl_abbr , clty , cltydef , _ ) -> let variance = decl . type_variance in ( decl , { cl_abbr with type_variance = variance } , { clty with cty_variance = variance } , { cltydef with clty_variance = variance } ) ) decls cldecls |
type type_decl = { td_name : string ; td_mod : string ; td_type : idltype ; td_abstract : bool ; td_c2ml : string option ; td_ml2c : string option ; td_finalize : string option ; td_compare : string option ; td_hash : string option ; td_errorcode : bool ; td_errorcheck : string option ; td_mltype : string option } |
let find = ref ( ( fun _ -> invalid_arg " Typedef . find " ) : string -> type_decl ) |
let ml_declaration oc td = match td with { td_mltype = Some s } -> fprintf oc " % s = % s \ n " ( String . uncapitalize_ascii td . td_name ) s | { td_abstract = true } -> fprintf oc " % s \ n " ( String . uncapitalize_ascii td . td_name ) | _ -> fprintf oc " % s = % a \ n " ( String . uncapitalize_ascii td . td_name ) out_ml_type td . td_type |
let c_declaration oc td = fprintf oc " typedef % a ; \ n " out_c_decl ( td . td_name , td . td_type ) ; begin match td . td_ml2c with None -> ( ) | Some s -> fprintf oc " extern void % s ( value , % s ) ; *\ n " s td . td_name end ; begin match td . td_c2ml with None -> ( ) | Some s -> fprintf oc " extern value % s ( % s ) ; *\ n " s td . td_name end ; begin match td . td_finalize with None -> ( ) | Some s -> fprintf oc " extern void % s ( % s ) ; *\ n " s td . td_name end ; begin match td . td_compare with None -> ( ) | Some s -> fprintf oc " extern int % s ( % s , * % s ) ; *\ n " s td . td_name td . td_name end ; begin match td . td_hash with None -> ( ) | Some s -> fprintf oc " extern long % s ( % s ) ; *\ n " s td . td_name end ; fprintf oc " \ n " |
let declare_transl oc td = begin match td . td_ml2c with Some s -> fprintf oc " extern void % s ( value , % s ) ; *\ n " s td . td_name ; fprintf oc " # define camlidl_ml2c_ % s_ % s ( v , c , ctx ) % s ( v , c ) \ n \ n " td . td_mod td . td_name s | None -> fprintf oc " extern void camlidl_ml2c_ % s_ % s ( value , % s , * camlidl_ctx _ctx ) ; \ n " td . td_mod td . td_name td . td_name end ; begin match td . td_c2ml with Some s -> fprintf oc " extern value % s ( % s ) ; *\ n " s td . td_name ; fprintf oc " # define camlidl_c2ml_ % s_ % s ( c , ctx ) % s ( c ) \ n \ n " td . td_mod td . td_name s | None -> fprintf oc " extern value camlidl_c2ml_ % s_ % s ( % s , * camlidl_ctx _ctx ) ; \ n " td . td_mod td . td_name td . td_name end ; fprintf oc " \ n " |
let is_custom_block td = td . td_abstract && not ( td . td_finalize = None && td . td_compare = None && td . td_hash = None ) |
let transl_ml_to_c oc td = current_function := sprintf " typedef % s " td . td_name ; let v = new_var " _v " in let c = new_var " _c " in fprintf oc " void camlidl_ml2c_ % s_ % s ( value % s , % s * % s , camlidl_ctx _ctx ) \ n " td . td_mod td . td_name v td . td_name c ; fprintf oc " { \ n " ; let pc = divert_output ( ) in increase_indent ( ) ; if td . td_abstract then if is_custom_block td then begin iprintf pc " *% s = ( ( *% s ) * Data_custom_val ( % s ) ) ; \ n " c td . td_name v end else begin iprintf pc " *% s = ( ( *% s ) * Bp_val ( % s ) ) ; \ n " c td . td_name v end else begin ml_to_c pc false Prefix . empty td . td_type v ( sprintf " |
let transl_c_to_ml oc td = begin match td . td_finalize with None -> ( ) | Some f -> fprintf oc " \ { % s ( ( % s ) * Data_custom_val ( v ) ) ; } end ; begin match td . td_compare with None -> ( ) | Some f -> fprintf oc " \ { return % s ( ( % s ) * Data_custom_val ( v1 ) , ( % s ) * Data_custom_val ( v2 ) ) ; } end ; begin match td . td_hash with None -> ( ) | Some f -> fprintf oc " \ { return % s ( ( % s ) * Data_custom_val ( v ) ) ; } end ; if is_custom_block td then begin fprintf oc " struct custom_operations camlidl_cops_ % s_ % s = { \ n " td . td_mod td . td_name ; fprintf oc " NULL , \ n " ; begin match td . td_finalize with None -> iprintf oc " custom_finalize_default , \ n " | Some f -> iprintf oc " camlidl_finalize_ % s_ % s , \ n " td . td_mod td . td_name end ; begin match td . td_compare with None -> iprintf oc " custom_compare_default , \ n " | Some f -> iprintf oc " camlidl_compare_ % s_ % s , \ n " td . td_mod td . td_name end ; begin match td . td_hash with None -> iprintf oc " custom_hash_default , \ n " | Some f -> iprintf oc " camlidl_hash_ % s_ % s , \ n " td . td_mod td . td_name end ; iprintf oc " custom_serialize_default , \ n " ; iprintf oc " custom_deserialize_default \ n " ; fprintf oc " } ; \ n \ n " end ; current_function := sprintf " typedef % s " td . td_name ; let v = new_ml_variable ( ) in let c = new_var " _c " in fprintf oc " value camlidl_c2ml_ % s_ % s ( % s * % s , camlidl_ctx _ctx ) \ n " td . td_mod td . td_name td . td_name c ; fprintf oc " { \ n " ; let pc = divert_output ( ) in increase_indent ( ) ; if td . td_abstract then if is_custom_block td then begin iprintf pc " % s = caml_alloc_custom ( & camlidl_cops_ % s_ % s , sizeof ( % s ) , 0 , 1 ) ; \ n " v td . td_mod td . td_name td . td_name ; iprintf pc " ( ( *% s ) * Data_custom_val ( % s ) ) = *% s ; \ n " td . td_name v c end else begin iprintf pc " % s = camlidl_alloc ( ( sizeof ( % s ) + sizeof ( value ) - 1 ) / sizeof ( value ) , Abstract_tag ) ; \ n " v td . td_name ; iprintf pc " ( ( *% s ) * Bp_val ( % s ) ) = *% s ; \ n " td . td_name v c end else begin c_to_ml pc Prefix . empty td . td_type ( sprintf " |
let emit_transl oc td = begin match td . td_ml2c with Some s -> fprintf oc " # define camlidl_ml2c_ % s_ % s ( v , c , ctx ) % s ( v , c ) \ n \ n " td . td_mod td . td_name s | None -> transl_ml_to_c oc td end ; begin match td . td_c2ml with Some s -> fprintf oc " # define camlidl_c2ml_ % s_ % s ( c , ctx ) % s ( c ) \ n \ n " td . td_mod td . td_name s | None -> transl_c_to_ml oc td end |
module Inductive = struct type notation = Name . t * VarEnv . t * Type . t type t = { constructor_records : ( Name . t * ( AdtConstructors . RecordSkeleton . t * VarEnv . t * Type . t ) t list ) list list ; notations : notation list ; records : AdtConstructors . RecordSkeleton . t list ; typs : ( Name . t * Name . t list * AdtConstructors . t ) t list ; } let get_notation_module_name ( inductive : t ) t : SmartPrint . t = " !^ ConstructorRecords_ " ^-^ separate " !^ _ " ( inductive . constructor_records |> List . map ( fun ( name , _ ) _ -> Name . to_coq name ) name ) name let to_coq_constructor_records ( inductive : t ) t : SmartPrint . t option = match inductive . constructor_records with | [ ] -> None | constructor_records -> let notation_module_name = get_notation_module_name inductive in Some ( " " !^ ^^ newline ^^ " !^ Module " ^^ notation_module_name ^-^ " . " !^ ^^ newline ^^ indent ( separate newline ( constructor_records |> List . map ( fun ( name , records ) records -> " !^ Module " ^^ Name . to_coq name ^-^ " . " !^ ^^ newline ^^ indent ( separate ( newline ^^ newline ) newline ( records |> List . map ( fun ( record , _ , _ ) _ -> AdtConstructors . RecordSkeleton . to_coq record ) record ) record ) record ^^ newline ^^ " !^ End " ^^ Name . to_coq name ^-^ ) ) ) " . " !^ ^^ newline ^^ " !^ End " ^^ notation_module_name ^-^ " . " !^ ^^ newline ^^ " !^ Import " ^^ notation_module_name ^-^ ) " . " !^ let to_coq_notation_name ( name : Name . t ) t : SmartPrint . t = " " !^\ ' " ^-^ Name . to_coq name ^-^ " " " !^\ let to_coq_notation_record ( prefix : Name . t ) t ( name : Name . t ) t : SmartPrint . t = " " !^\ ' " ^-^ Name . to_coq prefix ^-^ " . " !^ ^-^ Name . to_coq name ^-^ " " " !^\ let to_coq_notations_reserved ( inductive : t ) t : SmartPrint . t list = ( inductive . constructor_records |> List . map ( fun ( name , records ) records -> records |> List . map ( fun ( { AdtConstructors . RecordSkeleton . module_name ; _ } , _ , _ ) _ -> " !^ Reserved Notation " ^^ to_coq_notation_record name module_name ^-^ ) ) " . " !^ |> List . concat ) concat @ ( inductive . notations |> List . map ( fun ( name , _ , _ ) _ -> " !^ Reserved Notation " ^^ to_coq_notation_name name ^-^ ) ) " . " !^ let to_coq_record_skeletons ( inductive : t ) t : SmartPrint . t list = inductive . records |> List . map AdtConstructors . RecordSkeleton . to_coq let to_coq_typs ( fargs : FArgs . t ) t ( subst : Type . Subst . t ) t ( is_first : bool ) bool ( name : Name . t ) t ( left_typ_args : Name . t list ) list ( constructors : AdtConstructors . t ) t : SmartPrint . t = let keyword = if is_first then " !^ Inductive " else " !^ with " in nest ( keyword ^^ Name . to_coq name ^^ FArgs . to_coq fargs ^^ ( if left_typ_args = [ ] then empty else parens ( nest ( separate space ( left_typ_args |> List . map Name . to_coq ) to_coq ^^ " " !^: ^^ Pp . set ) set ) set ) set ^^ " " !^: ^^ let res_typ_length = match constructors with | [ ] -> 0 | constructor :: _ -> ( match constructor . res_typ_params with | AdtConstructors . Variant l -> List . length l | Tagged l -> List . length l ) l in let is_tagged = match constructors with [ ] -> false | c :: _ -> c . is_tagged in let arity = if not is_tagged then 1 else res_typ_length + 1 in let index_typs = List . init arity ( fun i -> if i = arity - 1 then Pp . set else " !^ vtag ) " in separate ( space ^^ " " !^-> ^^ space ) space index_typs ^^ " " !^:= ^-^ separate empty ( constructors |> List . map ( fun { AdtConstructors . constructor_name ; param_typs ; res_typ_params ; typ_vars ; _ ; } -> newline ^^ nest ( " " !^| ^^ Name . to_coq constructor_name ^^ " " !^: ^^ Type . typ_vars_to_coq braces " !^ forall " " , " !^ typ_vars ^^ group @@ separate space ( param_typs |> List . map ( fun param_typ -> group ( Type . to_coq ( Some subst ) subst ( Some Type . Context . Arrow ) Arrow param_typ ^^ ) ) ) " " !^-> ^^ let res_typ_params = match res_typ_params with | Variant vars -> let typs = vars |> AdtParameters . get_parameters |> List . map ( fun var -> Type . Variable var ) var in List . combine typs ( Type . tag_no_args typs ) typs | Tagged typ_params -> List . combine typ_params ( Type . tag_all_args typ_params ) typ_params in Type . to_coq ( Some subst ) subst None ( Type . Apply ( MixedPath . of_name name , res_typ_params ) res_typ_params ) res_typ_params ) ) ) ) let to_coq_notations_where ( subst : Type . Subst . t ) t ( notation : SmartPrint . t ) t ( typ_args : VarEnv . t ) t ( typ : Type . t ) t : SmartPrint . t = let typ = List . fold_left ( fun typ ( typ_arg , _ ) _ -> Type . subst_name typ_arg ( Name . prefix_by_t typ_arg ) typ_arg typ ) typ typ typ_args in let typ_args = typ_args |> List . map ( fun ( name , typ ) typ -> ( Name . prefix_by_t name , typ ) typ ) typ in nest ( notation ^^ " " !^:= ^^ parens ( ( match typ_args with | [ ] -> empty | _ :: _ -> Type . typ_vars_to_coq parens " !^ fun " " !^ => " typ_args ) typ_args ^-^ Type . to_coq ( Some subst ) subst None typ ) typ ) typ let rec sort_notations ( sorted : notation list ) list ( to_sort : notation list ) list : notation list = let to_sort_names = List . map ( fun ( name , _ , _ ) _ -> name ) name to_sort in let selected , to_sort = to_sort |> List . partition ( fun ( _ , typ_args , typ ) typ -> let dependencies = Name . Set . diff ( Type . local_typ_constructors_of_typ typ ) typ ( Name . Set . of_list ( List . map fst typ_args ) typ_args ) typ_args in not ( dependencies |> Name . Set . exists ( fun dependency -> List . mem dependency to_sort_names ) to_sort_names ) to_sort_names ) to_sort_names in match selected with | [ ] -> sorted @ to_sort | _ :: _ -> sort_notations ( sorted @ selected ) selected to_sort let to_coq_notations_wheres ( subst : Type . Subst . t ) t ( inductive : t ) t : SmartPrint . t list = let sorted_notations = sort_notations [ ] inductive . notations in ( sorted_notations |> List . map ( fun ( name , typ_args , typ ) typ -> to_coq_notations_where subst ( to_coq_notation_name name ) name typ_args typ ) typ ) @ ( inductive . constructor_records |> List . map ( fun ( name , records ) records -> records |> List . map ( fun ( { AdtConstructors . RecordSkeleton . module_name ; _ } , typ_args , typ ) -> to_coq_notations_where subst ( to_coq_notation_record name module_name ) module_name typ_args typ ) typ ) typ |> List . concat ) concat let to_coq_notations_definition ( name : Name . t ) t ( definition : SmartPrint . t ) t : SmartPrint . t = nest ( " !^ Definition " ^^ Name . to_coq name ^^ " " !^:= ^^ definition ^-^ ) " . " !^ let to_coq_notations_record_definitions ( inductive : t ) t : SmartPrint . t option = match inductive . constructor_records with | [ ] -> None | _ :: _ -> let notation_module_name = get_notation_module_name inductive in Some ( separate newline ( inductive . constructor_records |> List . map ( fun ( name , records ) records -> " !^ Module " ^^ Name . to_coq name ^-^ " . " !^ ^^ newline ^^ indent ( " !^ Include " ^^ notation_module_name ^-^ " . " !^ ^-^ Name . to_coq name ^-^ " . " !^ ^^ newline ^^ separate newline ( records |> List . map ( fun ( { AdtConstructors . RecordSkeleton . module_name ; _ ; } , _ , _ ) -> to_coq_notations_definition module_name ( " !^ ' " ^-^ Name . to_coq name ^-^ " . " !^ ^-^ Name . to_coq module_name ) module_name ) module_name ) module_name ) module_name ^^ newline ^^ " !^ End " ^^ Name . to_coq name ^-^ ) ) ) " . " !^ let to_coq_notations_definitions ( inductive : t ) t : SmartPrint . t list = inductive . notations |> List . map ( fun ( name , _ , _ ) _ -> to_coq_notations_definition name ( Name . to_coq ( Name . prefix_by_single_quote name ) name ) name ) name let to_coq_typs_implicits ( fargs : FArgs . t ) t ( left_typ_args : Name . t list ) list ( constructors : AdtConstructors . t ) t : SmartPrint . t list = match left_typ_args with | [ ] -> [ ] | _ :: _ -> constructors |> List . map ( fun { AdtConstructors . constructor_name ; typ_vars ; _ } -> " !^ Arguments " ^^ Name . to_coq constructor_name ^^ braces ( nest ( separate space ( FArgs . to_coq_underscores fargs @ ( left_typ_args |> List . map ( fun _ -> " !^ _ ) ) " @ ( typ_vars |> List . map ( fun _ -> " !^ _ ) ) ) ) ) " ^-^ ) " . " !^ let to_coq ( fargs : FArgs . t ) t ( inductive : t ) t : SmartPrint . t = let subst = { Type . Subst . path_name = ( fun path_name -> match path_name with | { PathName . path = [ ] ; base } -> let use_notation = inductive . notations |> List . exists ( fun ( name , _ , _ ) _ -> name = base ) base in if use_notation then { path = [ ] ; base = Name . prefix_by_single_quote base } else path_name | { path = [ prefix ] ; base } -> let use_notation = inductive . constructor_records |> List . exists ( fun ( name , records ) records -> records |> List . exists ( fun ( { AdtConstructors . RecordSkeleton . module_name ; _ ; } , _ , _ ) -> name = prefix && module_name = base ) base ) base in if use_notation then { path = [ Name . prefix_by_single_quote prefix ] ; base } else path_name | _ -> path_name ) path_name ; } in let constructor_records = to_coq_constructor_records inductive in let record_skeletons = to_coq_record_skeletons inductive in let reserved_notations = to_coq_notations_reserved inductive in let notations_wheres = to_coq_notations_wheres subst inductive in let notations_record_definitions = to_coq_notations_record_definitions inductive in let notations_definitions = to_coq_notations_definitions inductive in let implicit_arguments = List . concat ( inductive . typs |> List . map ( fun ( _ , left_typ_args , constructors ) constructors -> to_coq_typs_implicits fargs left_typ_args constructors ) constructors ) constructors in nest ( ( match constructor_records with | None -> empty | Some constructor_records -> constructor_records ^^ newline ^^ newline ) newline ^^ ( match record_skeletons with | [ ] -> empty | _ :: _ -> separate ( newline ^^ newline ) newline record_skeletons ^^ newline ^^ newline ) newline ^^ ( match reserved_notations with | [ ] -> empty | _ :: _ -> separate newline reserved_notations ^^ newline ^^ newline ) newline ^^ separate ( newline ^^ newline ) newline ( inductive . typs |> List . mapi ( fun index ( name , left_typ_args , constructors ) constructors -> let is_first = index = 0 in to_coq_typs fargs subst is_first name left_typ_args constructors ) constructors ) constructors ^-^ ( match notations_wheres with | [ ] -> empty | _ :: _ -> newline ^^ newline ^^ " !^ where " ^-^ separate ( newline ^^ " !^ and ) " notations_wheres ) notations_wheres ^-^ " . " !^ ^^ ( match notations_record_definitions with | None -> empty | Some notations -> newline ^^ newline ^^ notations ) notations ^^ ( match notations_definitions with | [ ] -> empty | _ :: _ -> newline ^^ newline ^^ separate newline notations_definitions ) notations_definitions ^^ match implicit_arguments with | [ ] -> empty | _ :: _ -> newline ^^ newline ^^ separate newline implicit_arguments ) implicit_arguments end |
type t = | Inductive of Inductive . t | Record of Name . t * VarEnv . t * ( Name . t * Type . t ) t list * bool | Synonym of Name . t * Name . t list * Type . t | ExtensibleTyp of Name . t | Abstract of Name . t * Name . t list |
let filter_in_free_vars ( typ_args : Name . t list ) list ( free_vars : Name . Set . t ) t : Name . t list = typ_args |> List . filter_map ( function typ_arg -> if Name . Set . mem typ_arg free_vars then Some typ_arg else None ) None |
let of_ocaml ( typs : type_declaration list ) list : t Monad . t = match typs with | [ { typ_id ; typ_type = { type_manifest = Some typ ; type_params ; _ } ; _ } ] -> ( let * name = Name . of_ident false typ_id in AdtParameters . of_ocaml type_params >>= fun ind_vars -> let typ_args = AdtParameters . get_parameters ind_vars in match typ . Types . desc with | Tvariant { row_fields ; _ } -> Monad . List . map ( AdtConstructors . of_ocaml_row ind_vars ) ind_vars row_fields >>= fun single_constructors -> AdtConstructors . of_ocaml ind_vars single_constructors >>= fun ( constructors , _ ) _ -> raise ( Inductive { constructor_records = [ ] ; notations = [ ] ; records = [ ] ; typs = [ ( name , typ_args , constructors ) constructors ] ; } ) NotSupported " Polymorphic variant types are defined as standard algebraic types " | _ -> Type . of_type_expr_without_free_vars typ >>= fun typ -> let free_vars = Type . typ_args_of_typ typ in let typ_args = filter_in_free_vars typ_args free_vars in return ( Synonym ( name , typ_args , typ ) typ ) typ ) typ | [ { typ_id ; typ_type = { type_kind = Type_abstract ; type_manifest = None ; type_params ; _ } ; typ_attributes ; _ ; } ; ] -> Attribute . of_attributes typ_attributes >>= fun typ_attributes -> let * name = Name . of_ident false typ_id in AdtParameters . of_ocaml type_params >>= fun typ_args -> let typ_args_with_unknowns = if not ( Attribute . has_phantom typ_attributes ) typ_attributes then AdtParameters . get_parameters typ_args else [ ] in return ( Abstract ( name , typ_args_with_unknowns ) typ_args_with_unknowns ) typ_args_with_unknowns | [ { typ_id ; typ_type = { type_kind = Type_record ( fields , _ ) _ ; type_params ; _ } ; _ ; } ; ] -> let * name = Name . of_ident false typ_id in fields |> Monad . List . map ( fun { Types . ld_id = x ; ld_type = typ ; _ } -> let * x = Name . of_ident false x in Type . of_typ_expr true Name . Map . empty typ >>= fun ( typ , _ , new_typ_args ) new_typ_args -> return ( x , typ , new_typ_args ) new_typ_args ) new_typ_args >>= fun fields -> let * _ , _ , typ_args = Type . of_typs_exprs true type_params Name . Map . empty in let typ_args = List . map fst typ_args in let fields_names , fields_types , new_typ_args = Util . List . split3 fields in let new_typ_args = VarEnv . unions new_typ_args in let typ_args = VarEnv . reorg typ_args new_typ_args in let * fields_types = Monad . List . map ( Type . decode_var_tags typ_args false ) false fields_types in let fields = List . combine fields_names fields_types in return ( Record ( name , typ_args , fields , true ) true ) true | [ { typ_id ; typ_type = { type_kind = Type_open ; _ } ; _ } ] -> let * name = Name . of_ident false typ_id in return ( ExtensibleTyp name ) name | _ -> typs |> Monad . List . fold_left ( fun ( constructor_records , notations , records , typs ) typs typ -> set_loc typ . typ_loc ( let * name = Name . of_ident false typ . typ_id in AdtParameters . of_ocaml typ . typ_type . type_params >>= fun typ_args -> match typ with | { typ_type = { type_manifest = Some typ ; _ } ; _ } -> ( match typ . Types . desc with | Tvariant { row_fields ; _ } -> Monad . List . map ( AdtConstructors . of_ocaml_row typ_args ) typ_args row_fields >>= fun single_constructors -> AdtConstructors . of_ocaml typ_args single_constructors >>= fun ( constructors , _ ) _ -> raise ( constructor_records , notations , records , ( name , typ_args , constructors ) constructors :: typs ) NotSupported " Polymorphic variant types are defined as standard \ algebraic types " | _ -> Type . of_typ_expr true Name . Map . empty typ >>= fun ( typ , _ , typ_args ) typ_args -> let * typ = Type . decode_var_tags typ_args false typ in return ( constructor_records , ( name , typ_args , typ ) typ :: notations , records , typs ) ) | { typ_type = { type_kind = Type_abstract ; type_manifest = None ; _ } ; _ ; } -> raise ( constructor_records , notations , records , typs ) typs NotSupported " Abstract types not supported in mutually recursive \ definitions " | { typ_type = { type_kind = Type_record ( fields , _ ) _ ; type_params = params ; _ ; } ; _ ; } -> fields |> Monad . List . map ( fun { Types . ld_id = x ; ld_type = typ ; _ } -> let * x = Name . of_ident false x in Type . of_typ_expr true Name . Map . empty typ >>= fun ( typ , _ , new_typ_args ) new_typ_args -> return ( x , typ , new_typ_args ) new_typ_args ) new_typ_args >>= fun fields -> let * _ , _ , typ_args = Type . of_typs_exprs true params Name . Map . empty in let typ_args = List . map fst typ_args in let fields_names , fields_types , new_typ_args = Util . List . split3 fields in let new_typ_args = VarEnv . unions new_typ_args in let typ_args = VarEnv . reorg typ_args new_typ_args in let * fields_types = Monad . List . map ( Type . decode_var_tags typ_args false ) false fields_types in return ( constructor_records , ( name , typ_args , Type . Apply ( MixedPath . of_name ( Name . suffix_by_skeleton name ) name , List . combine fields_types ( Type . tag_no_args fields_types ) fields_types ) ) :: notations , { AdtConstructors . RecordSkeleton . fields = fields_names ; module_name = name ; typ_name = Name . suffix_by_skeleton name ; } :: records , typs ) | { typ_type = { type_kind = Type_variant cases ; _ } ; typ_attributes ; _ ; } -> let * attributes = Attribute . of_attributes typ_attributes in Monad . List . map ( AdtConstructors . of_ocaml_case name attributes typ_args ) typ_args cases >>= fun cases -> let single_constructors , new_constructor_records = List . split cases in let new_constructor_records = new_constructor_records |> List . filter_map ( fun x -> x ) x in let constructor_records = match new_constructor_records with | [ ] -> constructor_records | _ :: _ -> ( name , new_constructor_records ) new_constructor_records :: constructor_records in AdtConstructors . of_ocaml typ_args single_constructors >>= fun ( constructors , merged_typ_params ) merged_typ_params -> return ( constructor_records , notations , records , ( name , merged_typ_params , constructors ) constructors :: typs ) | { typ_type = { type_kind = Type_open ; _ } ; _ } -> raise ( constructor_records , notations , records , typs ) typs ExtensibleType " We do not handle extensible types in mutually recursive \ definitions ) ) " ( [ ] , [ ] , [ ] , [ ] ) >>= fun ( constructor_records , notations , records , typs ) typs -> let typs = typs |> List . map ( function x , y , z -> ( x , AdtParameters . get_parameters y , z ) z ) z in return ( Inductive { constructor_records = List . rev constructor_records ; notations = List . rev notations ; records ; typs = List . rev typs ; } ) |
let to_coq ( fargs : FArgs . t ) t ( def : t ) t : SmartPrint . t = match def with | Inductive inductive -> Inductive . to_coq fargs inductive | Record ( name , typ_args , fields , with_with ) with_with -> AdtConstructors . RecordSkeleton . to_coq_record name name typ_args fields with_with | Synonym ( name , typ_args , value ) value -> nest ( " !^ Definition " ^^ Name . to_coq name ^^ FArgs . to_coq fargs ^^ ( match typ_args with | [ ] -> empty | _ -> parens ( separate space ( List . map Name . to_coq typ_args ) typ_args ^^ " " !^: ^^ Pp . set ) set ) set ^^ nest ( " " !^: ^^ Pp . set ) set ^^ " " !^:= ^^ Type . to_coq None None value ^-^ ) " . " !^ | ExtensibleTyp name -> nest ( " !^ Definition " ^^ Name . to_coq name ^^ " " !^:= ^^ " !^ extensible_type " ^-^ ) " . " !^ | Abstract ( name , typ_args ) typ_args -> nest ( " !^ Parameter " ^^ Name . to_coq name ^^ " " !^: ^^ ( match typ_args with | [ ] -> empty | _ :: _ -> " !^ forall " ^^ parens ( group ( separate space ( List . map Name . to_coq typ_args ) typ_args ^^ " " !^: ^^ Pp . set ) set ) set ^-^ ) " , " !^ ^^ Pp . set ^-^ ) " . " !^ |
type partial = Partial | Total |
type attributes = attribute list |
type pattern = { pat_desc : pattern_desc ; pat_loc : Location . t ; pat_extra : ( pat_extra * Location . t * attribute list ) list ; pat_type : type_expr ; mutable pat_env : Env . t ; pat_attributes : attribute list ; } | Tpat_constraint of core_type | Tpat_type of Path . t * Longident . t loc | Tpat_open of Path . t * Longident . t loc * Env . t | Tpat_unpack Tpat_any | Tpat_var of Ident . t * string loc | Tpat_alias of pattern * Ident . t * string loc | Tpat_constant of constant | Tpat_tuple of pattern list | Tpat_construct of Longident . t loc * constructor_description * pattern list | Tpat_variant of label * pattern option * row_desc ref | Tpat_record of ( Longident . t loc * label_description * pattern ) list * closed_flag | Tpat_array of pattern list | Tpat_or of pattern * pattern * row_desc option | Tpat_lazy of pattern | Tpat_exception of pattern { exp_desc : expression_desc ; exp_loc : Location . t ; exp_extra : ( exp_extra * Location . t * attribute list ) list ; exp_type : type_expr ; exp_env : Env . t ; exp_attributes : attribute list ; } | Texp_constraint of core_type | Texp_coerce of core_type option * core_type | Texp_poly of core_type option | Texp_newtype of string Texp_ident of Path . t * Longident . t loc * Types . value_description | Texp_constant of constant | Texp_let of rec_flag * value_binding list * expression | Texp_function of { arg_label : arg_label ; param : Ident . t ; cases : case list ; partial : partial ; } | Texp_apply of expression * ( arg_label * expression option ) list | Texp_match of expression * case list * partial | Texp_try of expression * case list | Texp_tuple of expression list | Texp_construct of Longident . t loc * constructor_description * expression list | Texp_variant of label * expression option | Texp_record of { fields : ( Types . label_description * record_label_definition ) array ; representation : Types . record_representation ; extended_expression : expression option ; } | Texp_field of expression * Longident . t loc * label_description | Texp_setfield of expression * Longident . t loc * label_description * expression | Texp_array of expression list | Texp_ifthenelse of expression * expression * expression option | Texp_sequence of expression * expression | Texp_while of expression * expression | Texp_for of Ident . t * Parsetree . pattern * expression * expression * direction_flag * expression | Texp_send of expression * meth * expression option | Texp_new of Path . t * Longident . t loc * Types . class_declaration | Texp_instvar of Path . t * Path . t * string loc | Texp_setinstvar of Path . t * Path . t * string loc * expression | Texp_override of Path . t * ( Path . t * string loc * expression ) list | Texp_letmodule of Ident . t option * string option loc * Types . module_presence * module_expr * expression | Texp_letexception of extension_constructor * expression | Texp_assert of expression | Texp_lazy of expression | Texp_object of class_structure * string list | Texp_pack of module_expr | Texp_letop of { let_ : binding_op ; ands : binding_op list ; param : Ident . t ; body : case ; partial : partial ; } | Texp_unreachable | Texp_extension_constructor of Longident . t loc * Path . t | Texp_open of open_declaration * expression Tmeth_name of string | Tmeth_val of Ident . t { c_lhs : pattern ; c_guard : expression option ; c_rhs : expression ; } | Kept of Types . type_expr | Overridden of Longident . t loc * expression { bop_op_path : Path . t ; bop_op_name : string loc ; bop_op_val : Types . value_description ; bop_op_type : Types . type_expr ; bop_exp : expression ; bop_loc : Location . t ; } { cl_desc : class_expr_desc ; cl_loc : Location . t ; cl_type : Types . class_type ; cl_env : Env . t ; cl_attributes : attribute list ; } Tcl_ident of Path . t * Longident . t loc * core_type list | Tcl_structure of class_structure | Tcl_fun of arg_label * pattern * ( Ident . t * expression ) list * class_expr * partial | Tcl_apply of class_expr * ( arg_label * expression option ) list | Tcl_let of rec_flag * value_binding list * ( Ident . t * expression ) list * class_expr | Tcl_constraint of class_expr * class_type option * string list * string list * Concr . t | Tcl_open of open_description * class_expr { cstr_self : pattern ; cstr_fields : class_field list ; cstr_type : Types . class_signature ; cstr_meths : Ident . t Meths . t ; } { cf_desc : class_field_desc ; cf_loc : Location . t ; cf_attributes : attribute list ; } | Tcfk_virtual of core_type | Tcfk_concrete of override_flag * expression Tcf_inherit of override_flag * class_expr * string option * ( string * Ident . t ) list * ( string * Ident . t ) list | Tcf_val of string loc * mutable_flag * Ident . t * class_field_kind * bool | Tcf_method of string loc * private_flag * class_field_kind | Tcf_constraint of core_type * core_type | Tcf_initializer of expression | Tcf_attribute of attribute { mod_desc : module_expr_desc ; mod_loc : Location . t ; mod_type : Types . module_type ; mod_env : Env . t ; mod_attributes : attribute list ; } Tmodtype_implicit | Unit | Named of Ident . t option * string option loc * module_type Tmod_ident of Path . t * Longident . t loc | Tmod_structure of structure | Tmod_functor of functor_parameter * module_expr | Tmod_apply of module_expr * module_expr * module_coercion | Tmod_constraint of module_expr * Types . module_type * module_type_constraint * module_coercion | Tmod_unpack of expression * Types . module_type str_items : structure_item list ; str_type : Types . signature ; str_final_env : Env . t ; } { str_desc : structure_item_desc ; str_loc : Location . t ; str_env : Env . t } Tstr_eval of expression * attributes | Tstr_value of rec_flag * value_binding list | Tstr_primitive of value_description | Tstr_type of rec_flag * type_declaration list | Tstr_typext of type_extension | Tstr_exception of type_exception | Tstr_module of module_binding | Tstr_recmodule of module_binding list | Tstr_modtype of module_type_declaration | Tstr_open of open_declaration | Tstr_class of ( class_declaration * string list ) list | Tstr_class_type of ( Ident . t * string loc * class_type_declaration ) list | Tstr_include of include_declaration | Tstr_attribute of attribute { mb_id : Ident . t option ; mb_name : string option loc ; mb_presence : module_presence ; mb_expr : module_expr ; mb_attributes : attribute list ; mb_loc : Location . t ; } { vb_pat : pattern ; vb_expr : expression ; vb_attributes : attributes ; vb_loc : Location . t ; } Tcoerce_none | Tcoerce_structure of ( int * module_coercion ) list * ( Ident . t * int * module_coercion ) list | Tcoerce_functor of module_coercion * module_coercion | Tcoerce_primitive of primitive_coercion | Tcoerce_alias of Env . t * Path . t * module_coercion { mty_desc : module_type_desc ; mty_type : Types . module_type ; mty_env : Env . t ; mty_loc : Location . t ; mty_attributes : attribute list ; } Tmty_ident of Path . t * Longident . t loc | Tmty_signature of signature | Tmty_functor of functor_parameter * module_type | Tmty_with of module_type * ( Path . t * Longident . t loc * with_constraint ) list | Tmty_typeof of module_expr | Tmty_alias of Path . t * Longident . t loc { pc_desc : Primitive . description ; pc_type : type_expr ; pc_env : Env . t ; pc_loc : Location . t ; } sig_items : signature_item list ; sig_type : Types . signature ; sig_final_env : Env . t ; } { sig_desc : signature_item_desc ; sig_env : Env . t ; sig_loc : Location . t } Tsig_value of value_description | Tsig_type of rec_flag * type_declaration list | Tsig_typesubst of type_declaration list | Tsig_typext of type_extension | Tsig_exception of type_exception | Tsig_module of module_declaration | Tsig_modsubst of module_substitution | Tsig_recmodule of module_declaration list | Tsig_modtype of module_type_declaration | Tsig_open of open_description | Tsig_include of include_description | Tsig_class of class_description list | Tsig_class_type of class_type_declaration list | Tsig_attribute of attribute { md_id : Ident . t option ; md_name : string option loc ; md_presence : module_presence ; md_type : module_type ; md_attributes : attribute list ; md_loc : Location . t ; } { ms_id : Ident . t ; ms_name : string loc ; ms_manifest : Path . t ; ms_txt : Longident . t loc ; ms_attributes : attributes ; ms_loc : Location . t ; } { mtd_id : Ident . t ; mtd_name : string loc ; mtd_type : module_type option ; mtd_attributes : attribute list ; mtd_loc : Location . t ; } { open_expr : ' a ; open_bound_items : Types . signature ; open_override : override_flag ; open_env : Env . t ; open_loc : Location . t ; open_attributes : attribute list ; } { incl_mod : ' a ; incl_type : Types . signature ; incl_loc : Location . t ; incl_attributes : attribute list ; } Twith_type of type_declaration | Twith_module of Path . t * Longident . t loc | Twith_typesubst of type_declaration | Twith_modsubst of Path . t * Longident . t loc { mutable ctyp_desc : core_type_desc ; mutable ctyp_type : type_expr ; ctyp_env : Env . t ; ctyp_loc : Location . t ; ctyp_attributes : attribute list ; } Ttyp_any | Ttyp_var of string | Ttyp_arrow of arg_label * core_type * core_type | Ttyp_tuple of core_type list | Ttyp_constr of Path . t * Longident . t loc * core_type list | Ttyp_object of object_field list * closed_flag | Ttyp_class of Path . t * Longident . t loc * core_type list | Ttyp_alias of core_type * string | Ttyp_variant of row_field list * closed_flag * label list option | Ttyp_poly of string list * core_type | Ttyp_package of package_type pack_path : Path . t ; pack_fields : ( Longident . t loc * core_type ) list ; pack_type : Types . module_type ; pack_txt : Longident . t loc ; } rf_desc : row_field_desc ; rf_loc : Location . t ; rf_attributes : attributes ; } Ttag of string loc * bool * core_type list | Tinherit of core_type of_desc : object_field_desc ; of_loc : Location . t ; of_attributes : attributes ; } | OTtag of string loc * core_type | OTinherit of core_type { val_id : Ident . t ; val_name : string loc ; val_desc : core_type ; val_val : Types . value_description ; val_prim : string list ; val_loc : Location . t ; val_attributes : attribute list ; } { typ_id : Ident . t ; typ_name : string loc ; typ_params : ( core_type * variance ) list ; typ_type : Types . type_declaration ; typ_cstrs : ( core_type * core_type * Location . t ) list ; typ_kind : type_kind ; typ_private : private_flag ; typ_manifest : core_type option ; typ_loc : Location . t ; typ_attributes : attribute list ; } Ttype_abstract | Ttype_variant of constructor_declaration list | Ttype_record of label_declaration list | Ttype_open { ld_id : Ident . t ; ld_name : string loc ; ld_mutable : mutable_flag ; ld_type : core_type ; ld_loc : Location . t ; ld_attributes : attribute list ; } { cd_id : Ident . t ; cd_name : string loc ; cd_args : constructor_arguments ; cd_res : core_type option ; cd_loc : Location . t ; cd_attributes : attribute list ; } | Cstr_tuple of core_type list | Cstr_record of label_declaration list { tyext_path : Path . t ; tyext_txt : Longident . t loc ; tyext_params : ( core_type * variance ) list ; tyext_constructors : extension_constructor list ; tyext_private : private_flag ; tyext_loc : Location . t ; tyext_attributes : attribute list ; } { tyexn_constructor : extension_constructor ; tyexn_loc : Location . t ; tyexn_attributes : attribute list ; } { ext_id : Ident . t ; ext_name : string loc ; ext_type : Types . extension_constructor ; ext_kind : extension_constructor_kind ; ext_loc : Location . t ; ext_attributes : attribute list ; } Text_decl of constructor_arguments * core_type option | Text_rebind of Path . t * Longident . t loc { cltyp_desc : class_type_desc ; cltyp_type : Types . class_type ; cltyp_env : Env . t ; cltyp_loc : Location . t ; cltyp_attributes : attribute list ; } Tcty_constr of Path . t * Longident . t loc * core_type list | Tcty_signature of class_signature | Tcty_arrow of arg_label * core_type * class_type | Tcty_open of open_description * class_type csig_self : core_type ; csig_fields : class_type_field list ; csig_type : Types . class_signature ; } ctf_desc : class_type_field_desc ; ctf_loc : Location . t ; ctf_attributes : attribute list ; } | Tctf_inherit of class_type | Tctf_val of ( string * mutable_flag * virtual_flag * core_type ) | Tctf_method of ( string * private_flag * virtual_flag * core_type ) | Tctf_constraint of ( core_type * core_type ) | Tctf_attribute of attribute class_expr class_infos class_type class_infos class_type class_infos { ci_virt : virtual_flag ; ci_params : ( core_type * variance ) list ; ci_id_name : string loc ; ci_id_class : Ident . t ; ci_id_class_type : Ident . t ; ci_id_object : Ident . t ; ci_id_typehash : Ident . t ; ci_expr : ' a ; ci_decl : Types . class_declaration ; ci_type_decl : Types . class_type_declaration ; ci_loc : Location . t ; ci_attributes : attribute list ; } |
let shallow_iter_pattern_desc f = function | Tpat_alias ( p , _ , _ ) -> f p | Tpat_tuple patl -> List . iter f patl | Tpat_construct ( _ , _ , patl ) -> List . iter f patl | Tpat_variant ( _ , pat , _ ) -> Option . iter f pat | Tpat_record ( lbl_pat_list , _ ) -> List . iter ( fun ( _ , _ , pat ) -> f pat ) lbl_pat_list | Tpat_array patl -> List . iter f patl | Tpat_or ( p1 , p2 , _ ) -> f p1 ; f p2 | Tpat_lazy p -> f p | Tpat_exception p -> f p | Tpat_any | Tpat_var _ | Tpat_constant _ -> ( ) |
let shallow_map_pattern_desc f d = match d with | Tpat_alias ( p1 , id , s ) -> Tpat_alias ( f p1 , id , s ) | Tpat_tuple pats -> Tpat_tuple ( List . map f pats ) | Tpat_record ( lpats , closed ) -> Tpat_record ( List . map ( fun ( lid , l , p ) -> lid , l , f p ) lpats , closed ) | Tpat_construct ( lid , c , pats ) -> Tpat_construct ( lid , c , List . map f pats ) | Tpat_array pats -> Tpat_array ( List . map f pats ) | Tpat_lazy p1 -> Tpat_lazy ( f p1 ) | Tpat_exception p1 -> Tpat_exception ( f p1 ) | Tpat_variant ( x1 , Some p1 , x2 ) -> Tpat_variant ( x1 , Some ( f p1 ) , x2 ) | Tpat_or ( p1 , p2 , path ) -> Tpat_or ( f p1 , f p2 , path ) | Tpat_var _ | Tpat_constant _ | Tpat_any | Tpat_variant ( _ , None , _ ) -> d |
let rec iter_pattern f p = f p ; shallow_iter_pattern_desc ( iter_pattern f ) p . pat_desc |
let exists_pattern f p = let exception Found in let raiser f x = if ( f x ) then raise Found else ( ) in match iter_pattern ( raiser f ) p with | exception Found -> true | ( ) -> false |
let rec iter_bound_idents f pat = match pat . pat_desc with | Tpat_var ( id , s ) -> f ( id , s , pat . pat_type ) | Tpat_alias ( p , id , s ) -> iter_bound_idents f p ; f ( id , s , pat . pat_type ) | Tpat_or ( p1 , _ , _ ) -> iter_bound_idents f p1 | d -> shallow_iter_pattern_desc ( iter_bound_idents f ) d |
let rev_pat_bound_idents_full pat = let idents_full = ref [ ] in let add id_full = idents_full := id_full :: ! idents_full in iter_bound_idents add pat ; ! idents_full |
let rev_only_idents idents_full = List . rev_map ( fun ( id , _ , _ ) -> id ) idents_full |
let pat_bound_idents_full pat = List . rev ( rev_pat_bound_idents_full pat ) |
let pat_bound_idents pat = rev_only_idents ( rev_pat_bound_idents_full pat ) |
let rev_let_bound_idents_full bindings = let idents_full = ref [ ] in let add id_full = idents_full := id_full :: ! idents_full in List . iter ( fun vb -> iter_bound_idents add vb . vb_pat ) bindings ; ! idents_full |
let let_bound_idents_full bindings = List . rev ( rev_let_bound_idents_full bindings ) |
let let_bound_idents pat = rev_only_idents ( rev_let_bound_idents_full pat ) |
let alpha_var env id = List . assoc id env |
let rec alpha_pat env p = match p . pat_desc with { p with pat_desc = try Tpat_var ( alpha_var env id , s ) with | Not_found -> Tpat_any } let new_p = alpha_pat env p1 in begin try { p with pat_desc = Tpat_alias ( new_p , alpha_var env id , s ) } with | Not_found -> new_p end { p with pat_desc = shallow_map_pattern_desc ( alpha_pat env ) d } |
let split_pattern pat = let combine_pattern_desc_opts ~ into p1 p2 = match p1 , p2 with | None , None -> None | Some p , None | None , Some p -> Some p | Some p1 , Some p2 -> Some { into with pat_desc = Tpat_or ( p1 , p2 , None ) } in let rec split_pattern pat = match pat . pat_desc with | Tpat_or ( p1 , p2 , None ) -> let vals1 , exns1 = split_pattern p1 in let vals2 , exns2 = split_pattern p2 in combine_pattern_desc_opts ~ into : pat vals1 vals2 , combine_pattern_desc_opts ~ into : pat exns1 exns2 | Tpat_exception p -> None , Some p | _ -> Some pat , None in split_pattern pat |
module type IteratorArgument = sig val enter_structure : structure -> unit val enter_value_description : value_description -> unit val enter_type_extension : type_extension -> unit val enter_extension_constructor : extension_constructor -> unit val enter_pattern : pattern -> unit val enter_expression : expression -> unit val enter_package_type : package_type -> unit val enter_signature : signature -> unit val enter_signature_item : signature_item -> unit val enter_module_type_declaration : module_type_declaration -> unit val enter_module_type : module_type -> unit val enter_module_expr : module_expr -> unit val enter_with_constraint : with_constraint -> unit val enter_class_expr : class_expr -> unit val enter_class_signature : class_signature -> unit val enter_class_declaration : class_declaration -> unit val enter_class_description : class_description -> unit val enter_class_type_declaration : class_type_declaration -> unit val enter_class_type : class_type -> unit val enter_class_type_field : class_type_field -> unit val enter_core_type : core_type -> unit val enter_class_structure : class_structure -> unit val enter_class_field : class_field -> unit val enter_structure_item : structure_item -> unit val leave_structure : structure -> unit val leave_value_description : value_description -> unit val leave_type_extension : type_extension -> unit val leave_extension_constructor : extension_constructor -> unit val leave_pattern : pattern -> unit val leave_expression : expression -> unit val leave_package_type : package_type -> unit val leave_signature : signature -> unit val leave_signature_item : signature_item -> unit val leave_module_type_declaration : module_type_declaration -> unit val leave_module_type : module_type -> unit val leave_module_expr : module_expr -> unit val leave_with_constraint : with_constraint -> unit val leave_class_expr : class_expr -> unit val leave_class_signature : class_signature -> unit val leave_class_declaration : class_declaration -> unit val leave_class_description : class_description -> unit val leave_class_type_declaration : class_type_declaration -> unit val leave_class_type : class_type -> unit val leave_class_type_field : class_type_field -> unit val leave_core_type : core_type -> unit val leave_class_structure : class_structure -> unit val leave_class_field : class_field -> unit val leave_structure_item : structure_item -> unit val enter_bindings : rec_flag -> unit val enter_binding : value_binding -> unit val leave_binding : value_binding -> unit val leave_bindings : rec_flag -> unit val enter_type_declarations : rec_flag -> unit val enter_type_declaration : type_declaration -> unit val leave_type_declaration : type_declaration -> unit val leave_type_declarations : rec_flag -> unit end |
module MakeIterator ( Iter : IteratorArgument ) : sig val iter_structure : structure -> unit val iter_signature : signature -> unit val iter_structure_item : structure_item -> unit val iter_signature_item : signature_item -> unit val iter_expression : expression -> unit val iter_module_type : module_type -> unit val iter_pattern : pattern -> unit val iter_class_expr : class_expr -> unit end = struct let may_iter f v = match v with None -> ( ) | Some x -> f x let rec iter_structure str = Iter . enter_structure str ; List . iter iter_structure_item str . str_items ; Iter . leave_structure str and iter_binding vb = Iter . enter_binding vb ; iter_pattern vb . vb_pat ; iter_expression vb . vb_expr ; Iter . leave_binding vb and iter_bindings rec_flag list = Iter . enter_bindings rec_flag ; List . iter iter_binding list ; Iter . leave_bindings rec_flag and iter_case { c_lhs ; c_guard ; c_rhs } = iter_pattern c_lhs ; may_iter iter_expression c_guard ; iter_expression c_rhs and iter_cases cases = List . iter iter_case cases and iter_structure_item item = Iter . enter_structure_item item ; begin match item . str_desc with Tstr_eval ( exp , _attrs ) -> iter_expression exp | Tstr_value ( rec_flag , list ) -> iter_bindings rec_flag list | Tstr_primitive vd -> iter_value_description vd | Tstr_type ( rf , list ) -> iter_type_declarations rf list | Tstr_typext tyext -> iter_type_extension tyext | Tstr_exception ext -> iter_extension_constructor ext | Tstr_module x -> iter_module_binding x | Tstr_recmodule list -> List . iter iter_module_binding list | Tstr_modtype mtd -> iter_module_type_declaration mtd | Tstr_open _ -> ( ) | Tstr_class list -> List . iter ( fun ( ci , _ ) -> iter_class_declaration ci ) list | Tstr_class_type list -> List . iter ( fun ( _ , _ , ct ) -> iter_class_type_declaration ct ) list | Tstr_include incl -> iter_module_expr incl . incl_mod | Tstr_attribute _ -> ( ) end ; Iter . leave_structure_item item and iter_module_binding x = iter_module_expr x . mb_expr and iter_value_description v = Iter . enter_value_description v ; iter_core_type v . val_desc ; Iter . leave_value_description v and iter_constructor_arguments = function | Cstr_tuple l -> List . iter iter_core_type l | Cstr_record l -> List . iter ( fun ld -> iter_core_type ld . ld_type ) l and iter_constructor_declaration cd = iter_constructor_arguments cd . cd_args ; option iter_core_type cd . cd_res ; and iter_type_parameter ( ct , _v ) = iter_core_type ct and iter_type_declaration decl = Iter . enter_type_declaration decl ; List . iter iter_type_parameter decl . typ_params ; List . iter ( fun ( ct1 , ct2 , _loc ) -> iter_core_type ct1 ; iter_core_type ct2 ) decl . typ_cstrs ; begin match decl . typ_kind with Ttype_abstract -> ( ) | Ttype_variant list -> List . iter iter_constructor_declaration list | Ttype_record list -> List . iter ( fun ld -> iter_core_type ld . ld_type ) list | Ttype_open -> ( ) end ; option iter_core_type decl . typ_manifest ; Iter . leave_type_declaration decl and iter_type_declarations rec_flag decls = Iter . enter_type_declarations rec_flag ; List . iter iter_type_declaration decls ; Iter . leave_type_declarations rec_flag and iter_extension_constructor ext = Iter . enter_extension_constructor ext ; begin match ext . ext_kind with Text_decl ( args , ret ) -> iter_constructor_arguments args ; option iter_core_type ret | Text_rebind _ -> ( ) end ; Iter . leave_extension_constructor ext ; and iter_type_extension tyext = Iter . enter_type_extension tyext ; List . iter iter_type_parameter tyext . tyext_params ; List . iter iter_extension_constructor tyext . tyext_constructors ; Iter . leave_type_extension tyext and iter_pattern pat = Iter . enter_pattern pat ; List . iter ( fun ( cstr , _ , _attrs ) -> match cstr with | Tpat_type _ -> ( ) | Tpat_unpack -> ( ) | Tpat_open _ -> ( ) | Tpat_constraint ct -> iter_core_type ct ) pat . pat_extra ; begin match pat . pat_desc with Tpat_any -> ( ) | Tpat_var _ -> ( ) | Tpat_alias ( pat1 , _ , _ ) -> iter_pattern pat1 | Tpat_constant _ -> ( ) | Tpat_tuple list -> List . iter iter_pattern list | Tpat_construct ( _ , _ , args ) -> List . iter iter_pattern args | Tpat_variant ( _ , pato , _ ) -> begin match pato with None -> ( ) | Some pat -> iter_pattern pat end | Tpat_record ( list , _closed ) -> List . iter ( fun ( _ , _ , pat ) -> iter_pattern pat ) list | Tpat_array list -> List . iter iter_pattern list | Tpat_or ( p1 , p2 , _ ) -> iter_pattern p1 ; iter_pattern p2 | Tpat_lazy p -> iter_pattern p end ; Iter . leave_pattern pat and option f x = match x with None -> ( ) | Some e -> f e and iter_expression exp = Iter . enter_expression exp ; List . iter ( function ( cstr , _ , _attrs ) -> match cstr with Texp_constraint ct -> iter_core_type ct | Texp_coerce ( cty1 , cty2 ) -> option iter_core_type cty1 ; iter_core_type cty2 | Texp_open _ -> ( ) | Texp_poly cto -> option iter_core_type cto | Texp_newtype _ -> ( ) ) exp . exp_extra ; begin match exp . exp_desc with Texp_ident _ -> ( ) | Texp_constant _ -> ( ) | Texp_let ( rec_flag , list , exp ) -> iter_bindings rec_flag list ; iter_expression exp | Texp_function { cases ; _ } -> iter_cases cases | Texp_apply ( exp , list ) -> iter_expression exp ; List . iter ( fun ( _label , expo ) -> match expo with None -> ( ) | Some exp -> iter_expression exp ) list | Texp_match ( exp , list1 , list2 , _ ) -> iter_expression exp ; iter_cases list1 ; iter_cases list2 ; | Texp_try ( exp , list ) -> iter_expression exp ; iter_cases list | Texp_tuple list -> List . iter iter_expression list | Texp_construct ( _ , _ , args ) -> List . iter iter_expression args | Texp_variant ( _label , expo ) -> begin match expo with None -> ( ) | Some exp -> iter_expression exp end | Texp_record { fields ; extended_expression ; _ } -> Array . iter ( function | _ , Kept _ -> ( ) | _ , Overridden ( _ , exp ) -> iter_expression exp ) fields ; begin match extended_expression with None -> ( ) | Some exp -> iter_expression exp end | Texp_field ( exp , _ , _label ) -> iter_expression exp | Texp_setfield ( exp1 , _ , _label , exp2 ) -> iter_expression exp1 ; iter_expression exp2 | Texp_array list -> List . iter iter_expression list | Texp_ifthenelse ( exp1 , exp2 , expo ) -> iter_expression exp1 ; iter_expression exp2 ; begin match expo with None -> ( ) | Some exp -> iter_expression exp end | Texp_sequence ( exp1 , exp2 ) -> iter_expression exp1 ; iter_expression exp2 | Texp_while ( exp1 , exp2 ) -> iter_expression exp1 ; iter_expression exp2 | Texp_for ( _id , _ , exp1 , exp2 , _dir , exp3 ) -> iter_expression exp1 ; iter_expression exp2 ; iter_expression exp3 | Texp_send ( exp , _meth , expo ) -> iter_expression exp ; begin match expo with None -> ( ) | Some exp -> iter_expression exp end | Texp_new _ -> ( ) | Texp_instvar _ -> ( ) | Texp_setinstvar ( _ , _ , _ , exp ) -> iter_expression exp | Texp_override ( _ , list ) -> List . iter ( fun ( _path , _ , exp ) -> iter_expression exp ) list | Texp_letmodule ( _id , _ , mexpr , exp ) -> iter_module_expr mexpr ; iter_expression exp | Texp_letexception ( cd , exp ) -> iter_extension_constructor cd ; iter_expression exp | Texp_assert exp -> iter_expression exp | Texp_lazy exp -> iter_expression exp | Texp_object ( cl , _ ) -> iter_class_structure cl | Texp_pack ( mexpr ) -> iter_module_expr mexpr | Texp_unreachable -> ( ) | Texp_extension_constructor _ -> ( ) end ; Iter . leave_expression exp ; and iter_package_type pack = Iter . enter_package_type pack ; List . iter ( fun ( _s , ct ) -> iter_core_type ct ) pack . pack_fields ; Iter . leave_package_type pack ; and iter_signature sg = Iter . enter_signature sg ; List . iter iter_signature_item sg . sig_items ; Iter . leave_signature sg ; and iter_signature_item item = Iter . enter_signature_item item ; begin match item . sig_desc with Tsig_value vd -> iter_value_description vd | Tsig_type ( rf , list ) -> iter_type_declarations rf list | Tsig_exception ext -> iter_extension_constructor ext | Tsig_typext tyext -> iter_type_extension tyext | Tsig_module md -> iter_module_type md . md_type | Tsig_recmodule list -> List . iter ( fun md -> iter_module_type md . md_type ) list | Tsig_modtype mtd -> iter_module_type_declaration mtd | Tsig_open _ -> ( ) | Tsig_include incl -> iter_module_type incl . incl_mod | Tsig_class list -> List . iter iter_class_description list | Tsig_class_type list -> List . iter iter_class_type_declaration list | Tsig_attribute _ -> ( ) end ; Iter . leave_signature_item item ; and iter_module_type_declaration mtd = Iter . enter_module_type_declaration mtd ; begin match mtd . mtd_type with | None -> ( ) | Some mtype -> iter_module_type mtype end ; Iter . leave_module_type_declaration mtd and iter_class_declaration cd = Iter . enter_class_declaration cd ; List . iter iter_type_parameter cd . ci_params ; iter_class_expr cd . ci_expr ; Iter . leave_class_declaration cd ; and iter_class_description cd = Iter . enter_class_description cd ; List . iter iter_type_parameter cd . ci_params ; iter_class_type cd . ci_expr ; Iter . leave_class_description cd ; and iter_class_type_declaration cd = Iter . enter_class_type_declaration cd ; List . iter iter_type_parameter cd . ci_params ; iter_class_type cd . ci_expr ; Iter . leave_class_type_declaration cd ; and iter_module_type mty = Iter . enter_module_type mty ; begin match mty . mty_desc with Tmty_ident _ -> ( ) | Tmty_alias _ -> ( ) | Tmty_signature sg -> iter_signature sg | Tmty_functor ( _ , _ , mtype1 , mtype2 ) -> Misc . may iter_module_type mtype1 ; iter_module_type mtype2 | Tmty_with ( mtype , list ) -> iter_module_type mtype ; List . iter ( fun ( _path , _ , withc ) -> iter_with_constraint withc ) list | Tmty_typeof mexpr -> iter_module_expr mexpr end ; Iter . leave_module_type mty ; and iter_with_constraint cstr = Iter . enter_with_constraint cstr ; begin match cstr with Twith_type decl -> iter_type_declaration decl | Twith_module _ -> ( ) | Twith_typesubst decl -> iter_type_declaration decl | Twith_modsubst _ -> ( ) end ; Iter . leave_with_constraint cstr ; and iter_module_expr mexpr = Iter . enter_module_expr mexpr ; begin match mexpr . mod_desc with Tmod_ident _ -> ( ) | Tmod_structure st -> iter_structure st | Tmod_functor ( _ , _ , mtype , mexpr ) -> Misc . may iter_module_type mtype ; iter_module_expr mexpr | Tmod_apply ( mexp1 , mexp2 , _ ) -> iter_module_expr mexp1 ; iter_module_expr mexp2 | Tmod_constraint ( mexpr , _ , Tmodtype_implicit , _ ) -> iter_module_expr mexpr | Tmod_constraint ( mexpr , _ , Tmodtype_explicit mtype , _ ) -> iter_module_expr mexpr ; iter_module_type mtype | Tmod_unpack ( exp , _mty ) -> iter_expression exp end ; Iter . leave_module_expr mexpr ; and iter_class_expr cexpr = Iter . enter_class_expr cexpr ; begin match cexpr . cl_desc with | Tcl_constraint ( cl , None , _ , _ , _ ) -> iter_class_expr cl ; | Tcl_structure clstr -> iter_class_structure clstr | Tcl_fun ( _label , pat , priv , cl , _partial ) -> iter_pattern pat ; List . iter ( fun ( _id , _ , exp ) -> iter_expression exp ) priv ; iter_class_expr cl | Tcl_apply ( cl , args ) -> iter_class_expr cl ; List . iter ( fun ( _label , expo ) -> match expo with None -> ( ) | Some exp -> iter_expression exp ) args | Tcl_let ( rec_flat , bindings , ivars , cl ) -> iter_bindings rec_flat bindings ; List . iter ( fun ( _id , _ , exp ) -> iter_expression exp ) ivars ; iter_class_expr cl | Tcl_constraint ( cl , Some clty , _vals , _meths , _concrs ) -> iter_class_expr cl ; iter_class_type clty | Tcl_ident ( _ , _ , tyl ) -> List . iter iter_core_type tyl | Tcl_open ( _ , _ , _ , _ , e ) -> iter_class_expr e end ; Iter . leave_class_expr cexpr ; and iter_class_type ct = Iter . enter_class_type ct ; begin match ct . cltyp_desc with Tcty_signature csg -> iter_class_signature csg | Tcty_constr ( _path , _ , list ) -> List . iter iter_core_type list | Tcty_arrow ( _label , ct , cl ) -> iter_core_type ct ; iter_class_type cl | Tcty_open ( _ , _ , _ , _ , e ) -> iter_class_type e end ; Iter . leave_class_type ct ; and iter_class_signature cs = Iter . enter_class_signature cs ; iter_core_type cs . csig_self ; List . iter iter_class_type_field cs . csig_fields ; Iter . leave_class_signature cs and iter_class_type_field ctf = Iter . enter_class_type_field ctf ; begin match ctf . ctf_desc with Tctf_inherit ct -> iter_class_type ct | Tctf_val ( _s , _mut , _virt , ct ) -> iter_core_type ct | Tctf_method ( _s , _priv , _virt , ct ) -> iter_core_type ct | Tctf_constraint ( ct1 , ct2 ) -> iter_core_type ct1 ; iter_core_type ct2 | Tctf_attribute _ -> ( ) end ; Iter . leave_class_type_field ctf and iter_core_type ct = Iter . enter_core_type ct ; begin match ct . ctyp_desc with Ttyp_any -> ( ) | Ttyp_var _ -> ( ) | Ttyp_arrow ( _label , ct1 , ct2 ) -> iter_core_type ct1 ; iter_core_type ct2 | Ttyp_tuple list -> List . iter iter_core_type list | Ttyp_constr ( _path , _ , list ) -> List . iter iter_core_type list | Ttyp_object ( list , _o ) -> List . iter iter_object_field list | Ttyp_class ( _path , _ , list ) -> List . iter iter_core_type list | Ttyp_alias ( ct , _s ) -> iter_core_type ct | Ttyp_variant ( list , _bool , _labels ) -> List . iter iter_row_field list | Ttyp_poly ( _list , ct ) -> iter_core_type ct | Ttyp_package pack -> iter_package_type pack end ; Iter . leave_core_type ct and iter_class_structure cs = Iter . enter_class_structure cs ; iter_pattern cs . cstr_self ; List . iter iter_class_field cs . cstr_fields ; Iter . leave_class_structure cs ; and iter_row_field rf = match rf with Ttag ( _label , _attrs , _bool , list ) -> List . iter iter_core_type list | Tinherit ct -> iter_core_type ct and iter_object_field ofield = match ofield with OTtag ( _ , _ , ct ) | OTinherit ct -> iter_core_type ct and iter_class_field cf = Iter . enter_class_field cf ; begin match cf . cf_desc with Tcf_inherit ( _ovf , cl , _super , _vals , _meths ) -> iter_class_expr cl | Tcf_constraint ( cty , cty ' ) -> iter_core_type cty ; iter_core_type cty ' | Tcf_val ( _lab , _ , _ , Tcfk_virtual cty , _ ) -> iter_core_type cty | Tcf_val ( _lab , _ , _ , Tcfk_concrete ( _ , exp ) , _ ) -> iter_expression exp | Tcf_method ( _lab , _ , Tcfk_virtual cty ) -> iter_core_type cty | Tcf_method ( _lab , _ , Tcfk_concrete ( _ , exp ) ) -> iter_expression exp | Tcf_initializer exp -> iter_expression exp | Tcf_attribute _ -> ( ) end ; Iter . leave_class_field cf ; end |
module DefaultIteratorArgument = struct let enter_structure _ = ( ) let enter_value_description _ = ( ) let enter_type_extension _ = ( ) let enter_extension_constructor _ = ( ) let enter_pattern _ = ( ) let enter_expression _ = ( ) let enter_package_type _ = ( ) let enter_signature _ = ( ) let enter_signature_item _ = ( ) let enter_module_type_declaration _ = ( ) let enter_module_type _ = ( ) let enter_module_expr _ = ( ) let enter_with_constraint _ = ( ) let enter_class_expr _ = ( ) let enter_class_signature _ = ( ) let enter_class_declaration _ = ( ) let enter_class_description _ = ( ) let enter_class_type_declaration _ = ( ) let enter_class_type _ = ( ) let enter_class_type_field _ = ( ) let enter_core_type _ = ( ) let enter_class_structure _ = ( ) let enter_class_field _ = ( ) let enter_structure_item _ = ( ) let leave_structure _ = ( ) let leave_value_description _ = ( ) let leave_type_extension _ = ( ) let leave_extension_constructor _ = ( ) let leave_pattern _ = ( ) let leave_expression _ = ( ) let leave_package_type _ = ( ) let leave_signature _ = ( ) let leave_signature_item _ = ( ) let leave_module_type_declaration _ = ( ) let leave_module_type _ = ( ) let leave_module_expr _ = ( ) let leave_with_constraint _ = ( ) let leave_class_expr _ = ( ) let leave_class_signature _ = ( ) let leave_class_declaration _ = ( ) let leave_class_description _ = ( ) let leave_class_type_declaration _ = ( ) let leave_class_type _ = ( ) let leave_class_type_field _ = ( ) let leave_core_type _ = ( ) let leave_class_structure _ = ( ) let leave_class_field _ = ( ) let leave_structure_item _ = ( ) let enter_binding _ = ( ) let leave_binding _ = ( ) let enter_bindings _ = ( ) let leave_bindings _ = ( ) let enter_type_declaration _ = ( ) let leave_type_declaration _ = ( ) let enter_type_declarations _ = ( ) let leave_type_declarations _ = ( ) end |
module type MapArgument = sig val enter_structure : structure -> structure val enter_value_description : value_description -> value_description val enter_type_declaration : type_declaration -> type_declaration val enter_type_extension : type_extension -> type_extension val enter_extension_constructor : extension_constructor -> extension_constructor val enter_pattern : pattern -> pattern val enter_expression : expression -> expression val enter_package_type : package_type -> package_type val enter_signature : signature -> signature val enter_signature_item : signature_item -> signature_item val enter_module_type_declaration : module_type_declaration -> module_type_declaration val enter_module_type : module_type -> module_type val enter_module_expr : module_expr -> module_expr val enter_with_constraint : with_constraint -> with_constraint val enter_class_expr : class_expr -> class_expr val enter_class_signature : class_signature -> class_signature val enter_class_declaration : class_declaration -> class_declaration val enter_class_description : class_description -> class_description val enter_class_type_declaration : class_type_declaration -> class_type_declaration val enter_class_type : class_type -> class_type val enter_class_type_field : class_type_field -> class_type_field val enter_core_type : core_type -> core_type val enter_class_structure : class_structure -> class_structure val enter_class_field : class_field -> class_field val enter_structure_item : structure_item -> structure_item val leave_structure : structure -> structure val leave_value_description : value_description -> value_description val leave_type_declaration : type_declaration -> type_declaration val leave_type_extension : type_extension -> type_extension val leave_extension_constructor : extension_constructor -> extension_constructor val leave_pattern : pattern -> pattern val leave_expression : expression -> expression val leave_package_type : package_type -> package_type val leave_signature : signature -> signature val leave_signature_item : signature_item -> signature_item val leave_module_type_declaration : module_type_declaration -> module_type_declaration val leave_module_type : module_type -> module_type val leave_module_expr : module_expr -> module_expr val leave_with_constraint : with_constraint -> with_constraint val leave_class_expr : class_expr -> class_expr val leave_class_signature : class_signature -> class_signature val leave_class_declaration : class_declaration -> class_declaration val leave_class_description : class_description -> class_description val leave_class_type_declaration : class_type_declaration -> class_type_declaration val leave_class_type : class_type -> class_type val leave_class_type_field : class_type_field -> class_type_field val leave_core_type : core_type -> core_type val leave_class_structure : class_structure -> class_structure val leave_class_field : class_field -> class_field val leave_structure_item : structure_item -> structure_item end |
module MakeMap ( Map : MapArgument ) = struct open Misc let rec map_structure str = let str = Map . enter_structure str in let str_items = List . map map_structure_item str . str_items in Map . leave_structure { str with str_items = str_items } and map_binding vb = { vb_pat = map_pattern vb . vb_pat ; vb_expr = map_expression vb . vb_expr ; vb_attributes = vb . vb_attributes ; vb_loc = vb . vb_loc ; } and map_bindings list = List . map map_binding list and map_case { c_lhs ; c_guard ; c_rhs } = { c_lhs = map_pattern c_lhs ; c_guard = may_map map_expression c_guard ; c_rhs = map_expression c_rhs ; } and map_cases list = List . map map_case list and map_structure_item item = let item = Map . enter_structure_item item in let str_desc = match item . str_desc with Tstr_eval ( exp , attrs ) -> Tstr_eval ( map_expression exp , attrs ) | Tstr_value ( rec_flag , list ) -> Tstr_value ( rec_flag , map_bindings list ) | Tstr_primitive vd -> Tstr_primitive ( map_value_description vd ) | Tstr_type ( rf , list ) -> Tstr_type ( rf , List . map map_type_declaration list ) | Tstr_typext tyext -> Tstr_typext ( map_type_extension tyext ) | Tstr_exception ext -> Tstr_exception ( map_extension_constructor ext ) | Tstr_module x -> Tstr_module ( map_module_binding x ) | Tstr_recmodule list -> let list = List . map map_module_binding list in Tstr_recmodule list | Tstr_modtype mtd -> Tstr_modtype ( map_module_type_declaration mtd ) | Tstr_open od -> Tstr_open od | Tstr_class list -> let list = List . map ( fun ( ci , string_list ) -> map_class_declaration ci , string_list ) list in Tstr_class list | Tstr_class_type list -> let list = List . map ( fun ( id , name , ct ) -> id , name , map_class_type_declaration ct ) list in Tstr_class_type list | Tstr_include incl -> Tstr_include { incl with incl_mod = map_module_expr incl . incl_mod } | Tstr_attribute x -> Tstr_attribute x in Map . leave_structure_item { item with str_desc = str_desc } and map_module_binding x = { x with mb_expr = map_module_expr x . mb_expr } and map_value_description v = let v = Map . enter_value_description v in let val_desc = map_core_type v . val_desc in Map . leave_value_description { v with val_desc = val_desc } and map_type_declaration decl = let decl = Map . enter_type_declaration decl in let typ_params = List . map map_type_parameter decl . typ_params in let typ_cstrs = List . map ( fun ( ct1 , ct2 , loc ) -> ( map_core_type ct1 , map_core_type ct2 , loc ) ) decl . typ_cstrs in let typ_kind = match decl . typ_kind with Ttype_abstract -> Ttype_abstract | Ttype_variant list -> let list = List . map map_constructor_declaration list in Ttype_variant list | Ttype_record list -> let list = List . map ( fun ld -> { ld with ld_type = map_core_type ld . ld_type } ) list in Ttype_record list | Ttype_open -> Ttype_open in let typ_manifest = may_map map_core_type decl . typ_manifest in Map . leave_type_declaration { decl with typ_params = typ_params ; typ_cstrs = typ_cstrs ; typ_kind = typ_kind ; typ_manifest = typ_manifest } and map_type_parameter ( ct , v ) = ( map_core_type ct , v ) and map_constructor_arguments = function | Cstr_tuple l -> Cstr_tuple ( List . map map_core_type l ) | Cstr_record l -> Cstr_record ( List . map ( fun ld -> { ld with ld_type = map_core_type ld . ld_type } ) l ) and map_constructor_declaration cd = let cd_args = map_constructor_arguments cd . cd_args in { cd with cd_args ; cd_res = may_map map_core_type cd . cd_res } and map_type_extension tyext = let tyext = Map . enter_type_extension tyext in let tyext_params = List . map map_type_parameter tyext . tyext_params in let tyext_constructors = List . map map_extension_constructor tyext . tyext_constructors in Map . leave_type_extension { tyext with tyext_params = tyext_params ; tyext_constructors = tyext_constructors } and map_extension_constructor ext = let ext = Map . enter_extension_constructor ext in let ext_kind = match ext . ext_kind with Text_decl ( args , ret ) -> let args = map_constructor_arguments args in let ret = may_map map_core_type ret in Text_decl ( args , ret ) | Text_rebind ( p , lid ) -> Text_rebind ( p , lid ) in Map . leave_extension_constructor { ext with ext_kind = ext_kind } and map_pattern pat = let pat = Map . enter_pattern pat in let pat_desc = match pat . pat_desc with | Tpat_alias ( pat1 , p , text ) -> let pat1 = map_pattern pat1 in Tpat_alias ( pat1 , p , text ) | Tpat_tuple list -> Tpat_tuple ( List . map map_pattern list ) | Tpat_construct ( lid , cstr_decl , args ) -> Tpat_construct ( lid , cstr_decl , List . map map_pattern args ) | Tpat_variant ( label , pato , rowo ) -> let pato = match pato with None -> pato | Some pat -> Some ( map_pattern pat ) in Tpat_variant ( label , pato , rowo ) | Tpat_record ( list , closed ) -> Tpat_record ( List . map ( fun ( lid , lab_desc , pat ) -> ( lid , lab_desc , map_pattern pat ) ) list , closed ) | Tpat_array list -> Tpat_array ( List . map map_pattern list ) | Tpat_or ( p1 , p2 , rowo ) -> Tpat_or ( map_pattern p1 , map_pattern p2 , rowo ) | Tpat_lazy p -> Tpat_lazy ( map_pattern p ) | Tpat_constant _ | Tpat_any | Tpat_var _ -> pat . pat_desc in let pat_extra = List . map map_pat_extra pat . pat_extra in Map . leave_pattern { pat with pat_desc = pat_desc ; pat_extra = pat_extra } and map_pat_extra pat_extra = match pat_extra with | Tpat_constraint ct , loc , attrs -> ( Tpat_constraint ( map_core_type ct ) , loc , attrs ) | ( Tpat_type _ | Tpat_unpack | Tpat_open _ ) , _ , _ -> pat_extra and map_expression exp = let exp = Map . enter_expression exp in let exp_desc = match exp . exp_desc with Texp_ident ( _ , _ , _ ) | Texp_constant _ -> exp . exp_desc | Texp_let ( rec_flag , list , exp ) -> Texp_let ( rec_flag , map_bindings list , map_expression exp ) | Texp_function { arg_label ; param ; cases ; partial ; } -> Texp_function { arg_label ; param ; cases = map_cases cases ; partial ; } | Texp_apply ( exp , list ) -> Texp_apply ( map_expression exp , List . map ( fun ( label , expo ) -> let expo = match expo with None -> expo | Some exp -> Some ( map_expression exp ) in ( label , expo ) ) list ) | Texp_match ( exp , list1 , list2 , partial ) -> Texp_match ( map_expression exp , map_cases list1 , map_cases list2 , partial ) | Texp_try ( exp , list ) -> Texp_try ( map_expression exp , map_cases list ) | Texp_tuple list -> Texp_tuple ( List . map map_expression list ) | Texp_construct ( lid , cstr_desc , args ) -> Texp_construct ( lid , cstr_desc , List . map map_expression args ) | Texp_variant ( label , expo ) -> let expo = match expo with None -> expo | Some exp -> Some ( map_expression exp ) in Texp_variant ( label , expo ) | Texp_record { fields ; representation ; extended_expression } -> let fields = Array . map ( function | label , Kept t -> label , Kept t | label , Overridden ( lid , exp ) -> label , Overridden ( lid , map_expression exp ) ) fields in let extended_expression = match extended_expression with None -> extended_expression | Some exp -> Some ( map_expression exp ) in Texp_record { fields ; representation ; extended_expression } | Texp_field ( exp , lid , label ) -> Texp_field ( map_expression exp , lid , label ) | Texp_setfield ( exp1 , lid , label , exp2 ) -> Texp_setfield ( map_expression exp1 , lid , label , map_expression exp2 ) | Texp_array list -> Texp_array ( List . map map_expression list ) | Texp_ifthenelse ( exp1 , exp2 , expo ) -> Texp_ifthenelse ( map_expression exp1 , map_expression exp2 , match expo with None -> expo | Some exp -> Some ( map_expression exp ) ) | Texp_sequence ( exp1 , exp2 ) -> Texp_sequence ( map_expression exp1 , map_expression exp2 ) | Texp_while ( exp1 , exp2 ) -> Texp_while ( map_expression exp1 , map_expression exp2 ) | Texp_for ( id , name , exp1 , exp2 , dir , exp3 ) -> Texp_for ( id , name , map_expression exp1 , map_expression exp2 , dir , map_expression exp3 ) | Texp_send ( exp , meth , expo ) -> Texp_send ( map_expression exp , meth , may_map map_expression expo ) | Texp_new _ -> exp . exp_desc | Texp_instvar _ -> exp . exp_desc | Texp_setinstvar ( path , lid , path2 , exp ) -> Texp_setinstvar ( path , lid , path2 , map_expression exp ) | Texp_override ( path , list ) -> Texp_override ( path , List . map ( fun ( path , lid , exp ) -> ( path , lid , map_expression exp ) ) list ) | Texp_letmodule ( id , name , mexpr , exp ) -> Texp_letmodule ( id , name , map_module_expr mexpr , map_expression exp ) | Texp_letexception ( cd , exp ) -> Texp_letexception ( map_extension_constructor cd , map_expression exp ) | Texp_assert exp -> Texp_assert ( map_expression exp ) | Texp_lazy exp -> Texp_lazy ( map_expression exp ) | Texp_object ( cl , string_list ) -> Texp_object ( map_class_structure cl , string_list ) | Texp_pack ( mexpr ) -> Texp_pack ( map_module_expr mexpr ) | Texp_unreachable -> Texp_unreachable | Texp_extension_constructor _ as e -> e in let exp_extra = List . map map_exp_extra exp . exp_extra in Map . leave_expression { exp with exp_desc = exp_desc ; exp_extra = exp_extra ; } and map_exp_extra ( ( desc , loc , attrs ) as exp_extra ) = match desc with | Texp_constraint ct -> Texp_constraint ( map_core_type ct ) , loc , attrs | Texp_coerce ( None , ct ) -> Texp_coerce ( None , map_core_type ct ) , loc , attrs | Texp_coerce ( Some ct1 , ct2 ) -> Texp_coerce ( Some ( map_core_type ct1 ) , map_core_type ct2 ) , loc , attrs | Texp_poly ( Some ct ) -> Texp_poly ( Some ( map_core_type ct ) ) , loc , attrs | Texp_newtype _ | Texp_open _ | Texp_poly None -> exp_extra and map_package_type pack = let pack = Map . enter_package_type pack in let pack_fields = List . map ( fun ( s , ct ) -> ( s , map_core_type ct ) ) pack . pack_fields in Map . leave_package_type { pack with pack_fields = pack_fields } and map_signature sg = let sg = Map . enter_signature sg in let sig_items = List . map map_signature_item sg . sig_items in Map . leave_signature { sg with sig_items = sig_items } and map_signature_item item = let item = Map . enter_signature_item item in let sig_desc = match item . sig_desc with Tsig_value vd -> Tsig_value ( map_value_description vd ) | Tsig_type ( rf , list ) -> Tsig_type ( rf , List . map map_type_declaration list ) | Tsig_typext tyext -> Tsig_typext ( map_type_extension tyext ) | Tsig_exception ext -> Tsig_exception ( map_extension_constructor ext ) | Tsig_module md -> Tsig_module { md with md_type = map_module_type md . md_type } | Tsig_recmodule list -> Tsig_recmodule ( List . map ( fun md -> { md with md_type = map_module_type md . md_type } ) list ) | Tsig_modtype mtd -> Tsig_modtype ( map_module_type_declaration mtd ) | Tsig_open _ -> item . sig_desc | Tsig_include incl -> Tsig_include { incl with incl_mod = map_module_type incl . incl_mod } | Tsig_class list -> Tsig_class ( List . map map_class_description list ) | Tsig_class_type list -> Tsig_class_type ( List . map map_class_type_declaration list ) | Tsig_attribute _ as x -> x in Map . leave_signature_item { item with sig_desc = sig_desc } and map_module_type_declaration mtd = let mtd = Map . enter_module_type_declaration mtd in let mtd = { mtd with mtd_type = may_map map_module_type mtd . mtd_type } in Map . leave_module_type_declaration mtd and map_class_declaration cd = let cd = Map . enter_class_declaration cd in let ci_params = List . map map_type_parameter cd . ci_params in let ci_expr = map_class_expr cd . ci_expr in Map . leave_class_declaration { cd with ci_params = ci_params ; ci_expr = ci_expr } and map_class_description cd = let cd = Map . enter_class_description cd in let ci_params = List . map map_type_parameter cd . ci_params in let ci_expr = map_class_type cd . ci_expr in Map . leave_class_description { cd with ci_params = ci_params ; ci_expr = ci_expr } and map_class_type_declaration cd = let cd = Map . enter_class_type_declaration cd in let ci_params = List . map map_type_parameter cd . ci_params in let ci_expr = map_class_type cd . ci_expr in Map . leave_class_type_declaration { cd with ci_params = ci_params ; ci_expr = ci_expr } and map_module_type mty = let mty = Map . enter_module_type mty in let mty_desc = match mty . mty_desc with Tmty_ident _ -> mty . mty_desc | Tmty_alias _ -> mty . mty_desc | Tmty_signature sg -> Tmty_signature ( map_signature sg ) | Tmty_functor ( id , name , mtype1 , mtype2 ) -> Tmty_functor ( id , name , Misc . may_map map_module_type mtype1 , map_module_type mtype2 ) | Tmty_with ( mtype , list ) -> Tmty_with ( map_module_type mtype , List . map ( fun ( path , lid , withc ) -> ( path , lid , map_with_constraint withc ) ) list ) | Tmty_typeof mexpr -> Tmty_typeof ( map_module_expr mexpr ) in Map . leave_module_type { mty with mty_desc = mty_desc } and map_with_constraint cstr = let cstr = Map . enter_with_constraint cstr in let cstr = match cstr with Twith_type decl -> Twith_type ( map_type_declaration decl ) | Twith_typesubst decl -> Twith_typesubst ( map_type_declaration decl ) | Twith_module _ -> cstr | Twith_modsubst _ -> cstr in Map . leave_with_constraint cstr and map_module_expr mexpr = let mexpr = Map . enter_module_expr mexpr in let mod_desc = match mexpr . mod_desc with Tmod_ident _ -> mexpr . mod_desc | Tmod_structure st -> Tmod_structure ( map_structure st ) | Tmod_functor ( id , name , mtype , mexpr ) -> Tmod_functor ( id , name , Misc . may_map map_module_type mtype , map_module_expr mexpr ) | Tmod_apply ( mexp1 , mexp2 , coercion ) -> Tmod_apply ( map_module_expr mexp1 , map_module_expr mexp2 , coercion ) | Tmod_constraint ( mexpr , mod_type , Tmodtype_implicit , coercion ) -> Tmod_constraint ( map_module_expr mexpr , mod_type , Tmodtype_implicit , coercion ) | Tmod_constraint ( mexpr , mod_type , Tmodtype_explicit mtype , coercion ) -> Tmod_constraint ( map_module_expr mexpr , mod_type , Tmodtype_explicit ( map_module_type mtype ) , coercion ) | Tmod_unpack ( exp , mod_type ) -> Tmod_unpack ( map_expression exp , mod_type ) in Map . leave_module_expr { mexpr with mod_desc = mod_desc } and map_class_expr cexpr = let cexpr = Map . enter_class_expr cexpr in let cl_desc = match cexpr . cl_desc with | Tcl_constraint ( cl , None , string_list1 , string_list2 , concr ) -> Tcl_constraint ( map_class_expr cl , None , string_list1 , string_list2 , concr ) | Tcl_structure clstr -> Tcl_structure ( map_class_structure clstr ) | Tcl_fun ( label , pat , priv , cl , partial ) -> Tcl_fun ( label , map_pattern pat , List . map ( fun ( id , name , exp ) -> ( id , name , map_expression exp ) ) priv , map_class_expr cl , partial ) | Tcl_apply ( cl , args ) -> Tcl_apply ( map_class_expr cl , List . map ( fun ( label , expo ) -> ( label , may_map map_expression expo ) ) args ) | Tcl_let ( rec_flag , bindings , ivars , cl ) -> Tcl_let ( rec_flag , map_bindings bindings , List . map ( fun ( id , name , exp ) -> ( id , name , map_expression exp ) ) ivars , map_class_expr cl ) | Tcl_constraint ( cl , Some clty , vals , meths , concrs ) -> Tcl_constraint ( map_class_expr cl , Some ( map_class_type clty ) , vals , meths , concrs ) | Tcl_ident ( id , name , tyl ) -> Tcl_ident ( id , name , List . map map_core_type tyl ) | Tcl_open ( ovf , p , lid , env , e ) -> Tcl_open ( ovf , p , lid , env , map_class_expr e ) in Map . leave_class_expr { cexpr with cl_desc = cl_desc } and map_class_type ct = let ct = Map . enter_class_type ct in let cltyp_desc = match ct . cltyp_desc with Tcty_signature csg -> Tcty_signature ( map_class_signature csg ) | Tcty_constr ( path , lid , list ) -> Tcty_constr ( path , lid , List . map map_core_type list ) | Tcty_arrow ( label , ct , cl ) -> Tcty_arrow ( label , map_core_type ct , map_class_type cl ) | Tcty_open ( ovf , p , lid , env , e ) -> Tcty_open ( ovf , p , lid , env , map_class_type e ) in Map . leave_class_type { ct with cltyp_desc = cltyp_desc } and map_class_signature cs = let cs = Map . enter_class_signature cs in let csig_self = map_core_type cs . csig_self in let csig_fields = List . map map_class_type_field cs . csig_fields in Map . leave_class_signature { cs with csig_self = csig_self ; csig_fields = csig_fields } and map_class_type_field ctf = let ctf = Map . enter_class_type_field ctf in let ctf_desc = match ctf . ctf_desc with Tctf_inherit ct -> Tctf_inherit ( map_class_type ct ) | Tctf_val ( s , mut , virt , ct ) -> Tctf_val ( s , mut , virt , map_core_type ct ) | Tctf_method ( s , priv , virt , ct ) -> Tctf_method ( s , priv , virt , map_core_type ct ) | Tctf_constraint ( ct1 , ct2 ) -> Tctf_constraint ( map_core_type ct1 , map_core_type ct2 ) | Tctf_attribute _ as x -> x in Map . leave_class_type_field { ctf with ctf_desc = ctf_desc } and map_core_type ct = let ct = Map . enter_core_type ct in let ctyp_desc = match ct . ctyp_desc with Ttyp_any | Ttyp_var _ -> ct . ctyp_desc | Ttyp_arrow ( label , ct1 , ct2 ) -> Ttyp_arrow ( label , map_core_type ct1 , map_core_type ct2 ) | Ttyp_tuple list -> Ttyp_tuple ( List . map map_core_type list ) | Ttyp_constr ( path , lid , list ) -> Ttyp_constr ( path , lid , List . map map_core_type list ) | Ttyp_object ( list , o ) -> Ttyp_object ( List . map map_object_field list , o ) | Ttyp_class ( path , lid , list ) -> Ttyp_class ( path , lid , List . map map_core_type list ) | Ttyp_alias ( ct , s ) -> Ttyp_alias ( map_core_type ct , s ) | Ttyp_variant ( list , bool , labels ) -> Ttyp_variant ( List . map map_row_field list , bool , labels ) | Ttyp_poly ( list , ct ) -> Ttyp_poly ( list , map_core_type ct ) | Ttyp_package pack -> Ttyp_package ( map_package_type pack ) in Map . leave_core_type { ct with ctyp_desc = ctyp_desc } and map_class_structure cs = let cs = Map . enter_class_structure cs in let cstr_self = map_pattern cs . cstr_self in let cstr_fields = List . map map_class_field cs . cstr_fields in Map . leave_class_structure { cs with cstr_self ; cstr_fields } and map_row_field rf = match rf with Ttag ( label , attrs , bool , list ) -> Ttag ( label , attrs , bool , List . map map_core_type list ) | Tinherit ct -> Tinherit ( map_core_type ct ) and map_object_field ofield = match ofield with OTtag ( label , attrs , ct ) -> OTtag ( label , attrs , map_core_type ct ) | OTinherit ct -> OTinherit ( map_core_type ct ) and map_class_field cf = let cf = Map . enter_class_field cf in let cf_desc = match cf . cf_desc with Tcf_inherit ( ovf , cl , super , vals , meths ) -> Tcf_inherit ( ovf , map_class_expr cl , super , vals , meths ) | Tcf_constraint ( cty , cty ' ) -> Tcf_constraint ( map_core_type cty , map_core_type cty ' ) | Tcf_val ( lab , mut , ident , Tcfk_virtual cty , b ) -> Tcf_val ( lab , mut , ident , Tcfk_virtual ( map_core_type cty ) , b ) | Tcf_val ( lab , mut , ident , Tcfk_concrete ( o , exp ) , b ) -> Tcf_val ( lab , mut , ident , Tcfk_concrete ( o , map_expression exp ) , b ) | Tcf_method ( lab , priv , Tcfk_virtual cty ) -> Tcf_method ( lab , priv , Tcfk_virtual ( map_core_type cty ) ) | Tcf_method ( lab , priv , Tcfk_concrete ( o , exp ) ) -> Tcf_method ( lab , priv , Tcfk_concrete ( o , map_expression exp ) ) | Tcf_initializer exp -> Tcf_initializer ( map_expression exp ) | Tcf_attribute _ as x -> x in Map . leave_class_field { cf with cf_desc = cf_desc } end |
module DefaultMapArgument = struct let enter_structure t = t let enter_value_description t = t let enter_type_declaration t = t let enter_type_extension t = t let enter_extension_constructor t = t let enter_pattern t = t let enter_expression t = t let enter_package_type t = t let enter_signature t = t let enter_signature_item t = t let enter_module_type_declaration t = t let enter_module_type t = t let enter_module_expr t = t let enter_with_constraint t = t let enter_class_expr t = t let enter_class_signature t = t let enter_class_declaration t = t let enter_class_description t = t let enter_class_type_declaration t = t let enter_class_type t = t let enter_class_type_field t = t let enter_core_type t = t let enter_class_structure t = t let enter_class_field t = t let enter_structure_item t = t let leave_structure t = t let leave_value_description t = t let leave_type_declaration t = t let leave_type_extension t = t let leave_extension_constructor t = t let leave_pattern t = t let leave_expression t = t let leave_package_type t = t let leave_signature t = t let leave_signature_item t = t let leave_module_type_declaration t = t let leave_module_type t = t let leave_module_expr t = t let leave_with_constraint t = t let leave_class_expr t = t let leave_class_signature t = t let leave_class_declaration t = t let leave_class_description t = t let leave_class_type_declaration t = t let leave_class_type t = t let leave_class_type_field t = t let leave_core_type t = t let leave_class_structure t = t let leave_class_field t = t let leave_structure_item t = t end |
let { Logger . log } = Logger . for_section " typedtrie " |
module StampMap = Map . Make ( struct type t = int let compare ( x : int ) ( y : int ) = compare x y end ) |
module Trie : sig [ @@@ ocaml . warning " - 30 " ] type t and elt = { loc : Location . t ; doc : string option ; namespace : Namespaced_path . Namespace . t ; node : node } and node = | Leaf | Internal of t Lazy . t | Included of include_ | Alias of Namespaced_path . t | Functor of functor_parameter * node | Apply of functor_application and include_ = | Named of Namespaced_path . t | Items of t Lazy . t | Apply of functor_application and functor_parameter = ( Ident . t option * Location . t * node ) option and functor_application = { funct : Location . t * functor_ ; arg : Location . t * node } and functor_ = | Apply of functor_application | Funct of functor_parameter * node | Named of Namespaced_path . t | Unpack val empty : t val add : Ident . t -> elt -> t -> t val singleton : Ident . t -> elt -> t val iter : ( name : string -> stamp : int -> elt -> unit ) -> t -> unit val get : Namespaced_path . Id . t -> t -> elt list val find_some : ( string -> int -> elt -> bool ) -> t -> ( string * int * elt ) option [ @@@ ocaml . warning " - 30 " ] type t = elt StampMap . t String . Map . t and elt = { loc : Location . t ; doc : string option ; namespace : Namespaced_path . Namespace . t ; node : node } and node = | Leaf | Internal of t Lazy . t | Included of include_ | Alias of Namespaced_path . t | Functor of functor_parameter * node | Apply of functor_application and include_ = | Named of Namespaced_path . t | Items of t Lazy . t | Apply of functor_application and functor_parameter = ( Ident . t option * Location . t * node ) option and functor_application = { funct : Location . t * functor_ ; arg : Location . t * node } and functor_ = | Apply of functor_application | Funct of functor_parameter * node | Named of Namespaced_path . t | Unpack let empty = String . Map . empty let add id elt t = let key = Ident . name id in match String . Map . find key t with | exception Not_found -> String . Map . add ~ key ~ data ( : StampMap . singleton ( Ident . stamp id ) elt ) t | stamp_map -> String . Map . add ( String . Map . remove key t ) ~ key ~ data ( : StampMap . add ( Ident . stamp id ) elt stamp_map ) let singleton id node = add id node empty let iter f t = String . Map . iter t ~ f ( : fun ~ key : name ~ data -> StampMap . iter ( fun stamp elt -> f ~ name ~ stamp elt ) data ) let get ( k : Namespaced_path . Id . t ) t = match k with | Id id -> [ StampMap . find ( Ident . stamp id ) ( String . Map . find ( Ident . name id ) t ) ] | String s -> List . map ( StampMap . bindings ( String . Map . find s t ) ) ~ f : snd exception Found of string * int * elt let find f ( t : t ) = try iter ( fun ~ name ~ stamp data -> if f name stamp data then raise ( Found ( name , stamp , data ) ) ) t ; raise Not_found with | Found ( name , stamp , data ) -> name , stamp , data let find_some f t = try Some ( find f t ) with Not_found -> None end |
let extract_doc ( attrs : Parsetree . attributes ) = String . concat ~ sep " :\ n " ( List . filter_map attrs ~ f ( : fun attr -> Option . map ~ f : fst ( Type_utils . read_doc_attributes [ attr ] ) ) ) |
let remove_top_indir = List . concat_map ~ f ( : fun bt -> match bt . t_node with | Signature _ | Structure _ -> Lazy . force bt . t_children | _ -> [ bt ] ) |
let of_structure s = let env , node = Mbrowse . leaf_node ( Mbrowse . of_structure s ) in Browse_tree . of_node ~ env node |
let of_signature s = let env , node = Mbrowse . leaf_node ( Mbrowse . of_signature s ) in Browse_tree . of_node ~ env node |
let remove_indir_me me = match me . Typedtree . mod_desc with | Typedtree . Tmod_ident ( path , _ ) -> ` Alias path | Typedtree . Tmod_structure str -> ` Str str | Typedtree . Tmod_functor _ -> let ( fp , me ) = Typedtree . unpack_functor_me me in ` Functor ( fp , ` Mod_expr me ) | Typedtree . Tmod_apply ( me1 , me2 , _ ) -> ` Apply ( me1 , me2 ) | Typedtree . Tmod_constraint ( me , _ , _ , _ ) -> ` Mod_expr me | Typedtree . Tmod_unpack _ -> ` Unpack |
let remove_indir_mty mty = match mty . Typedtree . mty_desc with | Typedtree . Tmty_alias ( path , _ ) -> ` Alias path | Typedtree . Tmty_ident ( path , _ ) -> ` Ident path | Typedtree . Tmty_signature sg -> ` Sg sg | Typedtree . Tmty_functor _ -> let ( fp , mty ) = Typedtree . unpack_functor_mty mty in ` Functor ( fp , ` Mod_type mty ) | Typedtree . Tmty_with ( mty , _ ) -> ` Mod_type mty | Typedtree . Tmty_typeof me -> ` Mod_expr me |
let sig_item_idns item = let open Types in let ns = match item with | Sig_value _ -> ` Vals | Sig_type _ -> ` Type | Sig_typext _ -> ` Type | Sig_module _ -> ` Mod | Sig_modtype _ -> ` Modtype | Sig_class _ -> ` Vals | Sig_class_type _ -> ` Type in signature_item_id item , ns |
let include_idents l = List . map ~ f : sig_item_idns l |
let identify_str_includes item = match item . Typedtree . str_desc with | Typedtree . Tstr_include { Typedtree . incl_type ; incl_mod ; _ } -> ` Included ( include_idents incl_type , ` Mod_expr incl_mod ) | _ -> ` Not_included |
let identify_sig_includes item = match item . Typedtree . sig_desc with | Typedtree . Tsig_include { Typedtree . incl_type ; incl_mod ; _ } -> ` Included ( include_idents incl_type , ` Mod_type incl_mod ) | _ -> ` Not_included |
let rec build ~ local_buffer ~ trie browses : t = let rec node_for_direct_mod namespace : _ -> Trie . node = function | ` Alias path -> Alias ( Namespaced_path . of_path ~ namespace path ) | ` Ident path -> Alias ( Namespaced_path . of_path ~ namespace ` : Modtype path ) | ` Str s -> Internal ( lazy ( build ~ local_buffer ~ trie : Trie . empty [ of_structure s ] ) ) | ` Sg s -> Internal ( lazy ( build ~ local_buffer ~ trie : Trie . empty [ of_signature s ] ) ) | ` Mod_expr me -> node_for_direct_mod ` Mod ( remove_indir_me me ) | ` Mod_type mty -> node_for_direct_mod ` Modtype ( remove_indir_mty mty ) | ` Functor ( fp , packed ) -> let param = match fp with | Typedtree . Unit -> None | Typedtree . Named ( id , loc , mty ) -> let mty = if local_buffer then node_for_direct_mod ` Modtype ( remove_indir_mty mty ) else Trie . Leaf in Some ( id , loc . Location . loc , mty ) in Functor ( param , node_for_direct_mod ` Mod packed ) | ` Apply ( funct , arg ) -> let funct = funct . Typedtree . mod_loc , functor_ ( remove_indir_me funct ) in let arg = arg . Typedtree . mod_loc , match remove_indir_me arg with | ` Str { str_items = [ ] ; _ } -> Trie . Leaf | otherwise -> node_for_direct_mod ` Mod otherwise in Apply { funct ; arg } | ` Unpack -> Leaf and functor_ : _ -> Trie . functor_ = function | ` Alias path | ` Ident path -> Named ( Namespaced_path . of_path ~ namespace ` : Mod path ) | ` Str _ | ` Sg _ -> assert false | ` Mod_expr me -> functor_ ( remove_indir_me me ) | ` Mod_type _ -> assert false | ` Functor ( fp , packed ) -> let param = match fp with | Typedtree . Unit -> None | Typedtree . Named ( id , loc , mty ) -> let mty = if local_buffer then node_for_direct_mod ` Modtype ( remove_indir_mty mty ) else Trie . Leaf in Some ( id , loc . Location . loc , mty ) in Funct ( param , node_for_direct_mod ` Mod packed ) | ` Apply ( funct , arg ) -> let funct = funct . Typedtree . mod_loc , functor_ ( remove_indir_me funct ) in let arg = arg . Typedtree . mod_loc , node_for_direct_mod ` Mod ( remove_indir_me arg ) in Apply { funct ; arg } | ` Unpack -> Unpack in List . fold_left ( remove_top_indir browses ) ~ init : trie ~ f ( : fun trie t -> let open Typedtree in let doc = let attrs = node_attributes t . t_node in let doc = extract_doc attrs in if doc = " " then None else Some doc in match t . t_node with | Signature _ | Structure _ -> assert false | Signature_item _ | Structure_item _ -> begin match match t . t_node with | Signature_item ( item , _ ) -> identify_sig_includes item | Structure_item ( item , _ ) -> identify_str_includes item | _ -> assert false with | ` Not_included -> build ~ local_buffer ~ trie ( Lazy . force t . t_children ) | ` Included ( included_idents , packed ) -> let rec helper packed = let f node = List . fold_left included_idents ~ init : trie ~ f ( : fun trie ( id , ns ) -> Trie . add id { loc = t . t_loc ; doc = None ; namespace = ns ; node } trie ) in match match packed with | ` Mod_expr me -> remove_indir_me me | ` Mod_type mt -> remove_indir_mty mt with | ` Alias path -> let namespace = match packed with | ` Mod_expr _ -> ` Mod | ` Mod_type _ -> ` Modtype in let p = Namespaced_path . of_path ~ namespace path in f ( Included ( Named p ) ) | ` Ident p -> let p = Namespaced_path . of_path ~ namespace ` : Modtype p in f ( Included ( Named p ) ) | ` Mod_type _ | ` Mod_expr _ as packed -> helper packed | ` Functor _ -> assert false | ` Unpack -> f Leaf | ` Apply ( funct , arg ) -> let funct = funct . Typedtree . mod_loc , functor_ ( remove_indir_me funct ) in let arg = arg . Typedtree . mod_loc , node_for_direct_mod ` Mod ( remove_indir_me arg ) in f ( Included ( Apply { funct ; arg } ) ) | ` Str str -> let str = lazy ( build ~ local_buffer ~ trie [ of_structure str ] ) in f ( Included ( Items str ) ) | ` Sg sg -> let sg = lazy ( build ~ local_buffer ~ trie [ of_signature sg ] ) in f ( Included ( Items sg ) ) in helper packed end | Value_binding vb -> let trie = List . fold_left ~ init : trie ~ f ( : fun trie ( id , { Asttypes . loc ; _ } ) -> Trie . add id { loc ; doc ; namespace = ` Vals ; node = Leaf } trie ) ( Typedtree . pat_bound_idents_with_loc vb . vb_pat ) in if not local_buffer then trie else ( let id = Ident . create_local " " ? in let intern = lazy ( build ~ local_buffer ~ trie : Trie . empty ( Lazy . force t . t_children ) ) in let extra_children : Trie . elt = { loc = t . t_loc ; doc = None ; namespace = ` Unknown ; node = Internal intern } in Trie . add id extra_children trie ) | Value_description vd -> Trie . add vd . val_id { loc = t . t_loc ; doc ; namespace = ` Vals ; node = Leaf } trie | Module_binding mb -> let node = node_for_direct_mod ` Mod ( remove_indir_me mb . mb_expr ) in begin match Raw_compat . mb_id mb with | None -> trie | Some id -> Trie . add id { loc = t . t_loc ; doc ; namespace ` = Mod ; node } trie end | Module_declaration md -> let node = node_for_direct_mod ` Mod ( remove_indir_mty md . md_type ) in begin match Raw_compat . md_id md with | None -> trie | Some id -> Trie . add id { loc = t . t_loc ; doc ; namespace ` = Mod ; node } trie end | Module_type_declaration mtd -> let node = match mtd . mtd_type with | None -> Trie . Leaf | Some m -> node_for_direct_mod ` Modtype ( remove_indir_mty m ) in Trie . add mtd . mtd_id { loc = t . t_loc ; doc ; namespace ` = Modtype ; node } trie | Type_declaration td -> Trie . add td . typ_id { loc = t . t_loc ; doc ; namespace = ` Type ; node = Leaf } trie | Type_extension te -> List . fold_left ~ init : trie ~ f ( : fun trie ec -> Trie . add ec . ext_id { loc = t . t_loc ; doc ; namespace = ` Type ; node = Leaf } trie ) te . tyext_constructors | Extension_constructor ec -> Trie . add ec . ext_id { loc = t . t_loc ; doc ; namespace = ` Type ; node = Leaf } trie | Case _ | Expression _ when local_buffer -> build ~ local_buffer ~ trie ( Lazy . force t . t_children ) | Pattern p when local_buffer -> List . fold_left ~ init : trie ~ f ( : fun trie ( id , { Asttypes . loc ; _ } ) -> Trie . add id { loc ; doc ; namespace = ` Vals ; node = Leaf } trie ) ( Typedtree . pat_bound_idents_with_loc p ) | ignored_node -> log ~ title " : build " " ignored node : % t " ( fun ( ) -> string_of_node ignored_node ) ; trie ) |
let of_browses ( ? local_buffer = false ) browses = build ~ local_buffer ~ trie : Trie . empty browses |
type scopes = ( t * Lexing . position option ) list |
type functor_argument = | Handled of Namespaced_path . t * scopes | Noop |
type substitution = { old_prefix : Namespaced_path . t ; new_prefix : Namespaced_path . t ; scopes : scopes } |
type result = | Found of Location . t * string option | Resolves_to of Namespaced_path . t * state { substs : substitution list ; functor_arguments : functor_argument list } |
let rec follow ~ remember_loc ~ state scopes ? before trie path = let trie , before , path , scopes , state = let rec try_substing_once path = function | [ ] -> None | { old_prefix ; new_prefix ; scopes } as subst :: substs -> match Namespaced_path . subst_prefix ~ old_prefix ~ new_prefix path with | Some new_path -> Some ( new_path , scopes , substs ) | None -> match try_substing_once path substs with | None -> None | Some ( new_path , scopes , remaining_substs ) -> Some ( new_path , scopes , subst :: remaining_substs ) in let rec try_substing ( path , _ , substs as acc ) = match try_substing_once path substs with | None -> acc | Some new_acc -> try_substing new_acc in let init = ( path , ( trie , before ) :: scopes , state . substs ) in let res = try_substing init in if res == init then trie , before , path , scopes , state else match res with | path , ( trie , before ) :: scopes , substs -> trie , before , path , scopes , { state with substs } | _ -> assert false in let try_next_scope ( ) = match scopes with | [ ] -> Resolves_to ( path , state ) | ( trie , before ) :: scopes -> follow ~ remember_loc ~ state scopes ? before trie path in match Namespaced_path . head_exn path with | Applied_to path -> let functor_argument = let scopes = ( trie , before ) :: scopes in Handled ( path , scopes ) in let state = { state with functor_arguments = functor_argument :: state . functor_arguments } in let new_path = Namespaced_path . peal_head_exn path in log ~ title " : applicative path " " % s " ( Namespaced_path . to_unique_string new_path ) ; follow ~ remember_loc ~ state scopes ? before trie new_path | Ident ( x , namespace ) -> try let lst = Trie . get x trie in let lst = List . filter lst ~ f ( : fun { Trie . namespace = ns ; _ } -> ns = namespace || ns = ` Unknown ) in let lst = match before with | None -> lst | Some before -> List . filter lst ~ f ( : fun { Trie . loc ; _ } -> Lexing . compare_pos loc . Location . loc_start before < 0 ) in match List . sort lst ~ cmp ( : fun { Trie . loc = l1 ; _ } { loc = l2 ; _ } -> Lexing . compare_pos l2 . Location . loc_end l1 . Location . loc_end ) with | [ ] -> try_next_scope ( ) | { loc ; doc ; node ; namespace = _ } :: _ -> let inspect_functor_arg : Trie . node -> _ = function | Leaf -> Noop | Internal ( lazy trie ) -> let scopes = ( trie , None ) :: ( trie , Some loc . Location . loc_start ) :: scopes in Handled ( Namespaced_path . empty , scopes ) | Included _ -> assert false | Alias path -> let scopes = ( trie , Some loc . Location . loc_start ) :: scopes in Handled ( path , scopes ) | Functor _ -> log ~ title " : inspect_functor_arg " " NOT HANDLED : functor given as functor argument " ; Noop | Apply _ -> Noop in let rec inspect_functor path state : Trie . functor_ -> _ = function | Named new_prefix -> let path = Namespaced_path . rewrite_head ~ new_prefix path in log ~ title " : inspect_functor " " resolves to % s " ( Namespaced_path . to_unique_string path ) ; follow ~ remember_loc ~ state ~ before : loc . Location . loc_start scopes trie path | Unpack -> log ~ title " : inspect_functor " " Unpack " ; Found ( loc , doc ) | Funct _ -> assert false | Apply { funct ; arg } -> log ~ title " : inspect_functor " " functor application " ; let functor_argument = inspect_functor_arg ( snd arg ) in let state = { state with functor_arguments = functor_argument :: state . functor_arguments } in inspect_functor path state ( snd funct ) in let rec inspect_node state = function | Trie . Leaf -> Found ( loc , doc ) | Alias new_prefix -> log ~ title " : aliased " " % s % s = % s " ( Namespaced_path . Id . name x ) ( Namespaced_path . Namespace . to_string namespace ) ( Namespaced_path . to_unique_string new_prefix ) ; let path = Namespaced_path . peal_head_exn path in let new_path = Namespaced_path . rewrite_head ~ new_prefix path in remember_loc loc ; follow ~ remember_loc ~ state ~ before : loc . Location . loc_start scopes trie new_path | Included include_ -> remember_loc loc ; let stampless = Namespaced_path . strip_stamps path in begin match include_ with | Named new_prefix -> let path = Namespaced_path . rewrite_head ~ new_prefix stampless in log ~ title " : include " " resolves to % s " ( Namespaced_path . to_unique_string path ) ; follow ~ remember_loc ~ state ~ before : loc . Location . loc_start scopes trie path | Items t -> follow ~ remember_loc ~ state scopes ( Lazy . force t ) stampless | Apply { funct ; arg } -> log ~ title " : include " " functor application " ; let functor_argument = inspect_functor_arg ( snd arg ) in let state = { state with functor_arguments = functor_argument :: state . functor_arguments } in inspect_functor stampless state ( snd funct ) end | Internal t -> let path = Namespaced_path . peal_head_exn path in begin match Namespaced_path . head path with | None -> Found ( loc , doc ) | Some _ -> let scopes = ( trie , Some loc . Location . loc_start ) :: scopes in follow ~ remember_loc ~ state ? before scopes ( Lazy . force t ) path end | Functor ( param , node ) -> log ~ title " : node " " functor " ; let path = Namespaced_path . peal_head_exn path in begin match Namespaced_path . head path with | None -> Found ( loc , doc ) | _ -> let state = match state . functor_arguments with | [ ] -> assert false | Noop :: functor_arguments -> { state with functor_arguments } | Handled ( new_prefix , scopes ) :: functor_arguments -> let id = match param with | None | Some ( None , _ , _ ) -> assert false | Some ( Some id , _ , _ ) -> id in let subst = { old_prefix = Namespaced_path . of_path ~ namespace ` : Mod ( Pident id ) ; new_prefix ; scopes } in { substs = subst :: state . substs ; functor_arguments } in inspect_node state node end | Apply { funct ; arg } -> log ~ title " : functor application " " " ; let functor_argument = inspect_functor_arg ( snd arg ) in let state = { state with functor_arguments = functor_argument :: state . functor_arguments } in inspect_functor ( Namespaced_path . peal_head_exn path ) state ( snd funct ) in inspect_node state node with | Not_found -> try_next_scope ( ) |
let initial_state = { substs = [ ] ; functor_arguments = [ ] } |
let rec find ~ remember_loc ~ before scopes trie path = match Trie . find_some ( fun _name _stmap { loc ; _ } -> Lexing . compare_pos loc . Location . loc_start before < 0 && Lexing . compare_pos loc . Location . loc_end before > 0 ) trie with | None -> log ~ title " : find " " didn ' t find anything " ; follow ~ state : initial_state ~ remember_loc ~ before scopes trie path | Some ( _name , _stamp , { Trie . loc ; node ; _ } ) -> log ~ title " : find " " inspecting % s " _name ; let rec inspect_node scopes : Trie . node -> _ = function | Internal ( lazy subtrie ) -> let scopes = ( trie , Some loc . Location . loc_start ) :: scopes in find ~ remember_loc ~ before scopes subtrie path | Functor ( None , fnode ) -> inspect_node scopes fnode | Functor ( Some ( id , ploc , node ) , fnode ) -> let param = match id with | None -> Trie . empty | Some id -> Trie . singleton id { loc = ploc ; doc = None ; namespace = ` Mod ; node } in if Lexing . compare_pos ploc . Location . loc_start before < 0 && Lexing . compare_pos ploc . Location . loc_end before > 0 then let scopes = ( trie , Some loc . Location . loc_start ) :: scopes in find ~ remember_loc ~ before scopes param path else let scopes = ( param , Some ploc . Location . loc_end ) :: scopes in inspect_node scopes fnode | Apply { funct = ( floc , f ) ; arg = ( _aloc , a ) } -> let scopes = ( trie , Some loc . Location . loc_start ) :: scopes in if Lexing . compare_pos floc . Location . loc_start before < 0 && Lexing . compare_pos floc . Location . loc_end before > 0 then inspect_functor scopes f else inspect_node scopes a | Leaf | Included _ | Alias _ -> log ~ title " : find " " cursor in a leaf , so we look only before the leaf " ; follow ~ state : initial_state ~ remember_loc ~ before : loc . Location . loc_start scopes trie path and inspect_functor scopes : Trie . functor_ -> _ = function | Funct ( None , fnode ) -> inspect_node scopes fnode | Funct ( Some ( id , ploc , node ) , fnode ) -> let param = match id with | None -> Trie . empty | Some id -> Trie . singleton id { loc = ploc ; doc = None ; namespace = ` Mod ; node } in if Lexing . compare_pos ploc . Location . loc_start before < 0 && Lexing . compare_pos ploc . Location . loc_end before > 0 then let scopes = ( trie , Some loc . Location . loc_start ) :: scopes in find ~ remember_loc ~ before scopes param path else let scopes = ( param , Some ploc . Location . loc_end ) :: scopes in inspect_node scopes fnode | Apply { funct = ( floc , f ) ; arg = ( _aloc , a ) } -> let scopes = ( trie , Some loc . Location . loc_start ) :: scopes in if Lexing . compare_pos floc . Location . loc_start before < 0 && Lexing . compare_pos floc . Location . loc_end before > 0 then inspect_functor scopes f else inspect_node scopes a | Unpack | Named _ -> log ~ title " : find " " cursor in a leaf , so we look only before the leaf " ; follow ~ state : initial_state ~ remember_loc ~ before : loc . Location . loc_start scopes trie path in inspect_node scopes node |
type context = | Initial of Lexing . position | Resume of state |
let rec dump fmt trie = let open Trie in let rec dump_node fmt = function | Leaf -> ( ) | Included Named path -> Format . fprintf fmt " <% s " > ( Namespaced_path . to_string path ) | Alias path -> Format . fprintf fmt " = % s " ( Namespaced_path . to_string path ) | Included Items t | Internal t -> if Lazy . is_val t then Format . fprintf fmt " = % a " dump ( Lazy . force t ) else Format . fprintf fmt " = < lazy " > | Functor ( ( None | Some ( None , _ , _ ) ) , node ) -> Format . fprintf fmt " ( ) ->% a " dump_node node | Functor ( Some ( Some id , _ , _ ) , node ) -> Format . fprintf fmt " % s ->% a " ( Ident . name id ) dump_node node | Included Apply { funct ; arg } | Apply { funct ; arg } -> Format . fprintf fmt " % a ( % a ) " dump_functor funct dump_node ( snd arg ) and dump_functor fmt ( _loc , funct ) = match funct with | Apply { funct ; arg } -> Format . fprintf fmt " % a ( % a ) " dump_functor funct dump_node ( snd arg ) | Funct ( ( None | Some ( None , _ , _ ) ) , node ) -> Format . fprintf fmt " ( ) ->% a " dump_node node | Funct ( Some ( Some id , _ , _ ) , node ) -> Format . fprintf fmt " % s ->% a " ( Ident . name id ) dump_node node | Named path -> Format . fprintf fmt " = % s " ( Namespaced_path . to_string path ) | Unpack -> Format . fprintf fmt " ! unpack " ! in let dump_elt { loc ; namespace ; node ; _ } = Format . pp_print_string fmt ( Namespaced_path . Namespace . to_string namespace ) ; Location . print_loc fmt loc ; dump_node fmt node in Format . pp_print_string fmt " { \ n " ; Trie . iter ( fun ~ name ~ stamp elt -> Format . fprintf fmt " % s /% d -> " name stamp ; dump_elt elt ; Format . pp_print_newline fmt ( ) ) trie ; Format . pp_print_string fmt " } \ n " |
let find ~ remember_loc ~ context trie path = match context with | Initial before -> log ~ title " : initial find " " before % s , trie : % a " ( Lexing . print_position ( ) before ) Logger . fmt ( fun fmt -> dump fmt trie ) ; find ~ remember_loc ~ before [ ] trie path | Resume state -> follow ~ remember_loc ~ state [ ] trie path |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.