text
stringlengths 0
601k
|
---|
let has_remove_aliases_attribute attr = let remove_aliases = Attr_helper . get_no_payload_attribute [ " remove_aliases " ; " ocaml . remove_aliases " ] attr in match remove_aliases with | None -> false | Some _ -> true |
let transl_modtype_longident loc env lid = let ( path , _info ) = Env . lookup_modtype ~ loc lid env in path |
let transl_module_alias loc env lid = Env . lookup_module_path ~ load : false ~ loc lid env |
let mkmty desc typ env loc attrs = let mty = { mty_desc = desc ; mty_type = typ ; mty_loc = loc ; mty_env = env ; mty_attributes = attrs ; } in Cmt_format . add_saved_type ( Cmt_format . Partial_module_type mty ) ; mty |
let mksig desc env loc = let sg = { sig_desc = desc ; sig_loc = loc ; sig_env = env } in Cmt_format . add_saved_type ( Cmt_format . Partial_signature_item sg ) ; sg |
let rec transl_modtype env smty = Builtin_attributes . warning_scope smty . pmty_attributes ( fun ( ) -> transl_modtype_aux env smty ) let mty = transl_modtype env sarg in { mty with mty_type = Mtype . scrape_for_functor_arg env mty . mty_type } let loc = smty . pmty_loc in match smty . pmty_desc with Pmty_ident lid -> let path = transl_modtype_longident loc env lid . txt in mkmty ( Tmty_ident ( path , lid ) ) ( Mty_ident path ) env loc smty . pmty_attributes | Pmty_alias lid -> let path = transl_module_alias loc env lid . txt in mkmty ( Tmty_alias ( path , lid ) ) ( Mty_alias path ) env loc smty . pmty_attributes | Pmty_signature ssg -> let sg = transl_signature env ssg in mkmty ( Tmty_signature sg ) ( Mty_signature sg . sig_type ) env loc smty . pmty_attributes | Pmty_functor ( sarg_opt , sres ) -> let t_arg , ty_arg , newenv = match sarg_opt with | Unit -> Unit , Types . Unit , env | Named ( param , sarg ) -> let arg = transl_modtype_functor_arg env sarg in let ( id , newenv ) = match param . txt with | None -> None , env | Some name -> let scope = Ctype . create_scope ( ) in let id , newenv = let arg_md = { md_type = arg . mty_type ; md_attributes = [ ] ; md_loc = param . loc ; } in Env . enter_module_declaration ~ scope ~ arg : true name Mp_present arg_md env in Some id , newenv in Named ( id , param , arg ) , Types . Named ( id , arg . mty_type ) , newenv in let res = transl_modtype newenv sres in mkmty ( Tmty_functor ( t_arg , res ) ) ( Mty_functor ( ty_arg , res . mty_type ) ) env loc smty . pmty_attributes | Pmty_with ( sbody , constraints ) -> let body = transl_modtype env sbody in let init_sg = extract_sig env sbody . pmty_loc body . mty_type in let remove_aliases = has_remove_aliases_attribute smty . pmty_attributes in let ( rev_tcstrs , final_sg ) = List . fold_left ( fun ( rev_tcstrs , sg ) sdecl -> let ( tcstr , sg ) = merge_constraint env remove_aliases smty . pmty_loc sg sdecl in ( tcstr :: rev_tcstrs , sg ) ) ( [ ] , init_sg ) constraints in let scope = Ctype . create_scope ( ) in mkmty ( Tmty_with ( body , List . rev rev_tcstrs ) ) ( Mtype . freshen ~ scope ( Mty_signature final_sg ) ) env loc smty . pmty_attributes | Pmty_typeof smod -> let env = Env . in_signature false env in let tmty , mty = ! type_module_type_of_fwd env smod in mkmty ( Tmty_typeof tmty ) mty env loc smty . pmty_attributes | Pmty_extension ext -> raise ( Error_forward ( Builtin_attributes . error_of_extension ext ) ) let names = Signature_names . create ( ) in let rec transl_sig env sg = match sg with [ ] -> [ ] , [ ] , env | item :: srem -> let loc = item . psig_loc in match item . psig_desc with | Psig_value sdesc -> let ( tdesc , newenv ) = Typedecl . transl_value_decl env item . psig_loc sdesc in Signature_names . check_value names tdesc . val_loc tdesc . val_id ; let ( trem , rem , final_env ) = transl_sig newenv srem in mksig ( Tsig_value tdesc ) env loc :: trem , Sig_value ( tdesc . val_id , tdesc . val_val , Exported ) :: rem , final_env | Psig_type ( rec_flag , sdecls ) -> let ( decls , newenv ) = Typedecl . transl_type_decl env rec_flag sdecls in List . iter ( fun td -> Signature_names . check_type names td . typ_loc td . typ_id ) decls ; let ( trem , rem , final_env ) = transl_sig newenv srem in let sg = map_rec_type_with_row_types ~ rec_flag ( fun rs td -> Sig_type ( td . typ_id , td . typ_type , rs , Exported ) ) decls rem in mksig ( Tsig_type ( rec_flag , decls ) ) env loc :: trem , sg , final_env | Psig_typesubst sdecls -> let ( decls , newenv ) = Typedecl . transl_type_decl env Nonrecursive sdecls in List . iter ( fun td -> if td . typ_kind <> Ttype_abstract || td . typ_manifest = None || td . typ_private = Private then raise ( Error ( td . typ_loc , env , Invalid_type_subst_rhs ) ) ; let info = let subst = Subst . add_type_function ( Pident td . typ_id ) ~ params : td . typ_type . type_params ~ body ( : Option . get td . typ_type . type_manifest ) Subst . identity in Some ( ` Substituted_away subst ) in Signature_names . check_type ? info names td . typ_loc td . typ_id ) decls ; let ( trem , rem , final_env ) = transl_sig newenv srem in let sg = rem in mksig ( Tsig_typesubst decls ) env loc :: trem , sg , final_env | Psig_typext styext -> let ( tyext , newenv ) = Typedecl . transl_type_extension false env item . psig_loc styext in let constructors = tyext . tyext_constructors in List . iter ( fun ext -> Signature_names . check_typext names ext . ext_loc ext . ext_id ) constructors ; let ( trem , rem , final_env ) = transl_sig newenv srem in mksig ( Tsig_typext tyext ) env loc :: trem , map_ext ( fun es ext -> Sig_typext ( ext . ext_id , ext . ext_type , es , Exported ) ) constructors rem , final_env | Psig_exception sext -> let ( ext , newenv ) = Typedecl . transl_type_exception env sext in let constructor = ext . tyexn_constructor in Signature_names . check_typext names constructor . ext_loc constructor . ext_id ; let ( trem , rem , final_env ) = transl_sig newenv srem in mksig ( Tsig_exception ext ) env loc :: trem , Sig_typext ( constructor . ext_id , constructor . ext_type , Text_exception , Exported ) :: rem , final_env | Psig_module pmd -> let scope = Ctype . create_scope ( ) in let tmty = Builtin_attributes . warning_scope pmd . pmd_attributes ( fun ( ) -> transl_modtype env pmd . pmd_type ) in let pres = match tmty . mty_type with | Mty_alias _ -> Mp_absent | _ -> Mp_present in let md = { md_type = tmty . mty_type ; md_attributes = pmd . pmd_attributes ; md_loc = pmd . pmd_loc ; } in let id , newenv = match pmd . pmd_name . txt with | None -> None , env | Some name -> let id , newenv = Env . enter_module_declaration ~ scope name pres md env in Signature_names . check_module names pmd . pmd_name . loc id ; Some id , newenv in let ( trem , rem , final_env ) = transl_sig newenv srem in mksig ( Tsig_module { md_id = id ; md_name = pmd . pmd_name ; md_presence = pres ; md_type = tmty ; md_loc = pmd . pmd_loc ; md_attributes = pmd . pmd_attributes } ) env loc :: trem , ( match id with | None -> rem | Some id -> Sig_module ( id , pres , md , Trec_not , Exported ) :: rem ) , final_env | Psig_modsubst pms -> let scope = Ctype . create_scope ( ) in let path , md = Env . lookup_module ~ loc : pms . pms_manifest . loc pms . pms_manifest . txt env in let aliasable = not ( Env . is_functor_arg path env ) in let md = if not aliasable then md else { md_type = Mty_alias path ; md_attributes = pms . pms_attributes ; md_loc = pms . pms_loc } in let pres = match md . md_type with | Mty_alias _ -> Mp_absent | _ -> Mp_present in let id , newenv = Env . enter_module_declaration ~ scope pms . pms_name . txt pres md env in let info = ` Substituted_away ( Subst . add_module id path Subst . identity ) in Signature_names . check_module ~ info names pms . pms_name . loc id ; let ( trem , rem , final_env ) = transl_sig newenv srem in mksig ( Tsig_modsubst { ms_id = id ; ms_name = pms . pms_name ; ms_manifest = path ; ms_txt = pms . pms_manifest ; ms_loc = pms . pms_loc ; ms_attributes = pms . pms_attributes } ) env loc :: trem , rem , final_env | Psig_recmodule sdecls -> let ( tdecls , newenv ) = transl_recmodule_modtypes env sdecls in let decls = List . filter_map ( fun md -> match md . md_id with | None -> None | Some id -> Some ( id , md ) ) tdecls in List . iter ( fun ( id , md ) -> Signature_names . check_module names md . md_loc id ) decls ; let ( trem , rem , final_env ) = transl_sig newenv srem in mksig ( Tsig_recmodule tdecls ) env loc :: trem , map_rec ( fun rs ( id , md ) -> let d = { Types . md_type = md . md_type . mty_type ; md_attributes = md . md_attributes ; md_loc = md . md_loc ; } in Sig_module ( id , Mp_present , d , rs , Exported ) ) decls rem , final_env | Psig_modtype pmtd -> let newenv , mtd , sg = transl_modtype_decl names env pmtd in let ( trem , rem , final_env ) = transl_sig newenv srem in mksig ( Tsig_modtype mtd ) env loc :: trem , sg :: rem , final_env | Psig_open sod -> let ( od , newenv ) = type_open_descr env sod in let ( trem , rem , final_env ) = transl_sig newenv srem in mksig ( Tsig_open od ) env loc :: trem , rem , final_env | Psig_include sincl -> let smty = sincl . pincl_mod in let tmty = Builtin_attributes . warning_scope sincl . pincl_attributes ( fun ( ) -> transl_modtype env smty ) in let mty = tmty . mty_type in let scope = Ctype . create_scope ( ) in let sg , newenv = Env . enter_signature ~ scope ( extract_sig env smty . pmty_loc mty ) env in List . iter ( Signature_names . check_sig_item names item . psig_loc ) sg ; let incl = { incl_mod = tmty ; incl_type = sg ; incl_attributes = sincl . pincl_attributes ; incl_loc = sincl . pincl_loc ; } in let ( trem , rem , final_env ) = transl_sig newenv srem in mksig ( Tsig_include incl ) env loc :: trem , sg @ rem , final_env | Psig_class cl -> let ( classes , newenv ) = Typeclass . class_descriptions env cl in List . iter ( fun cls -> let open Typeclass in let loc = cls . cls_id_loc . Location . loc in Signature_names . check_type names loc cls . cls_obj_id ; Signature_names . check_class names loc cls . cls_id ; Signature_names . check_class_type names loc cls . cls_ty_id ; Signature_names . check_type names loc cls . cls_typesharp_id ; ) classes ; let ( trem , rem , final_env ) = transl_sig newenv srem in let sg = map_rec ( fun rs cls -> let open Typeclass in [ Sig_class ( cls . cls_id , cls . cls_decl , rs , Exported ) ; Sig_class_type ( cls . cls_ty_id , cls . cls_ty_decl , rs , Exported ) ; Sig_type ( cls . cls_obj_id , cls . cls_obj_abbr , rs , Exported ) ; Sig_type ( cls . cls_typesharp_id , cls . cls_abbr , rs , Exported ) ] ) classes [ rem ] |> List . flatten in let typedtree = mksig ( Tsig_class ( List . map ( fun decr -> decr . Typeclass . cls_info ) classes ) ) env loc :: trem in typedtree , sg , final_env | Psig_class_type cl -> let ( classes , newenv ) = Typeclass . class_type_declarations env cl in List . iter ( fun decl -> let open Typeclass in let loc = decl . clsty_id_loc . Location . loc in Signature_names . check_class_type names loc decl . clsty_ty_id ; Signature_names . check_type names loc decl . clsty_obj_id ; Signature_names . check_type names loc decl . clsty_typesharp_id ; ) classes ; let ( trem , rem , final_env ) = transl_sig newenv srem in let sg = map_rec ( fun rs decl -> let open Typeclass in [ Sig_class_type ( decl . clsty_ty_id , decl . clsty_ty_decl , rs , Exported ) ; Sig_type ( decl . clsty_obj_id , decl . clsty_obj_abbr , rs , Exported ) ; Sig_type ( decl . clsty_typesharp_id , decl . clsty_abbr , rs , Exported ) ] ) classes [ rem ] |> List . flatten in let typedtree = mksig ( Tsig_class_type ( List . map ( fun decl -> decl . Typeclass . clsty_info ) classes ) ) env loc :: trem in typedtree , sg , final_env | Psig_attribute x -> Builtin_attributes . warning_attribute x ; let ( trem , rem , final_env ) = transl_sig env srem in mksig ( Tsig_attribute x ) env loc :: trem , rem , final_env | Psig_extension ( ext , _attrs ) -> raise ( Error_forward ( Builtin_attributes . error_of_extension ext ) ) in let previous_saved_types = Cmt_format . get_saved_types ( ) in Builtin_attributes . warning_scope [ ] ( fun ( ) -> let ( trem , rem , final_env ) = transl_sig ( Env . in_signature true env ) sg in let rem = Signature_names . simplify final_env names rem in let sg = { sig_items = trem ; sig_type = rem ; sig_final_env = final_env } in Cmt_format . set_saved_types ( ( Cmt_format . Partial_signature sg ) :: previous_saved_types ) ; sg ) Builtin_attributes . warning_scope pmtd . pmtd_attributes ( fun ( ) -> transl_modtype_decl_aux names env pmtd ) { pmtd_name ; pmtd_type ; pmtd_attributes ; pmtd_loc } = let tmty = Option . map ( transl_modtype ( Env . in_signature true env ) ) pmtd_type in let decl = { Types . mtd_type = Option . map ( fun t -> t . mty_type ) tmty ; mtd_attributes = pmtd_attributes ; mtd_loc = pmtd_loc ; } in let scope = Ctype . create_scope ( ) in let ( id , newenv ) = Env . enter_modtype ~ scope pmtd_name . txt decl env in Signature_names . check_modtype names pmtd_loc id ; let mtd = { mtd_id = id ; mtd_name = pmtd_name ; mtd_type = tmty ; mtd_attributes = pmtd_attributes ; mtd_loc = pmtd_loc ; } in newenv , mtd , Sig_modtype ( id , decl , Exported ) let make_env curr = List . fold_left ( fun env ( id , _ , mty ) -> Option . fold ~ none : env ~ some ( : fun id -> Env . add_module ~ arg : true id Mp_present mty env ) id ) env curr in let make_env2 curr = List . fold_left ( fun env ( id , _ , mty ) -> Option . fold ~ none : env ~ some ( : fun id -> Env . add_module ~ arg : true id Mp_present mty . mty_type env ) id ) env curr in let transition env_c curr = List . map2 ( fun pmd ( id , id_loc , _mty ) -> let tmty = Builtin_attributes . warning_scope pmd . pmd_attributes ( fun ( ) -> transl_modtype env_c pmd . pmd_type ) in ( id , id_loc , tmty ) ) sdecls curr in let map_mtys = List . filter_map ( fun ( id , _ , mty ) -> Option . map ( fun id -> ( id , Types . { md_type = mty . mty_type ; md_loc = mty . mty_loc ; md_attributes = mty . mty_attributes } ) ) id ) in let scope = Ctype . create_scope ( ) in let ids = List . map ( fun x -> Option . map ( Ident . create_scoped ~ scope ) x . pmd_name . txt ) sdecls in let approx_env = List . fold_left ( fun env -> Option . fold ~ none : env ~ some ( : fun id -> Env . enter_unbound_module ( Ident . name id ) Mod_unbound_illegal_recursion env ) ) env ids in let init = List . map2 ( fun id pmd -> ( id , pmd . pmd_name , approx_modtype approx_env pmd . pmd_type ) ) ids sdecls in let env0 = make_env init in let dcl1 = Warnings . without_warnings ( fun ( ) -> transition env0 init ) in let env1 = make_env2 dcl1 in check_recmod_typedecls env1 ( map_mtys dcl1 ) ; let dcl2 = transition env1 dcl1 in let env2 = make_env2 dcl2 in check_recmod_typedecls env2 ( map_mtys dcl2 ) ; let dcl2 = List . map2 ( fun pmd ( id , id_loc , mty ) -> { md_id = id ; md_name = id_loc ; md_type = mty ; md_presence = Mp_present ; md_loc = pmd . pmd_loc ; md_attributes = pmd . pmd_attributes } ) sdecls dcl2 in ( dcl2 , env2 ) |
let rec path_of_module mexp = match mexp . mod_desc with | Tmod_ident ( p , _ ) -> p | Tmod_apply ( funct , arg , _coercion ) when ! Clflags . applicative_functors -> Papply ( path_of_module funct , path_of_module arg ) | Tmod_constraint ( mexp , _ , _ , _ ) -> path_of_module mexp | _ -> raise Not_a_path |
let path_of_module mexp = try Some ( path_of_module mexp ) with Not_a_path -> None |
let rec closed_modtype env = function Mty_ident _ -> true | Mty_alias _ -> true | Mty_signature sg -> let env = Env . add_signature sg env in List . for_all ( closed_signature_item env ) sg | Mty_functor ( arg_opt , body ) -> let env = match arg_opt with | Unit | Named ( None , _ ) -> env | Named ( Some id , param ) -> Env . add_module ~ arg : true id Mp_present param env in closed_modtype env body Sig_value ( _id , desc , _ ) -> Ctype . closed_schema env desc . val_type | Sig_module ( _id , _ , md , _ , _ ) -> closed_modtype env md . md_type | _ -> true |
let check_nongen_scheme env sig_item = match sig_item with Sig_value ( _id , vd , _ ) -> if not ( Ctype . closed_schema env vd . val_type ) then raise ( Error ( vd . val_loc , env , Non_generalizable vd . val_type ) ) | Sig_module ( _id , _ , md , _ , _ ) -> if not ( closed_modtype env md . md_type ) then raise ( Error ( md . md_loc , env , Non_generalizable_module md . md_type ) ) | _ -> ( ) |
let check_nongen_schemes env sg = List . iter ( check_nongen_scheme env ) sg |
let anchor_submodule name anchor = match anchor , name with | None , _ | _ , None -> None | Some p , Some name -> Some ( Pdot ( p , name ) ) |
let anchor_recmodule = Option . map ( fun id -> Pident id ) |
let enrich_type_decls anchor decls oldenv newenv = match anchor with None -> newenv | Some p -> List . fold_left ( fun e info -> let id = info . typ_id in let info ' = Mtype . enrich_typedecl oldenv ( Pdot ( p , Ident . name id ) ) id info . typ_type in Env . add_type ~ check : true id info ' e ) oldenv decls |
let enrich_module_type anchor name mty env = match anchor , name with | None , _ | _ , None -> mty | Some p , Some name -> Mtype . enrich_modtype env ( Pdot ( p , name ) ) mty |
let check_recmodule_inclusion env bindings = let subst_and_strengthen env scope s id mty = let mty = Subst . modtype ( Rescope scope ) s mty in match id with | None -> mty | Some id -> Mtype . strengthen ~ aliasable : false env mty ( Subst . module_path s ( Pident id ) ) in let rec check_incl first_time n env s = let scope = Ctype . create_scope ( ) in if n > 0 then begin let bindings1 = List . map ( fun ( id , _name , _mty_decl , _modl , mty_actual , _attrs , _loc ) -> let ids = Option . map ( fun id -> ( id , Ident . create_scoped ~ scope ( Ident . name id ) ) ) id in ( ids , mty_actual ) ) bindings in let env ' = List . fold_left ( fun env ( ids , mty_actual ) -> match ids with | None -> env | Some ( id , id ' ) -> let mty_actual ' = if first_time then mty_actual else subst_and_strengthen env scope s ( Some id ) mty_actual in Env . add_module ~ arg : false id ' Mp_present mty_actual ' env ) env bindings1 in let s ' = List . fold_left ( fun s ( ids , _mty_actual ) -> match ids with | None -> s | Some ( id , id ' ) -> Subst . add_module id ( Pident id ' ) s ) Subst . identity bindings1 in check_incl false ( n - 1 ) env ' s ' end else begin let check_inclusion ( id , name , mty_decl , modl , mty_actual , attrs , loc ) = let mty_decl ' = Subst . modtype ( Rescope scope ) s mty_decl . mty_type and mty_actual ' = subst_and_strengthen env scope s id mty_actual in let coercion = try Includemod . modtypes ~ loc : modl . mod_loc env mty_actual ' mty_decl ' with Includemod . Error msg -> raise ( Error ( modl . mod_loc , env , Not_included msg ) ) in let modl ' = { mod_desc = Tmod_constraint ( modl , mty_decl . mty_type , Tmodtype_explicit mty_decl , coercion ) ; mod_type = mty_decl . mty_type ; mod_env = env ; mod_loc = modl . mod_loc ; mod_attributes = [ ] ; } in { mb_id = id ; mb_name = name ; mb_presence = Mp_present ; mb_expr = modl ' ; mb_attributes = attrs ; mb_loc = loc ; } in List . map check_inclusion bindings end in check_incl true ( List . length bindings ) env Subst . identity |
let rec package_constraints env loc mty constrs = if constrs = [ ] then mty else let sg = extract_sig env loc mty in let sg ' = List . map ( function | Sig_type ( id , ( { type_params [ ] } = as td ) , rs , priv ) when List . mem_assoc [ Ident . name id ] constrs -> let ty = List . assoc [ Ident . name id ] constrs in Sig_type ( id , { td with type_manifest = Some ty } , rs , priv ) | Sig_module ( id , _ , md , rs , priv ) -> let rec aux = function | ( m :: ( ( _ :: _ ) as l ) , t ) :: rest when m = Ident . name id -> ( l , t ) :: aux rest | _ :: rest -> aux rest | [ ] -> [ ] in let md = { md with md_type = package_constraints env loc md . md_type ( aux constrs ) } in Sig_module ( id , Mp_present , md , rs , priv ) | item -> item ) sg in Mty_signature sg ' |
let modtype_of_package env loc p nl tl = match ( Env . find_modtype p env ) . mtd_type with | Some mty when nl <> [ ] -> package_constraints env loc mty ( List . combine ( List . map Longident . flatten nl ) tl ) | _ -> if nl = [ ] then Mty_ident p else raise ( Error ( loc , env , Signature_expected ) ) | exception Not_found -> assert false |
let package_subtype env p1 nl1 tl1 p2 nl2 tl2 = let mkmty p nl tl = let ntl = List . filter ( fun ( _n , t ) -> Ctype . free_variables t = [ ] ) ( List . combine nl tl ) in let ( nl , tl ) = List . split ntl in modtype_of_package env Location . none p nl tl in let mty1 = mkmty p1 nl1 tl1 and mty2 = mkmty p2 nl2 tl2 in try Includemod . modtypes ~ loc : Location . none env mty1 mty2 = Tcoerce_none with Includemod . Error _msg -> false |
let ( ) = Ctype . package_subtype := package_subtype |
let wrap_constraint env mark arg mty explicit = let mark = if mark then Includemod . Mark_both else Includemod . Mark_neither in let coercion = try Includemod . modtypes ~ loc : arg . mod_loc env ~ mark arg . mod_type mty with Includemod . Error msg -> raise ( Error ( arg . mod_loc , env , Not_included msg ) ) in { mod_desc = Tmod_constraint ( arg , mty , explicit , coercion ) ; mod_type = mty ; mod_env = env ; mod_attributes = [ ] ; mod_loc = arg . mod_loc } |
let rec type_module ( ? alias = false ) sttn funct_body anchor env smod = Builtin_attributes . warning_scope smod . pmod_attributes ( fun ( ) -> type_module_aux ~ alias sttn funct_body anchor env smod ) match smod . pmod_desc with Pmod_ident lid -> let path = Env . lookup_module_path ~ load ( : not alias ) ~ loc : smod . pmod_loc lid . txt env in let md = { mod_desc = Tmod_ident ( path , lid ) ; mod_type = Mty_alias path ; mod_env = env ; mod_attributes = smod . pmod_attributes ; mod_loc = smod . pmod_loc } in let aliasable = not ( Env . is_functor_arg path env ) in let md = if alias && aliasable then ( Env . add_required_global ( Path . head path ) ; md ) else match ( Env . find_module path env ) . md_type with | Mty_alias p1 when not alias -> let p1 = Env . normalize_module_path ( Some smod . pmod_loc ) env p1 in let mty = Includemod . expand_module_alias env [ ] p1 in { md with mod_desc = Tmod_constraint ( md , mty , Tmodtype_implicit , Tcoerce_alias ( env , path , Tcoerce_none ) ) ; mod_type = if sttn then Mtype . strengthen ~ aliasable : true env mty p1 else mty } | mty -> let mty = if sttn then Mtype . strengthen ~ aliasable env mty path else mty in { md with mod_type = mty } in rm md | Pmod_structure sstr -> let ( str , sg , names , _finalenv ) = type_structure funct_body anchor env sstr smod . pmod_loc in let md = rm { mod_desc = Tmod_structure str ; mod_type = Mty_signature sg ; mod_env = env ; mod_attributes = smod . pmod_attributes ; mod_loc = smod . pmod_loc } in let sg ' = Signature_names . simplify _finalenv names sg in if List . length sg ' = List . length sg then md else wrap_constraint env false md ( Mty_signature sg ' ) Tmodtype_implicit | Pmod_functor ( arg_opt , sbody ) -> let t_arg , ty_arg , newenv , funct_body = match arg_opt with | Unit -> Unit , Types . Unit , env , false | Named ( param , smty ) -> let mty = transl_modtype_functor_arg env smty in let scope = Ctype . create_scope ( ) in let ( id , newenv ) = match param . txt with | None -> None , env | Some name -> let arg_md = { md_type = mty . mty_type ; md_attributes = [ ] ; md_loc = param . loc ; } in let id , newenv = Env . enter_module_declaration ~ scope ~ arg : true name Mp_present arg_md env in Some id , newenv in Named ( id , param , mty ) , Types . Named ( id , mty . mty_type ) , newenv , true in let body = type_module sttn funct_body None newenv sbody in rm { mod_desc = Tmod_functor ( t_arg , body ) ; mod_type = Mty_functor ( ty_arg , body . mod_type ) ; mod_env = env ; mod_attributes = smod . pmod_attributes ; mod_loc = smod . pmod_loc } | Pmod_apply ( sfunct , sarg ) -> let arg = type_module true funct_body None env sarg in let path = path_of_module arg in let funct = type_module ( sttn && path <> None ) funct_body None env sfunct in begin match Env . scrape_alias env funct . mod_type with | Mty_functor ( Unit , mty_res ) -> if sarg . pmod_desc <> Pmod_structure [ ] then raise ( Error ( sfunct . pmod_loc , env , Apply_generative ) ) ; if funct_body && Mtype . contains_type env funct . mod_type then raise ( Error ( smod . pmod_loc , env , Not_allowed_in_functor_body ) ) ; rm { mod_desc = Tmod_apply ( funct , arg , Tcoerce_none ) ; mod_type = mty_res ; mod_env = env ; mod_attributes = smod . pmod_attributes ; mod_loc = smod . pmod_loc } | Mty_functor ( Named ( param , mty_param ) , mty_res ) as mty_functor -> let coercion = try Includemod . modtypes ~ loc : sarg . pmod_loc env arg . mod_type mty_param with Includemod . Error msg -> raise ( Error ( sarg . pmod_loc , env , Not_included msg ) ) in let mty_appl = match path with | Some path -> let scope = Ctype . create_scope ( ) in let subst = match param with | None -> Subst . identity | Some p -> Subst . add_module p path Subst . identity in Subst . modtype ( Rescope scope ) subst mty_res | None -> let env , nondep_mty = match param with | None -> env , mty_res | Some param -> let env = Env . add_module ~ arg : true param Mp_present arg . mod_type env in check_well_formed_module env smod . pmod_loc " the signature of this functor application " mty_res ; try env , Mtype . nondep_supertype env [ param ] mty_res with Ctype . Nondep_cannot_erase _ -> raise ( Error ( smod . pmod_loc , env , Cannot_eliminate_dependency mty_functor ) ) in begin match Includemod . modtypes ~ loc : smod . pmod_loc env mty_res nondep_mty with | Tcoerce_none -> ( ) | _ -> fatal_error " unexpected coercion from original module type to \ nondep_supertype one " | exception Includemod . Error _ -> fatal_error " nondep_supertype not included in original module type " end ; nondep_mty in check_well_formed_module env smod . pmod_loc " the signature of this functor application " mty_appl ; rm { mod_desc = Tmod_apply ( funct , arg , coercion ) ; mod_type = mty_appl ; mod_env = env ; mod_attributes = smod . pmod_attributes ; mod_loc = smod . pmod_loc } | Mty_alias path -> raise ( Error ( sfunct . pmod_loc , env , Cannot_scrape_alias path ) ) | _ -> raise ( Error ( sfunct . pmod_loc , env , Cannot_apply funct . mod_type ) ) end | Pmod_constraint ( sarg , smty ) -> let arg = type_module ~ alias true funct_body anchor env sarg in let mty = transl_modtype env smty in let md = wrap_constraint env true arg mty . mty_type ( Tmodtype_explicit mty ) in rm { md with mod_loc = smod . pmod_loc ; mod_attributes = smod . pmod_attributes ; } | Pmod_unpack sexp -> if ! Clflags . principal then Ctype . begin_def ( ) ; let exp = Typecore . type_exp env sexp in if ! Clflags . principal then begin Ctype . end_def ( ) ; Ctype . generalize_structure exp . exp_type end ; let mty = match Ctype . expand_head env exp . exp_type with { desc = Tpackage ( p , nl , tl ) } -> if List . exists ( fun t -> Ctype . free_variables t <> [ ] ) tl then raise ( Error ( smod . pmod_loc , env , Incomplete_packed_module exp . exp_type ) ) ; if ! Clflags . principal && not ( Typecore . generalizable ( Btype . generic_level - 1 ) exp . exp_type ) then Location . prerr_warning smod . pmod_loc ( Warnings . Not_principal " this module unpacking " ) ; modtype_of_package env smod . pmod_loc p nl tl | { desc = Tvar _ } -> raise ( Typecore . Error ( smod . pmod_loc , env , Typecore . Cannot_infer_signature ) ) | _ -> raise ( Error ( smod . pmod_loc , env , Not_a_packed_module exp . exp_type ) ) in if funct_body && Mtype . contains_type env mty then raise ( Error ( smod . pmod_loc , env , Not_allowed_in_functor_body ) ) ; rm { mod_desc = Tmod_unpack ( exp , mty ) ; mod_type = mty ; mod_env = env ; mod_attributes = smod . pmod_attributes ; mod_loc = smod . pmod_loc } | Pmod_extension ext -> raise ( Error_forward ( Builtin_attributes . error_of_extension ext ) ) Builtin_attributes . warning_scope sod . popen_attributes ( fun ( ) -> type_open_decl_aux ? used_slot ? toplevel funct_body names env sod ) let loc = od . popen_loc in match od . popen_expr . pmod_desc with | Pmod_ident lid -> let path , newenv = type_open_ ? used_slot ? toplevel od . popen_override env loc lid in let md = { mod_desc = Tmod_ident ( path , lid ) ; mod_type = Mty_alias path ; mod_env = env ; mod_attributes = od . popen_expr . pmod_attributes ; mod_loc = od . popen_expr . pmod_loc } in let open_descr = { open_expr = md ; open_bound_items = [ ] ; open_override = od . popen_override ; open_env = newenv ; open_loc = loc ; open_attributes = od . popen_attributes } in open_descr , [ ] , newenv | _ -> let md = type_module true funct_body None env od . popen_expr in let scope = Ctype . create_scope ( ) in let sg , newenv = Env . enter_signature ~ scope ( extract_sig_open env md . mod_loc md . mod_type ) env in List . iter ( Signature_names . check_sig_item ~ info ` : From_open names loc ) sg ; let sg = List . map ( function | Sig_value ( id , vd , _ ) -> Sig_value ( id , vd , Hidden ) | Sig_type ( id , td , rs , _ ) -> Sig_type ( id , td , rs , Hidden ) | Sig_typext ( id , ec , et , _ ) -> Sig_typext ( id , ec , et , Hidden ) | Sig_module ( id , mp , md , rs , _ ) -> Sig_module ( id , mp , md , rs , Hidden ) | Sig_modtype ( id , mtd , _ ) -> Sig_modtype ( id , mtd , Hidden ) | Sig_class ( id , cd , rs , _ ) -> Sig_class ( id , cd , rs , Hidden ) | Sig_class_type ( id , ctd , rs , _ ) -> Sig_class_type ( id , ctd , rs , Hidden ) ) sg in let open_descr = { open_expr = md ; open_bound_items = sg ; open_override = od . popen_override ; open_env = newenv ; open_loc = loc ; open_attributes = od . popen_attributes } in open_descr , sg , newenv let names = Signature_names . create ( ) in let type_str_item env srem { pstr_loc = loc ; pstr_desc = desc } = match desc with | Pstr_eval ( sexpr , attrs ) -> let expr = Builtin_attributes . warning_scope attrs ( fun ( ) -> Typecore . type_expression env sexpr ) in Tstr_eval ( expr , attrs ) , [ ] , env | Pstr_value ( rec_flag , sdefs ) -> let scope = match rec_flag with | Recursive -> Some ( Annot . Idef { scope with Location . loc_start = loc . Location . loc_start } ) | Nonrecursive -> let start = match srem with | [ ] -> loc . Location . loc_end | { pstr_loc = loc2 } :: _ -> loc2 . Location . loc_start in Some ( Annot . Idef { scope with Location . loc_start = start } ) in let ( defs , newenv ) = Typecore . type_binding env rec_flag sdefs scope in let ( ) = if rec_flag = Recursive then Typecore . check_recursive_bindings env defs in Tstr_value ( rec_flag , defs ) , List . map ( fun ( id , { Asttypes . loc ; _ } , _typ ) -> Signature_names . check_value names loc id ; Sig_value ( id , Env . find_value ( Pident id ) newenv , Exported ) ) ( let_bound_idents_full defs ) , newenv | Pstr_primitive sdesc -> let ( desc , newenv ) = Typedecl . transl_value_decl env loc sdesc in Signature_names . check_value names desc . val_loc desc . val_id ; Tstr_primitive desc , [ Sig_value ( desc . val_id , desc . val_val , Exported ) ] , newenv | Pstr_type ( rec_flag , sdecls ) -> let ( decls , newenv ) = Typedecl . transl_type_decl env rec_flag sdecls in List . iter Signature_names . ( fun td -> check_type names td . typ_loc td . typ_id ) decls ; Tstr_type ( rec_flag , decls ) , map_rec_type_with_row_types ~ rec_flag ( fun rs info -> Sig_type ( info . typ_id , info . typ_type , rs , Exported ) ) decls [ ] , enrich_type_decls anchor decls env newenv | Pstr_typext styext -> let ( tyext , newenv ) = Typedecl . transl_type_extension true env loc styext in let constructors = tyext . tyext_constructors in List . iter Signature_names . ( fun ext -> check_typext names ext . ext_loc ext . ext_id ) constructors ; ( Tstr_typext tyext , map_ext ( fun es ext -> Sig_typext ( ext . ext_id , ext . ext_type , es , Exported ) ) constructors [ ] , newenv ) | Pstr_exception sext -> let ( ext , newenv ) = Typedecl . transl_type_exception env sext in let constructor = ext . tyexn_constructor in Signature_names . check_typext names constructor . ext_loc constructor . ext_id ; Tstr_exception ext , [ Sig_typext ( constructor . ext_id , constructor . ext_type , Text_exception , Exported ) ] , newenv | Pstr_module { pmb_name = name ; pmb_expr = smodl ; pmb_attributes = attrs ; pmb_loc ; } -> let outer_scope = Ctype . get_current_level ( ) in let scope = Ctype . create_scope ( ) in let modl = Builtin_attributes . warning_scope attrs ( fun ( ) -> type_module ~ alias : true true funct_body ( anchor_submodule name . txt anchor ) env smodl ) in let pres = match modl . mod_type with | Mty_alias _ -> Mp_absent | _ -> Mp_present in let md = { md_type = enrich_module_type anchor name . txt modl . mod_type env ; md_attributes = attrs ; md_loc = pmb_loc ; } in Mtype . lower_nongen outer_scope md . md_type ; let id , newenv , sg = match name . txt with | None -> None , env , [ ] | Some name -> let id , e = Env . enter_module_declaration ~ scope name pres md env in Signature_names . check_module names pmb_loc id ; Some id , e , [ Sig_module ( id , pres , { md_type = modl . mod_type ; md_attributes = attrs ; md_loc = pmb_loc ; } , Trec_not , Exported ) ] in Tstr_module { mb_id = id ; mb_name = name ; mb_expr = modl ; mb_presence = pres ; mb_attributes = attrs ; mb_loc = pmb_loc ; } , sg , newenv | Pstr_recmodule sbind -> let sbind = List . map ( function | { pmb_name = name ; pmb_expr = { pmod_desc = Pmod_constraint ( expr , typ ) } ; pmb_attributes = attrs ; pmb_loc = loc ; } -> name , typ , expr , attrs , loc | mb -> raise ( Error ( mb . pmb_expr . pmod_loc , env , Recursive_module_require_explicit_type ) ) ) sbind in let ( decls , newenv ) = transl_recmodule_modtypes env ( List . map ( fun ( name , smty , _smodl , attrs , loc ) -> { pmd_name = name ; pmd_type = smty ; pmd_attributes = attrs ; pmd_loc = loc } ) sbind ) in List . iter ( fun md -> Option . iter Signature_names . ( check_module names md . md_loc ) md . md_id ) decls ; let bindings1 = List . map2 ( fun { md_id = id ; md_type = mty } ( name , _ , smodl , attrs , loc ) -> let modl = Builtin_attributes . warning_scope attrs ( fun ( ) -> type_module true funct_body ( anchor_recmodule id ) newenv smodl ) in let mty ' = enrich_module_type anchor name . txt modl . mod_type newenv in ( id , name , mty , modl , mty ' , attrs , loc ) ) decls sbind in let newenv = List . fold_left ( fun env md -> match md . md_id with | None -> env | Some id -> let mdecl = { md_type = md . md_type . mty_type ; md_attributes = md . md_attributes ; md_loc = md . md_loc ; } in Env . add_module_declaration ~ check : true id Mp_present mdecl env ) env decls in let bindings2 = check_recmodule_inclusion newenv bindings1 in let mbs = List . filter_map ( fun mb -> Option . map ( fun id -> id , mb ) mb . mb_id ) bindings2 in Tstr_recmodule bindings2 , map_rec ( fun rs ( id , mb ) -> Sig_module ( id , Mp_present , { md_type = mb . mb_expr . mod_type ; md_attributes = mb . mb_attributes ; md_loc = mb . mb_loc ; } , rs , Exported ) ) mbs [ ] , newenv | Pstr_modtype pmtd -> let newenv , mtd , sg = transl_modtype_decl names env pmtd in Tstr_modtype mtd , [ sg ] , newenv | Pstr_open sod -> let ( od , sg , newenv ) = type_open_decl ~ toplevel funct_body names env sod in Tstr_open od , sg , newenv | Pstr_class cl -> let ( classes , new_env ) = Typeclass . class_declarations env cl in List . iter ( fun cls -> let open Typeclass in let loc = cls . cls_id_loc . Location . loc in Signature_names . check_class names loc cls . cls_id ; Signature_names . check_class_type names loc cls . cls_ty_id ; Signature_names . check_type names loc cls . cls_obj_id ; Signature_names . check_type names loc cls . cls_typesharp_id ; ) classes ; Tstr_class ( List . map ( fun cls -> ( cls . Typeclass . cls_info , cls . Typeclass . cls_pub_methods ) ) classes ) , List . flatten ( map_rec ( fun rs cls -> let open Typeclass in [ Sig_class ( cls . cls_id , cls . cls_decl , rs , Exported ) ; Sig_class_type ( cls . cls_ty_id , cls . cls_ty_decl , rs , Exported ) ; Sig_type ( cls . cls_obj_id , cls . cls_obj_abbr , rs , Exported ) ; Sig_type ( cls . cls_typesharp_id , cls . cls_abbr , rs , Exported ) ] ) classes [ ] ) , new_env | Pstr_class_type cl -> let ( classes , new_env ) = Typeclass . class_type_declarations env cl in List . iter ( fun decl -> let open Typeclass in let loc = decl . clsty_id_loc . Location . loc in Signature_names . check_class_type names loc decl . clsty_ty_id ; Signature_names . check_type names loc decl . clsty_obj_id ; Signature_names . check_type names loc decl . clsty_typesharp_id ; ) classes ; Tstr_class_type ( List . map ( fun cl -> ( cl . Typeclass . clsty_ty_id , cl . Typeclass . clsty_id_loc , cl . Typeclass . clsty_info ) ) classes ) , List . flatten ( map_rec ( fun rs decl -> let open Typeclass in [ Sig_class_type ( decl . clsty_ty_id , decl . clsty_ty_decl , rs , Exported ) ; Sig_type ( decl . clsty_obj_id , decl . clsty_obj_abbr , rs , Exported ) ; Sig_type ( decl . clsty_typesharp_id , decl . clsty_abbr , rs , Exported ) ] ) classes [ ] ) , new_env | Pstr_include sincl -> let smodl = sincl . pincl_mod in let modl = Builtin_attributes . warning_scope sincl . pincl_attributes ( fun ( ) -> type_module true funct_body None env smodl ) in let scope = Ctype . create_scope ( ) in let sg , new_env = Env . enter_signature ~ scope ( extract_sig_open env smodl . pmod_loc modl . mod_type ) env in List . iter ( Signature_names . check_sig_item names loc ) sg ; let incl = { incl_mod = modl ; incl_type = sg ; incl_attributes = sincl . pincl_attributes ; incl_loc = sincl . pincl_loc ; } in Tstr_include incl , sg , new_env | Pstr_extension ( ext , _attrs ) -> raise ( Error_forward ( Builtin_attributes . error_of_extension ext ) ) | Pstr_attribute x -> Builtin_attributes . warning_attribute x ; Tstr_attribute x , [ ] , env in let rec type_struct env sstr = match sstr with | [ ] -> ( [ ] , [ ] , env ) | pstr :: srem -> let previous_saved_types = Cmt_format . get_saved_types ( ) in let desc , sg , new_env = type_str_item env srem pstr in let str = { str_desc = desc ; str_loc = pstr . pstr_loc ; str_env = env } in Cmt_format . set_saved_types ( Cmt_format . Partial_structure_item str :: previous_saved_types ) ; let ( str_rem , sig_rem , final_env ) = type_struct new_env srem in ( str :: str_rem , sg @ sig_rem , final_env ) in if ! Clflags . annotations then List . iter ( function { pstr_loc = l } -> Stypes . record_phrase l ) sstr ; let previous_saved_types = Cmt_format . get_saved_types ( ) in let run ( ) = let ( items , sg , final_env ) = type_struct env sstr in let str = { str_items = items ; str_type = sg ; str_final_env = final_env } in Cmt_format . set_saved_types ( Cmt_format . Partial_structure str :: previous_saved_types ) ; str , sg , names , final_env in if toplevel then run ( ) else Builtin_attributes . warning_scope [ ] run |
let type_toplevel_phrase env s = Env . reset_required_globals ( ) ; let ( str , sg , to_remove_from_sg , env ) = type_structure ~ toplevel : true false None env s Location . none in ( str , sg , to_remove_from_sg , env ) |
let type_module_alias = type_module ~ alias : true true false None |
let type_module = type_module true false None |
let type_structure = type_structure false None |
let rec normalize_modtype env = function Mty_ident _ | Mty_alias _ -> ( ) | Mty_signature sg -> normalize_signature env sg | Mty_functor ( _param , body ) -> normalize_modtype env body Sig_value ( _id , desc , _ ) -> Ctype . normalize_type env desc . val_type | Sig_module ( _id , _ , md , _ , _ ) -> normalize_modtype env md . md_type | _ -> ( ) |
let type_module_type_of env smod = let remove_aliases = has_remove_aliases_attribute smod . pmod_attributes in let tmty = match smod . pmod_desc with | Pmod_ident lid -> let path , md = Env . lookup_module ~ loc : smod . pmod_loc lid . txt env in rm { mod_desc = Tmod_ident ( path , lid ) ; mod_type = md . md_type ; mod_env = env ; mod_attributes = smod . pmod_attributes ; mod_loc = smod . pmod_loc } | _ -> type_module env smod in let mty = Mtype . scrape_for_type_of ~ remove_aliases env tmty . mod_type in if not ( closed_modtype env mty ) then raise ( Error ( smod . pmod_loc , env , Non_generalizable_module mty ) ) ; tmty , mty |
let rec extend_path path = fun lid -> match lid with | Lident name -> Pdot ( path , name ) | Ldot ( m , name ) -> Pdot ( extend_path path m , name ) | Lapply _ -> assert false |
let lookup_type_in_sig sg = let types , modules = List . fold_left ( fun acc item -> match item with | Sig_type ( id , _ , _ , _ ) -> let types , modules = acc in let types = String . Map . add ( Ident . name id ) id types in types , modules | Sig_module ( id , _ , _ , _ , _ ) -> let types , modules = acc in let modules = String . Map . add ( Ident . name id ) id modules in types , modules | _ -> acc ) ( String . Map . empty , String . Map . empty ) sg in let rec module_path = function | Lident name -> Pident ( String . Map . find name modules ) | Ldot ( m , name ) -> Pdot ( module_path m , name ) | Lapply _ -> assert false in fun lid -> match lid with | Lident name -> Pident ( String . Map . find name types ) | Ldot ( m , name ) -> Pdot ( module_path m , name ) | Lapply _ -> assert false |
let type_package env m p nl = Ctype . begin_def ( ) ; let context = Typetexp . narrow ( ) in let modl = type_module env m in let scope = Ctype . create_scope ( ) in Typetexp . widen context ; let nl ' , tl ' , env = match nl with | [ ] -> [ ] , [ ] , env | nl -> let type_path , env = match modl . mod_desc with | Tmod_ident ( mp , _ ) | Tmod_constraint ( { mod_desc = Tmod_ident ( mp , _ ) } , _ , Tmodtype_implicit , _ ) -> extend_path mp , env | _ -> let sg = extract_sig_open env modl . mod_loc modl . mod_type in let sg , env = Env . enter_signature ~ scope sg env in lookup_type_in_sig sg , env in let nl ' , tl ' = List . fold_right ( fun lid ( nl , tl ) -> match type_path lid with | exception Not_found -> ( nl , tl ) | path -> begin match Env . find_type path env with | exception Not_found -> ( nl , tl ) | decl -> if decl . type_arity > 0 then begin ( nl , tl ) end else begin let t = Btype . newgenty ( Tconstr ( path , [ ] , ref Mnil ) ) in ( lid :: nl , t :: tl ) end end ) nl ( [ ] , [ ] ) in nl ' , tl ' , env in Ctype . end_def ( ) ; let mty = if nl = [ ] then ( Mty_ident p ) else modtype_of_package env modl . mod_loc p nl ' tl ' in List . iter2 ( fun n ty -> try Ctype . unify env ty ( Ctype . newvar ( ) ) with Ctype . Unify _ -> raise ( Error ( modl . mod_loc , env , Scoping_pack ( n , ty ) ) ) ) nl ' tl ' ; let modl = wrap_constraint env true modl mty Tmodtype_implicit in assert ( List . length nl = List . length tl ' ) ; modl , tl ' |
let type_open_decl ? used_slot env od = type_open_decl ? used_slot ? toplevel : None false ( Signature_names . create ( ) ) env od |
let type_open_descr ? used_slot env od = type_open_descr ? used_slot ? toplevel : None env od |
let ( ) = Typecore . type_module := type_module_alias ; Typetexp . transl_modtype_longident := transl_modtype_longident ; Typetexp . transl_modtype := transl_modtype ; Typecore . type_open := type_open_ ? toplevel : None ; Typecore . type_open_decl := type_open_decl ; Typecore . type_package := type_package ; Typeclass . type_open_descr := type_open_descr ; type_module_type_of_fwd := type_module_type_of |
let type_implementation sourcefile outputprefix modulename initial_env ast = Cmt_format . clear ( ) ; Misc . try_finally ( fun ( ) -> Typecore . reset_delayed_checks ( ) ; Env . reset_required_globals ( ) ; if ! Clflags . print_types then Warnings . parse_options false " - 32 - 34 - 37 - 38 - 60 " ; let ( str , sg , names , finalenv ) = type_structure initial_env ast ( Location . in_file sourcefile ) in let simple_sg = Signature_names . simplify finalenv names sg in if ! Clflags . print_types then begin Typecore . force_delayed_checks ( ) ; Printtyp . wrap_printing_env ~ error : false initial_env ( fun ( ) -> fprintf std_formatter " % a . " @ ( Printtyp . printed_signature sourcefile ) simple_sg ) ; ( str , Tcoerce_none ) end else begin let sourceintf = Filename . remove_extension sourcefile ^ ! Config . interface_suffix in if Sys . file_exists sourceintf then begin let intf_file = try Load_path . find_uncap ( modulename ^ " . cmi " ) with Not_found -> raise ( Error ( Location . in_file sourcefile , Env . empty , Interface_not_compiled sourceintf ) ) in let dclsig = Env . read_signature modulename intf_file in let coercion = Includemod . compunit initial_env ~ mark : Includemod . Mark_positive sourcefile sg intf_file dclsig in Typecore . force_delayed_checks ( ) ; Cmt_format . save_cmt ( outputprefix ^ " . cmt " ) modulename ( Cmt_format . Implementation str ) ( Some sourcefile ) initial_env None ; ( str , coercion ) end else begin let coercion = Includemod . compunit initial_env ~ mark : Includemod . Mark_positive sourcefile sg " ( inferred signature ) " simple_sg in check_nongen_schemes finalenv simple_sg ; normalize_signature finalenv simple_sg ; Typecore . force_delayed_checks ( ) ; if not ! Clflags . dont_write_files then begin let alerts = Builtin_attributes . alerts_of_str ast in let cmi = Env . save_signature ~ alerts simple_sg modulename ( outputprefix ^ " . cmi " ) in Cmt_format . save_cmt ( outputprefix ^ " . cmt " ) modulename ( Cmt_format . Implementation str ) ( Some sourcefile ) initial_env ( Some cmi ) ; end ; ( str , coercion ) end end ) ~ exceptionally ( : fun ( ) -> Cmt_format . save_cmt ( outputprefix ^ " . cmt " ) modulename ( Cmt_format . Partial_implementation ( Array . of_list ( Cmt_format . get_saved_types ( ) ) ) ) ( Some sourcefile ) initial_env None ) |
let save_signature modname tsg outputprefix source_file initial_env cmi = Cmt_format . save_cmt ( outputprefix ^ " . cmti " ) modname ( Cmt_format . Interface tsg ) ( Some source_file ) initial_env ( Some cmi ) |
let type_interface env ast = transl_signature env ast |
let package_signatures units = let units_with_ids = List . map ( fun ( name , sg ) -> let oldid = Ident . create_persistent name in let newid = Ident . create_local name in ( oldid , newid , sg ) ) units in let subst = List . fold_left ( fun acc ( oldid , newid , _ ) -> Subst . add_module oldid ( Pident newid ) acc ) Subst . identity units_with_ids in List . map ( fun ( _ , newid , sg ) -> let sg = Subst . signature Make_local subst sg in let md = { md_type = Mty_signature sg ; md_attributes [ ] ; = md_loc = Location . none ; } in Sig_module ( newid , Mp_present , md , Trec_not , Exported ) ) units_with_ids |
let package_units initial_env objfiles cmifile modulename = let units = List . map ( fun f -> let pref = chop_extensions f in let modname = String . capitalize_ascii ( Filename . basename pref ) in let sg = Env . read_signature modname ( pref ^ " . cmi " ) in if Filename . check_suffix f " . cmi " && not ( Mtype . no_code_needed_sig Env . initial_safe_string sg ) then raise ( Error ( Location . none , Env . empty , Implementation_is_required f ) ) ; ( modname , Env . read_signature modname ( pref ^ " . cmi " ) ) ) objfiles in Ident . reinit ( ) ; let sg = package_signatures units in let prefix = Filename . remove_extension cmifile in let mlifile = prefix ^ ! Config . interface_suffix in if Sys . file_exists mlifile then begin if not ( Sys . file_exists cmifile ) then begin raise ( Error ( Location . in_file mlifile , Env . empty , Interface_not_compiled mlifile ) ) end ; let dclsig = Env . read_signature modulename cmifile in Cmt_format . save_cmt ( prefix ^ " . cmt " ) modulename ( Cmt_format . Packed ( sg , objfiles ) ) None initial_env None ; Includemod . compunit initial_env " ( obtained by packing ) " sg mlifile dclsig end else begin let unit_names = List . map fst units in let imports = List . filter ( fun ( name , _crc ) -> not ( List . mem name unit_names ) ) ( Env . imports ( ) ) in if not ! Clflags . dont_write_files then begin let cmi = Env . save_signature_with_imports ~ alerts : Misc . Stdlib . String . Map . empty sg modulename ( prefix ^ " . cmi " ) imports in Cmt_format . save_cmt ( prefix ^ " . cmt " ) modulename ( Cmt_format . Packed ( cmi . Cmi_format . cmi_sign , objfiles ) ) None initial_env ( Some cmi ) end ; Tcoerce_none end |
let report_error ppf = function Cannot_apply mty -> fprintf ppf " [ @ This module is not a functor ; it has type @ % a ] " @ modtype mty | Not_included errs -> fprintf ppf " [ @< v > Signature mismatch :@ % a ] " @ Includemod . report_error errs | Cannot_eliminate_dependency mty -> fprintf ppf " [ @ This functor has type @ % a @ \ The parameter cannot be eliminated in the result type . @ \ Please bind the argument to a module identifier . ] " @ modtype mty | Signature_expected -> fprintf ppf " This module type is not a signature " | Structure_expected mty -> fprintf ppf " [ @ This module is not a structure ; it has type @ % a " modtype mty | With_no_component lid -> fprintf ppf " [ @ The signature constrained by ` with ' has no component named % a ] " @ longident lid | With_mismatch ( lid , explanation ) -> fprintf ppf " [ @< v >\ [ @ In this ` with ' constraint , the new definition of % a @ \ does not match its original definition @ \ in the constrained signature ] :@@ \ % a ] " @ longident lid Includemod . report_error explanation | With_makes_applicative_functor_ill_typed ( lid , path , explanation ) -> fprintf ppf " [ @< v >\ [ @ This ` with ' constraint on % a makes the applicative functor @ \ type % s ill - typed in the constrained signature ] :@@ \ % a ] " @ longident lid ( Path . name path ) Includemod . report_error explanation | With_changes_module_alias ( lid , id , path ) -> fprintf ppf " [ @< v >\ [ @ This ` with ' constraint on % a changes % s , which is aliased @ \ in the constrained signature ( as % s ) ] . ] " @@ longident lid ( Path . name path ) ( Ident . name id ) | With_cannot_remove_constrained_type -> fprintf ppf " [ @< v > Destructive substitutions are not supported for constrained @ \ types ( other than when replacing a type constructor with @ \ a type constructor with the same arguments ) . ] " @ | Repeated_name ( kind , name ) -> fprintf ppf " [ @ Multiple definition of the % s name % s . @ \ Names must be unique in a given structure or signature . ] " @ ( Sig_component_kind . to_string kind ) name | Non_generalizable typ -> fprintf ppf " [ @ The type of this expression , @ % a , @ \ contains type variables that cannot be generalized ] " @ type_scheme typ | Non_generalizable_class ( id , desc ) -> fprintf ppf " [ @ The type of this class , @ % a , @ \ contains type variables that cannot be generalized ] " @ ( class_declaration id ) desc | Non_generalizable_module mty -> fprintf ppf " [ @ The type of this module , @ % a , @ \ contains type variables that cannot be generalized ] " @ modtype mty | Implementation_is_required intf_name -> fprintf ppf " [ @ The interface % a @ declares values , not just types . @ \ An implementation must be provided . ] " @ Location . print_filename intf_name | Interface_not_compiled intf_name -> fprintf ppf " [ @ Could not find the . cmi file for interface @ % a . ] " @ Location . print_filename intf_name | Not_allowed_in_functor_body -> fprintf ppf " [ @ This expression creates fresh types . @ % s ] " @ " It is not allowed inside applicative functors . " | Not_a_packed_module ty -> fprintf ppf " This expression is not a packed module . It has type @ % a " type_expr ty | Incomplete_packed_module ty -> fprintf ppf " The type of this packed module contains variables :@ % a " type_expr ty | Scoping_pack ( lid , ty ) -> fprintf ppf " The type % a in this module cannot be exported . @ " longident lid ; fprintf ppf " Its type contains local dependencies :@ % a " type_expr ty | Recursive_module_require_explicit_type -> fprintf ppf " Recursive modules require an explicit module type . " | Apply_generative -> fprintf ppf " This is a generative functor . It can only be applied to ( ) " | Cannot_scrape_alias p -> fprintf ppf " This is an alias for module % a , which is missing " path p | Badly_formed_signature ( context , err ) -> fprintf ppf " [ @ In % s :@ % a ] " @ context Typedecl . report_error err | Cannot_hide_id Illegal_shadowing { shadowed_item_kind ; shadowed_item_id ; shadowed_item_loc ; shadower_id ; user_id ; user_kind ; user_loc } -> let shadowed_item_kind = Sig_component_kind . to_string shadowed_item_kind in fprintf ppf " [ @< v > Illegal shadowing of included % s % a by % a @ \ % a ; :@< 1 2 >% s % a came from this include @ \ % a ; :@< 1 2 > The % s % s has no valid type if % a is shadowed ] " @ shadowed_item_kind Ident . print shadowed_item_id Ident . print shadower_id Location . print_loc shadowed_item_loc ( String . capitalize_ascii shadowed_item_kind ) Ident . print shadowed_item_id Location . print_loc user_loc ( Sig_component_kind . to_string user_kind ) ( Ident . name user_id ) Ident . print shadowed_item_id | Cannot_hide_id Appears_in_signature { opened_item_kind ; opened_item_id ; user_id ; user_kind ; user_loc } -> let opened_item_kind = Sig_component_kind . to_string opened_item_kind in fprintf ppf " [ @< v > The % s % a introduced by this open appears in the signature @ \ % a ; :@< 1 2 > The % s % s has no valid type if % a is hidden ] " @ opened_item_kind Ident . print opened_item_id Location . print_loc user_loc ( Sig_component_kind . to_string user_kind ) ( Ident . name user_id ) Ident . print opened_item_id | Invalid_type_subst_rhs -> fprintf ppf " Only type synonyms are allowed on the right of " := |
let report_error env ppf err = Printtyp . wrap_printing_env ~ error : true env ( fun ( ) -> report_error ppf err ) |
let ( ) = Location . register_error_of_exn ( function | Error ( loc , env , err ) -> Some ( Location . error_of_printer ~ loc ( report_error env ) err ) | Error_forward err -> Some err | _ -> None ) |
let should_hide ( x : Typedtree . module_binding ) = match x . mb_attributes with | [ ] -> false | { attr_name = { txt = " internal . local " ; _ } ; _ } :: _ -> true | _ :: rest -> Ext_list . exists rest ( fun { attr_name = x ; _ } -> x . txt = " internal . local " ) |
let attrs : Parsetree . attributes = [ { attr_name = { txt = " internal . local " ; loc = Location . none } ; attr_payload = PStr [ ] ; attr_loc = Location . none ; } ; ] |
let no_type_defined ( x : Parsetree . structure_item ) = match x . pstr_desc with | Pstr_eval _ | Pstr_value _ | Pstr_primitive _ | Pstr_typext _ | Pstr_exception _ -> true | Pstr_include { pincl_mod = { pmod_desc = Pmod_constraint ( { pmod_desc = Pmod_structure [ { pstr_desc = Pstr_primitive _ } ] ; } , _ ) ; } ; } -> true | _ -> false |
let check ( x : Parsetree . structure ) = Ext_list . iter x ( fun x -> if not ( no_type_defined x ) then Location . raise_errorf ~ loc : x . pstr_loc " the structure is not supported in local extension " ) |
module List = struct include List let compare cmp a b = let rec loop a b = match a , b with | [ ] , [ ] -> 0 | [ ] , _ -> - 1 | _ , [ ] -> 1 | x :: xs , y :: ys -> let n = cmp x y in if n = 0 then loop xs ys else n in loop a b ; ; end |
module Uid : sig type t val compare : t -> t -> int val equal : t -> t -> bool val next : string -> t val hash : t -> int val name : t -> string val static : t type t = { code : int ; name : string } let compare a b = compare ( a . code : int ) b . code let equal a b = ( a . code : int ) = b . code let uid = ref 0 let next name = let code = ! uid in incr uid ; { code ; name } ; ; let hash a = Hashtbl . hash a . code let name a = a . name let static = next " static " end |
module Key = struct type t = { uid : Uid . t ; params : t list } let rec compare k1 k2 = if k1 == k2 then 0 else ( let cmp = Uid . compare k1 . uid k2 . uid in if cmp <> 0 then cmp else List . compare compare k1 . params k2 . params ) ; ; let equal a b = compare a b = 0 let hash = ( Hashtbl . hash : t -> int ) let static = { uid = Uid . static ; params = [ ] } end |
type ' a t = Key . t |
type ' a typename = ' a t |
let key t = t |
let uid t = t . Key . uid |
let name t = Uid . name t . Key . uid |
let create ( ? name = " Typename . create " ) ( ) = { Key . uid = Uid . next name ; params = [ ] } let same ( type a b ) ( nm1 : a t ) ( nm2 : b t ) = Key . compare nm1 nm2 = 0 let same_witness ( type a b ) ( nm1 : a t ) ( nm2 : b t ) = if Key . compare nm1 nm2 = 0 then Some ( Obj . magic Type_equal . refl : ( a , b ) Type_equal . t ) else None ; ; let same_witness_exn ( type a b ) ( nm1 : a t ) ( nm2 : b t ) = if Key . compare nm1 nm2 = 0 then ( Obj . magic Type_equal . refl : ( a , b ) Type_equal . t ) else failwith " Typename . same_witness_exn " ; ; end |
module type S0 = sig type t val typename_of_t : t typename end |
module type S1 = sig type ' a t val typename_of_t : ' a typename -> ' a t typename end |
module type S2 = sig type ( ' a , ' b ) t val typename_of_t : ' a typename -> ' b typename -> ( ' a , ' b ) t typename end |
module type S3 = sig type ( ' a , ' b , ' c ) t val typename_of_t : ' a typename -> ' b typename -> ' c typename -> ( ' a , ' b , ' c ) t typename end |
module type S4 = sig type ( ' a , ' b , ' c , ' d ) t val typename_of_t : ' a typename -> ' b typename -> ' c typename -> ' d typename -> ( ' a , ' b , ' c , ' d ) t typename end |
module type S5 = sig type ( ' a , ' b , ' c , ' d , ' e ) t val typename_of_t : ' a typename -> ' b typename -> ' c typename -> ' d typename -> ' e typename -> ( ' a , ' b , ' c , ' d , ' e ) t typename end |
module Make0 ( X : Named_intf . S0 ) = struct let uid = Uid . next X . name let typename_of_t = { Key . uid ; params = [ ] } end |
module Make1 ( X : Named_intf . S1 ) = struct let uid = Uid . next X . name let typename_of_t a = { Key . uid ; params = [ a ] } end |
module Make2 ( X : Named_intf . S2 ) = struct let uid = Uid . next X . name let typename_of_t a b = { Key . uid ; params = [ a ; b ] } end |
module Make3 ( X : Named_intf . S3 ) = struct let uid = Uid . next X . name let typename_of_t a b c = { Key . uid ; params = [ a ; b ; c ] } end |
module Make4 ( X : Named_intf . S4 ) = struct let uid = Uid . next X . name let typename_of_t a b c d = { Key . uid ; params = [ a ; b ; c ; d ] } end |
module Make5 ( X : Named_intf . S5 ) = struct let uid = Uid . next X . name let typename_of_t a b c d e = { Key . uid ; params = [ a ; b ; c ; d ; e ] } end |
module Key_table = Hashtbl . Make ( Key ) |
module Table ( X : sig type ' a t end ) = struct type data = Data : ' a t * ' a X . t -> data type t = data Key_table . t let create int = Key_table . create int let mem table name = Key_table . mem table ( key name ) let set table name data = Key_table . replace table ( key name ) ( Data ( name , data ) ) let find ( type a ) table ( name : a typename ) = let data = try Some ( Key_table . find table ( key name ) ) with | Base . Not_found_s _ | Caml . Not_found -> None in match data with | None -> None | Some ( Data ( name ' , data ) ) -> ( fun ( type b ) ( name ' : b typename ) ( data : b X . t ) -> let Type_equal . T = ( same_witness_exn name ' name : ( b , a ) Type_equal . t ) in Some ( data : a X . t ) ) name ' data ; ; end |
let fail uid_a uid_b = let msg = Printf . sprintf " Typename . Same_witness_exn % S % S " ( Uid . name uid_a ) ( Uid . name uid_b ) in failwith msg ; ; |
module Same_witness_exn_1 ( A : S1 ) ( B : S1 ) = struct type t = { eq : ' a . ( ' a A . t , ' a B . t ) Type_equal . t } let witness = let uid_a = uid ( A . typename_of_t static ) in let uid_b = uid ( B . typename_of_t static ) in if Uid . equal uid_a uid_b then { eq = Obj . magic Type_equal . refl } else fail uid_a uid_b ; ; end |
module Same_witness_exn_2 ( A : S2 ) ( B : S2 ) = struct type t = { eq : ' a ' b . ( ( ' a , ' b ) A . t , ( ' a , ' b ) B . t ) Type_equal . t } let witness = let uid_a = uid ( A . typename_of_t static static ) in let uid_b = uid ( B . typename_of_t static static ) in if Uid . equal uid_a uid_b then { eq = Obj . magic Type_equal . refl } else fail uid_a uid_b ; ; end |
module Same_witness_exn_3 ( A : S3 ) ( B : S3 ) = struct type t = { eq : ' a ' b ' c . ( ( ' a , ' b , ' c ) A . t , ( ' a , ' b , ' c ) B . t ) Type_equal . t } let witness = let uid_a = uid ( A . typename_of_t static static static ) in let uid_b = uid ( B . typename_of_t static static static ) in if Uid . equal uid_a uid_b then { eq = Obj . magic Type_equal . refl } else fail uid_a uid_b ; ; end |
module Same_witness_exn_4 ( A : S4 ) ( B : S4 ) = struct type t = { eq : ' a ' b ' c ' d . ( ( ' a , ' b , ' c , ' d ) A . t , ( ' a , ' b , ' c , ' d ) B . t ) Type_equal . t } let witness = let uid_a = uid ( A . typename_of_t static static static static ) in let uid_b = uid ( B . typename_of_t static static static static ) in if Uid . equal uid_a uid_b then { eq = Obj . magic Type_equal . refl } else fail uid_a uid_b ; ; end |
module Same_witness_exn_5 ( A : S5 ) ( B : S5 ) = struct type t = { eq : ' a ' b ' c ' d ' e . ( ( ' a , ' b , ' c , ' d , ' e ) A . t , ( ' a , ' b , ' c , ' d , ' e ) B . t ) Type_equal . t } let witness = let uid_a = uid ( A . typename_of_t static static static static static ) in let uid_b = uid ( B . typename_of_t static static static static static ) in if Uid . equal uid_a uid_b then { eq = Obj . magic Type_equal . refl } else fail uid_a uid_b ; ; end |
type t = { env : Env . t ; get_search_dirs : string -> string list } let from_summary ~ get_search_dirs summary subst = Persistent_env . get_search_dirs := get_search_dirs ; Envaux . reset_cache ( ) ; let env = Envaux . env_from_summary ( Obj . magic summary ) summary subst in { env ; get_search_dirs } let find_value path { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Env . find_value path env let find_value_by_name name { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Env . find_value_by_name name env let find_type path { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Env . find_type path env let find_constructor_by_name lid { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Env . find_constructor_by_name lid env let find_value_address path { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Env . find_value_address path env |> Obj . magic let find_module_address path { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Env . find_module_address path env |> Obj . magic let find_modtype_expansion path { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Env . find_modtype_expansion path env let is_structure_module path { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Env . is_structure_module path env let add_module ? arg id presence mty { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; let env ' = Env . add_module ? arg id presence mty env in { env = env ' ; get_search_dirs } let extract_modules path { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Env . extract_modules path env let extract_values path { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Env . extract_values path env let type_apply { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Ctype . apply ( Obj . magic env ) env let type_matches { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Ctype . matches ( Obj . magic env ) env let full_expand { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Ctype . full_expand ( Obj . magic env ) env let ( ) = Env . add_delayed_check_forward := Obj . magic ! Ocaml_common . Env . add_delayed_check_forward ; Env . same_constr := Obj . magic ! Ocaml_common . Env . same_constr ; Env . check_well_formed_module := Obj . magic ! Ocaml_common . Env . check_well_formed_module ; Env . check_functor_application := Obj . magic ! Ocaml_common . Env . check_functor_application ; Env . strengthen := Obj . magic ! Ocaml_common . Env . strengthen ; Env . print_longident := Obj . magic ! Ocaml_common . Env . print_longident ; Env . print_path := Obj . magic ! Ocaml_common . Env . print_path ; Ctype . package_subtype := Obj . magic ! Ocaml_common . Ctype . package_subtype ; Persistent_env . add_delayed_check_forward := Obj . magic ! Ocaml_common . Persistent_env . add_delayed_check_forward ; |
type callable_and_self_argument = { callable : Type . Callable . t ; self_argument : Type . t option ; } |
module TypeOperation = struct module Compose = struct let apply_callable ~ signature_select input_annotation ~ callable_and_self { : callable ; self_argument } = match signature_select ~ arguments : [ { AttributeResolution . Argument . expression = None ; kind = Ast . Expression . Call . Argument . Positional ; resolved = input_annotation ; } ; ] ~ callable ~ self_argument with | SignatureSelectionTypes . Found { selected_return_annotation } -> Some selected_return_annotation | _ -> None let compose ~ signature_select { callable = original_left_callable ; self_argument = left_self_argument } right_callable_and_self = let replace_return_annotation new_annotation ~ input = { input with Type . Callable . implementation = { input . Type . Callable . implementation with annotation = new_annotation } ; } in let namespace = Type . Variable . Namespace . create_fresh ( ) in let compose left_callable_namespaced = let left_free_variables = Type . Variable . all_free_variables ( Type . Callable left_callable_namespaced ) in Type . Callable . map ~ f ( : Type . Variable . mark_all_variables_as_bound ~ specific : left_free_variables ) left_callable_namespaced >>| ( fun { Type . Callable . implementation = { annotation ; _ } ; _ } -> annotation ) >>= apply_callable ~ signature_select ~ callable_and_self : right_callable_and_self >>| Type . Variable . mark_all_variables_as_free ~ specific ( : List . map ~ f : Type . Variable . mark_as_bound left_free_variables ) >>| replace_return_annotation ~ input : left_callable_namespaced >>| fun result -> { callable = result ; self_argument = left_self_argument } in Type . Callable . map ~ f ( : Type . Variable . namespace_all_free_variables ~ namespace ) original_left_callable >>= compose let compose_list ~ signature_select = function | [ ] -> None | first :: rest -> List . fold ~ init ( : Some first ) ~ f ( : fun left right -> left >>= fun inner_left -> compose ~ signature_select inner_left right ) rest end end |
let to_defined parameters = Type . Callable . Defined ( List . mapi ~ f ( : fun i element -> Type . Callable . Parameter . PositionalOnly { index = i ; annotation = element ; default = false } ) parameters ) |
let make_overload ~ parameters ~ return = Some { Type . Callable . annotation = return ; parameters = to_defined parameters } |
let test_compose_list _ = let assert_compose_list ( ? aliases = fun _ -> None ) ~ signature_select given expected = let parse source = Type . create ~ aliases ( : fun ? replace_unbound_parameters_with_any : _ -> aliases ) ( parse_single_expression ~ preprocess : true source ) in let merge_option_list = function | [ ] -> Some [ ] | first :: rest -> List . fold_left ~ init ( : first >>| List . return ) ~ f ( : Option . map2 ~ f ( : Fn . flip List . cons ) ) rest >>| List . rev in let get_callable = function | Type . Callable callable -> Some { callable ; self_argument = None } | _ -> None in let annotation_list = List . map ~ f : parse given |> List . map ~ f : get_callable |> merge_option_list in let actual = annotation_list >>= TypeOperation . Compose . compose_list ~ signature_select in assert_equal ~ cmp : ( Option . equal ( fun { callable = left_callable ; self_argument = left_self_argument } { callable = right_callable ; self_argument = right_self_argument } -> [ % eq : Type . t option ] left_self_argument right_self_argument && Type . namespace_insensitive_compare ( Type . Callable left_callable ) ( Type . Callable right_callable ) = 0 ) ) ~ printer [ :% show : callable_and_self_argument option ] actual ( expected >>| fun expected -> { callable = { Type . Callable . kind = Type . Callable . Anonymous ; implementation = expected ; overloads = [ ] ; } ; self_argument = None ; } ) in let constant_select annotation ~ arguments : _ ~ callable : _ ~ self_argument : _ = SignatureSelectionTypes . Found { selected_return_annotation = annotation } in let fail_select ~ arguments : _ ~ callable : _ ~ self_argument : _ = SignatureSelectionTypes . NotFound { closest_return_annotation = Type . Bottom ; reason = None } in assert_compose_list ~ signature_select ( : constant_select Type . bool ) [ " typing . Callable [ [ int ] , str ] " ; " typing . Callable [ [ str ] , bool ] " ] ( make_overload ~ parameters [ : Type . integer ] ~ return : Type . bool ) ; assert_compose_list ~ signature_select : fail_select [ " typing . Callable [ [ int ] , bool ] " ; " typing . Callable [ [ str ] , bool ] " ] None ; let variable = Type . Variable . Unary . create " T " in let variable2 = Type . Variable . Unary . create " R " in let identity_select ~ arguments ~ callable ~ self_argument = match arguments with | [ { AttributeResolution . Argument . resolved ; _ } ] -> SignatureSelectionTypes . Found { selected_return_annotation = resolved } | _ -> fail_select ~ arguments ~ callable ~ self_argument in let aliases = function | " T " -> Some ( Type . Variable variable ) | _ -> None in let aliases = create_type_alias_table aliases in assert_compose_list ~ aliases ~ signature_select : identity_select [ " typing . Callable [ [ int ] , int ] " ; " typing . Callable [ [ T ] , T ] " ] ( make_overload ~ parameters [ : Type . integer ] ~ return : Type . integer ) ; let aliases = function | " T " -> Some ( Type . Variable variable ) | " R " -> Some ( Type . Variable variable2 ) | _ -> None in let aliases = create_type_alias_table aliases in assert_compose_list ~ aliases ~ signature_select : identity_select [ " typing . Callable [ [ R ] , R ] " ; " typing . Callable [ [ T ] , T ] " ] ( make_overload ~ parameters [ : Type . Variable variable2 ] ~ return ( : Type . Variable variable2 ) ) ; assert_compose_list ~ signature_select ( : constant_select Type . integer ) [ " typing . Callable [ [ int ] , int ] " ] ( make_overload ~ parameters [ : Type . integer ] ~ return : Type . integer ) ; assert_compose_list ~ signature_select : identity_select [ ] None ; assert_compose_list ~ aliases ~ signature_select : identity_select [ " typing . Callable [ [ int ] , int ] " ; " typing . Callable [ [ T ] , T ] " ; " typing . Callable [ [ R ] , R ] " ; " typing . Callable [ [ T ] , T ] " ; ] ( make_overload ~ parameters [ : Type . integer ] ~ return : Type . integer ) ; ( ) |
let ( ) = " typeOperation " >::: [ " compose_list " >:: test_compose_list ] |> Test . run |
let scrape_ty env ty = let ty = Ctype . expand_head_opt env ( Ctype . correct_levels ty ) in match ty . desc with | Tconstr ( p , _ , _ ) -> begin match Env . find_type p env with | { type_unboxed = { unboxed = true ; _ } ; _ } -> begin match Typedecl . get_unboxed_type_representation env ty with | None -> ty | Some ty2 -> ty2 end | _ -> ty | exception Not_found -> ty end | _ -> ty |
let scrape env ty = ( scrape_ty env ty ) . desc |
let is_function_type env ty = match scrape env ty with | Tarrow ( _ , lhs , rhs , _ ) -> Some ( lhs , rhs ) | _ -> None |
let is_base_type env ty base_ty_path = match scrape env ty with | Tconstr ( p , _ , _ ) -> Path . same p base_ty_path | _ -> false |
let maybe_pointer_type env ty = let ty = scrape_ty env ty in if Ctype . maybe_pointer_type env ty then Pointer else Immediate |
let maybe_pointer exp = maybe_pointer_type exp . exp_env exp . exp_type |
type classification = | Int | Float | Lazy | Addr | Any |
let classify env ty = let ty = scrape_ty env ty in if maybe_pointer_type env ty = Immediate then Int else match ty . desc with | Tvar _ | Tunivar _ -> Any | Tconstr ( p , _args , _abbrev ) -> if Path . same p Predef . path_float then Float else if Path . same p Predef . path_lazy_t then Lazy else if Path . same p Predef . path_string || Path . same p Predef . path_bytes || Path . same p Predef . path_array || Path . same p Predef . path_nativeint || Path . same p Predef . path_int32 || Path . same p Predef . path_int64 then Addr else begin try match ( Env . find_type p env ) . type_kind with | Type_abstract -> Any | Type_record _ | Type_variant _ | Type_open -> Addr with Not_found -> Any end | Tarrow _ | Ttuple _ | Tpackage _ | Tobject _ | Tnil | Tvariant _ -> Addr | Tlink _ | Tsubst _ | Tpoly _ | Tfield _ -> assert false |
let array_type_kind env ty = match scrape env ty with | Tconstr ( p , [ elt_ty ] , _ ) | Tpoly ( { desc = Tconstr ( p , [ elt_ty ] , _ ) } , _ ) when Path . same p Predef . path_array -> begin match classify env elt_ty with | Any -> if Config . flat_float_array then Pgenarray else Paddrarray | Float -> if Config . flat_float_array then Pfloatarray else Paddrarray | Addr | Lazy -> Paddrarray | Int -> Pintarray end | Tconstr ( p , [ ] , _ ) | Tpoly ( { desc = Tconstr ( p , [ ] , _ ) } , _ ) when Path . same p Predef . path_floatarray -> Pfloatarray | _ -> Pgenarray |
let array_kind exp = array_type_kind exp . exp_env exp . exp_type |
let array_pattern_kind pat = array_type_kind pat . pat_env pat . pat_type |
let bigarray_decode_type env ty tbl dfl = match scrape env ty with | Tconstr ( Pdot ( Pident mod_id , type_name ) , [ ] , _ ) when Ident . name mod_id = " Stdlib__bigarray " -> begin try List . assoc type_name tbl with Not_found -> dfl end | _ -> dfl |
let kind_table = [ " float32_elt " , Pbigarray_float32 ; " float64_elt " , Pbigarray_float64 ; " int8_signed_elt " , Pbigarray_sint8 ; " int8_unsigned_elt " , Pbigarray_uint8 ; " int16_signed_elt " , Pbigarray_sint16 ; " int16_unsigned_elt " , Pbigarray_uint16 ; " int32_elt " , Pbigarray_int32 ; " int64_elt " , Pbigarray_int64 ; " int_elt " , Pbigarray_caml_int ; " nativeint_elt " , Pbigarray_native_int ; " complex32_elt " , Pbigarray_complex32 ; " complex64_elt " , Pbigarray_complex64 ] |
let layout_table = [ " c_layout " , Pbigarray_c_layout ; " fortran_layout " , Pbigarray_fortran_layout ] |
let bigarray_type_kind_and_layout env typ = match scrape env typ with | Tconstr ( _p , [ _caml_type ; elt_type ; layout_type ] , _abbrev ) -> ( bigarray_decode_type env elt_type kind_table Pbigarray_unknown , bigarray_decode_type env layout_type layout_table Pbigarray_unknown_layout ) | _ -> ( Pbigarray_unknown , Pbigarray_unknown_layout ) |
let value_kind env ty = match scrape env ty with | Tconstr ( p , _ , _ ) when Path . same p Predef . path_int -> Pintval | Tconstr ( p , _ , _ ) when Path . same p Predef . path_char -> Pintval | Tconstr ( p , _ , _ ) when Path . same p Predef . path_float -> Pfloatval | Tconstr ( p , _ , _ ) when Path . same p Predef . path_int32 -> Pboxedintval Pint32 | Tconstr ( p , _ , _ ) when Path . same p Predef . path_int64 -> Pboxedintval Pint64 | Tconstr ( p , _ , _ ) when Path . same p Predef . path_nativeint -> Pboxedintval Pnativeint | _ -> Pgenval |
let function_return_value_kind env ty = match is_function_type env ty with | Some ( _lhs , rhs ) -> value_kind env rhs | None -> Pgenval |
let lazy_val_requires_forward env ty = match classify env ty with | Any | Lazy -> true | Float -> Config . flat_float_array | Addr | Int -> false |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.