1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
|
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
(* Operations on module types *)
open Asttypes
open Path
open Types
let rec scrape_lazy env mty =
let open Subst.Lazy in
match mty with
MtyL_ident p ->
begin try
scrape_lazy env (Env.find_modtype_expansion_lazy p env)
with Not_found ->
mty
end
| _ -> mty
let scrape env mty =
match mty with
Mty_ident p ->
Subst.Lazy.force_modtype (scrape_lazy env (MtyL_ident p))
| _ -> mty
let freshen ~scope mty =
Subst.modtype (Rescope scope) Subst.identity mty
let rec strengthen_lazy ~aliasable env mty p =
let open Subst.Lazy in
match scrape_lazy env mty with
MtyL_signature sg ->
MtyL_signature(strengthen_lazy_sig ~aliasable env sg p)
| MtyL_functor(Named (Some param, arg), res)
when !Clflags.applicative_functors ->
let env =
Env.add_module_lazy ~update_summary:false param Mp_present arg env
in
MtyL_functor(Named (Some param, arg),
strengthen_lazy ~aliasable:false env res (Papply(p, Pident param)))
| MtyL_functor(Named (None, arg), res)
when !Clflags.applicative_functors ->
let param = Ident.create_scoped ~scope:(Path.scope p) "Arg" in
MtyL_functor(Named (Some param, arg),
strengthen_lazy ~aliasable:false env res (Papply(p, Pident param)))
| mty ->
mty
and strengthen_lazy_sig' ~aliasable env sg p =
let open Subst.Lazy in
match sg with
[] -> []
| (SigL_value(_, _, _) as sigelt) :: rem ->
sigelt :: strengthen_lazy_sig' ~aliasable env rem p
| SigL_type(id, {type_kind=Type_abstract _}, _, _) :: rem
when Btype.is_row_name (Ident.name id) ->
strengthen_lazy_sig' ~aliasable env rem p
| SigL_type(id, decl, rs, vis) :: rem ->
let newdecl =
match decl.type_manifest, decl.type_private, decl.type_kind with
Some _, Public, _ -> decl
| Some _, Private, (Type_record _ | Type_variant _) -> decl
| _ ->
let manif =
Some(Btype.newgenty(Tconstr(Pdot(p, Ident.name id),
decl.type_params, ref Mnil))) in
if Btype.type_kind_is_abstract decl then
{ decl with type_private = Public; type_manifest = manif }
else
{ decl with type_manifest = manif }
in
SigL_type(id, newdecl, rs, vis) ::
strengthen_lazy_sig' ~aliasable env rem p
| (SigL_typext _ as sigelt) :: rem ->
sigelt :: strengthen_lazy_sig' ~aliasable env rem p
| SigL_module(id, pres, md, rs, vis) :: rem ->
let str =
strengthen_lazy_decl ~aliasable env md (Pdot(p, Ident.name id))
in
let env =
Env.add_module_declaration_lazy ~update_summary:false id pres md env in
SigL_module(id, pres, str, rs, vis)
:: strengthen_lazy_sig' ~aliasable env rem p
(* Need to add the module in case it defines manifest module types *)
| SigL_modtype(id, decl, vis) :: rem ->
let newdecl =
match decl.mtdl_type with
| Some _ when not aliasable ->
(* [not alisable] condition needed because of recursive modules.
See [Typemod.check_recmodule_inclusion]. *)
decl
| _ ->
{decl with mtdl_type = Some(MtyL_ident(Pdot(p,Ident.name id)))}
in
let env = Env.add_modtype_lazy ~update_summary:false id decl env in
SigL_modtype(id, newdecl, vis) ::
strengthen_lazy_sig' ~aliasable env rem p
(* Need to add the module type in case it is manifest *)
| (SigL_class _ as sigelt) :: rem ->
sigelt :: strengthen_lazy_sig' ~aliasable env rem p
| (SigL_class_type _ as sigelt) :: rem ->
sigelt :: strengthen_lazy_sig' ~aliasable env rem p
and strengthen_lazy_sig ~aliasable env sg p =
let sg = Subst.Lazy.force_signature_once sg in
let sg = strengthen_lazy_sig' ~aliasable env sg p in
Subst.Lazy.of_signature_items sg
and strengthen_lazy_decl ~aliasable env md p =
let open Subst.Lazy in
match md.mdl_type with
| MtyL_alias _ -> md
| _ when aliasable -> {md with mdl_type = MtyL_alias p}
| mty -> {md with mdl_type = strengthen_lazy ~aliasable env mty p}
let () = Env.strengthen := strengthen_lazy
let strengthen ~aliasable env mty p =
let mty = strengthen_lazy ~aliasable env (Subst.Lazy.of_modtype mty) p in
Subst.Lazy.force_modtype mty
let strengthen_decl ~aliasable env md p =
let md = strengthen_lazy_decl ~aliasable env
(Subst.Lazy.of_module_decl md) p in
Subst.Lazy.force_module_decl md
let rec make_aliases_absent pres mty =
match mty with
| Mty_alias _ -> Mp_absent, mty
| Mty_signature sg ->
pres, Mty_signature(make_aliases_absent_sig sg)
| Mty_functor(arg, res) ->
let _, res = make_aliases_absent Mp_present res in
pres, Mty_functor(arg, res)
| mty ->
pres, mty
and make_aliases_absent_sig sg =
match sg with
[] -> []
| Sig_module(id, pres, md, rs, priv) :: rem ->
let pres, md_type = make_aliases_absent pres md.md_type in
let md = { md with md_type } in
Sig_module(id, pres, md, rs, priv) :: make_aliases_absent_sig rem
| sigelt :: rem ->
sigelt :: make_aliases_absent_sig rem
let scrape_for_type_of env pres mty =
let rec loop env path mty =
match mty, path with
| Mty_alias path, _ -> begin
try
let md = Env.find_module path env in
loop env (Some path) md.md_type
with Not_found -> mty
end
| mty, Some path ->
strengthen ~aliasable:false env mty path
| _ -> mty
in
make_aliases_absent pres (loop env None mty)
(* In nondep_supertype, env is only used for the type it assigns to id.
Hence there is no need to keep env up-to-date by adding the bindings
traversed. *)
type variance = Co | Contra | Strict
let rec nondep_mty_with_presence env va ids pres mty =
match mty with
Mty_ident p ->
begin match Path.find_free_opt ids p with
| Some id ->
let expansion =
try Env.find_modtype_expansion p env
with Not_found ->
raise (Ctype.Nondep_cannot_erase id)
in
nondep_mty_with_presence env va ids pres expansion
| None -> pres, mty
end
| Mty_alias p ->
begin match Path.find_free_opt ids p with
| Some id ->
let expansion =
try Env.find_module p env
with Not_found ->
raise (Ctype.Nondep_cannot_erase id)
in
nondep_mty_with_presence env va ids Mp_present expansion.md_type
| None -> pres, mty
end
| Mty_signature sg ->
let mty = Mty_signature(nondep_sig env va ids sg) in
pres, mty
| Mty_functor(Unit, res) ->
pres, Mty_functor(Unit, nondep_mty env va ids res)
| Mty_functor(Named (param, arg), res) ->
let var_inv =
match va with Co -> Contra | Contra -> Co | Strict -> Strict in
let res_env =
match param with
| None -> env
| Some param -> Env.add_module ~arg:true param Mp_present arg env
in
let mty =
Mty_functor(Named (param, nondep_mty env var_inv ids arg),
nondep_mty res_env va ids res)
in
pres, mty
and nondep_mty env va ids mty =
snd (nondep_mty_with_presence env va ids Mp_present mty)
and nondep_sig_item env va ids = function
| Sig_value(id, d, vis) ->
Sig_value(id,
{d with val_type = Ctype.nondep_type env ids d.val_type},
vis)
| Sig_type(id, d, rs, vis) ->
Sig_type(id, Ctype.nondep_type_decl env ids (va = Co) d, rs, vis)
| Sig_typext(id, ext, es, vis) ->
Sig_typext(id, Ctype.nondep_extension_constructor env ids ext, es, vis)
| Sig_module(id, pres, md, rs, vis) ->
let pres, mty = nondep_mty_with_presence env va ids pres md.md_type in
Sig_module(id, pres, {md with md_type = mty}, rs, vis)
| Sig_modtype(id, d, vis) ->
Sig_modtype(id, nondep_modtype_decl env ids d, vis)
| Sig_class(id, d, rs, vis) ->
Sig_class(id, Ctype.nondep_class_declaration env ids d, rs, vis)
| Sig_class_type(id, d, rs, vis) ->
Sig_class_type(id, Ctype.nondep_cltype_declaration env ids d, rs, vis)
and nondep_sig env va ids sg =
let scope = Ctype.create_scope () in
let sg, env = Env.enter_signature ~scope sg env in
List.map (nondep_sig_item env va ids) sg
and nondep_modtype_decl env ids mtd =
{mtd with mtd_type = Option.map (nondep_mty env Strict ids) mtd.mtd_type}
let nondep_supertype env ids = nondep_mty env Co ids
let nondep_sig_item env ids = nondep_sig_item env Co ids
let enrich_typedecl env p id decl =
match decl.type_manifest with
Some _ -> decl
| None ->
match Env.find_type p env with
| exception Not_found -> decl
(* Type which was not present in the signature, so we don't have
anything to do. *)
| orig_decl ->
if decl.type_arity <> orig_decl.type_arity then
decl
else begin
let orig_ty =
Ctype.reify_univars env
(Btype.newgenty(Tconstr(p, orig_decl.type_params, ref Mnil)))
in
let new_ty =
Ctype.reify_univars env
(Btype.newgenty(Tconstr(Pident id, decl.type_params, ref Mnil)))
in
let env = Env.add_type ~check:false id decl env in
match Ctype.mcomp env orig_ty new_ty with
| exception Ctype.Incompatible -> decl
(* The current declaration is not compatible with the one we got
from the signature. We should just fail now, but then, we could
also have failed if the arities of the two decls were
different, which we didn't. *)
| () ->
let orig_ty =
Btype.newgenty(Tconstr(p, decl.type_params, ref Mnil))
in
{decl with type_manifest = Some orig_ty}
end
let rec enrich_modtype env p mty =
match mty with
Mty_signature sg ->
Mty_signature(List.map (enrich_item env p) sg)
| _ ->
mty
and enrich_item env p = function
Sig_type(id, decl, rs, priv) ->
Sig_type(id,
enrich_typedecl env (Pdot(p, Ident.name id)) id decl, rs, priv)
| Sig_module(id, pres, md, rs, priv) ->
Sig_module(id, pres,
{md with
md_type = enrich_modtype env
(Pdot(p, Ident.name id)) md.md_type},
rs,
priv)
| item -> item
let rec type_paths env p mty =
match scrape env mty with
Mty_ident _ -> []
| Mty_alias _ -> []
| Mty_signature sg -> type_paths_sig env p sg
| Mty_functor _ -> []
and type_paths_sig env p sg =
match sg with
[] -> []
| Sig_type(id, _decl, _, _) :: rem ->
Pdot(p, Ident.name id) :: type_paths_sig env p rem
| Sig_module(id, pres, md, _, _) :: rem ->
type_paths env (Pdot(p, Ident.name id)) md.md_type @
type_paths_sig (Env.add_module_declaration ~check:false id pres md env)
p rem
| Sig_modtype(id, decl, _) :: rem ->
type_paths_sig (Env.add_modtype id decl env) p rem
| (Sig_value _ | Sig_typext _ | Sig_class _ | Sig_class_type _) :: rem ->
type_paths_sig env p rem
let rec no_code_needed_mod env pres mty =
match pres with
| Mp_absent -> true
| Mp_present -> begin
match scrape env mty with
Mty_ident _ -> false
| Mty_signature sg -> no_code_needed_sig env sg
| Mty_functor _ -> false
| Mty_alias _ -> false
end
and no_code_needed_sig env sg =
match sg with
[] -> true
| Sig_value(_id, decl, _) :: rem ->
begin match decl.val_kind with
| Val_prim _ -> no_code_needed_sig env rem
| _ -> false
end
| Sig_module(id, pres, md, _, _) :: rem ->
no_code_needed_mod env pres md.md_type &&
no_code_needed_sig
(Env.add_module_declaration ~check:false id pres md env) rem
| (Sig_type _ | Sig_modtype _ | Sig_class_type _) :: rem ->
no_code_needed_sig env rem
| (Sig_typext _ | Sig_class _) :: _ ->
false
let no_code_needed env mty = no_code_needed_mod env Mp_present mty
(* Check whether a module type may return types *)
let rec contains_type env = function
Mty_ident path ->
begin try match (Env.find_modtype path env).mtd_type with
| None -> raise Exit (* PR#6427 *)
| Some mty -> contains_type env mty
with Not_found -> raise Exit
end
| Mty_signature sg ->
contains_type_sig env sg
| Mty_functor (_, body) ->
contains_type env body
| Mty_alias _ ->
()
and contains_type_sig env = List.iter (contains_type_item env)
and contains_type_item env = function
Sig_type (_,({type_manifest = None} |
{type_kind = Type_abstract _; type_private = Private}),_, _)
| Sig_modtype _
| Sig_typext (_, {ext_args = Cstr_record _}, _, _) ->
(* We consider that extension constructors with an inlined
record create a type (the inlined record), even though
it would be technically safe to ignore that considering
the current constraints which guarantee that this type
is kept local to expressions. *)
raise Exit
| Sig_module (_, _, {md_type = mty}, _, _) ->
contains_type env mty
| Sig_value _
| Sig_type _
| Sig_typext _
| Sig_class _
| Sig_class_type _ ->
()
let contains_type env mty =
try contains_type env mty; false with Exit -> true
(* Remove module aliases from a signature *)
let rec get_prefixes = function
| Pident _ -> Path.Set.empty
| Pdot (p, _) | Papply (p, _) | Pextra_ty (p, _)
-> Path.Set.add p (get_prefixes p)
let rec get_arg_paths = function
| Pident _ -> Path.Set.empty
| Pdot (p, _) | Pextra_ty (p, _) -> get_arg_paths p
| Papply (p1, p2) ->
Path.Set.add p2
(Path.Set.union (get_prefixes p2)
(Path.Set.union (get_arg_paths p1) (get_arg_paths p2)))
let rec rollback_path subst p =
try Pident (Path.Map.find p subst)
with Not_found ->
match p with
Pident _ | Papply _ -> p
| Pdot (p1, s) ->
let p1' = rollback_path subst p1 in
if Path.same p1 p1' then p else rollback_path subst (Pdot (p1', s))
| Pextra_ty (p1, extra) ->
let p1' = rollback_path subst p1 in
if Path.same p1 p1' then p
else rollback_path subst (Pextra_ty (p1', extra))
let rec collect_ids subst bindings p =
begin match rollback_path subst p with
Pident id ->
let ids =
try collect_ids subst bindings (Ident.find_same id bindings)
with Not_found -> Ident.Set.empty
in
Ident.Set.add id ids
| _ -> Ident.Set.empty
end
let collect_arg_paths mty =
let open Btype in
let paths = ref Path.Set.empty
and subst = ref Path.Map.empty
and bindings = ref Ident.empty in
(* let rt = Ident.create "Root" in
and prefix = ref (Path.Pident rt) in *)
with_type_mark begin fun mark ->
let super = type_iterators mark in
let it_path p = paths := Path.Set.union (get_arg_paths p) !paths
and it_signature_item it si =
super.it_signature_item it si;
match si with
| Sig_module (id, _, {md_type=Mty_alias p}, _, _) ->
bindings := Ident.add id p !bindings
| Sig_module (id, _, {md_type=Mty_signature sg}, _, _) ->
List.iter
(function Sig_module (id', _, _, _, _) ->
subst :=
Path.Map.add (Pdot (Pident id, Ident.name id')) id' !subst
| _ -> ())
sg
| _ -> ()
in
let it = {super with it_path; it_signature_item} in
it.it_module_type it mty;
Path.Set.fold (fun p -> Ident.Set.union (collect_ids !subst !bindings p))
!paths Ident.Set.empty
end
type remove_alias_args =
{ mutable modified: bool;
exclude: Ident.t -> Path.t -> bool;
scrape: Env.t -> module_type -> module_type }
let rec remove_aliases_mty env args pres mty =
let args' = {args with modified = false} in
let res =
match args.scrape env mty with
Mty_signature sg ->
Mp_present, Mty_signature (remove_aliases_sig env args' sg)
| Mty_alias _ ->
let mty' = Env.scrape_alias env mty in
if mty' = mty then begin
pres, mty
end else begin
args'.modified <- true;
remove_aliases_mty env args' Mp_present mty'
end
| mty ->
Mp_present, mty
in
if args'.modified then begin
args.modified <- true;
res
end else begin
pres, mty
end
and remove_aliases_sig env args sg =
match sg with
[] -> []
| Sig_module(id, pres, md, rs, priv) :: rem ->
let pres, mty =
match md.md_type with
Mty_alias p when args.exclude id p ->
pres, md.md_type
| mty ->
remove_aliases_mty env args pres mty
in
Sig_module(id, pres, {md with md_type = mty} , rs, priv) ::
remove_aliases_sig (Env.add_module id pres mty env) args rem
| Sig_modtype(id, mtd, priv) :: rem ->
Sig_modtype(id, mtd, priv) ::
remove_aliases_sig (Env.add_modtype id mtd env) args rem
| it :: rem ->
it :: remove_aliases_sig env args rem
let scrape_for_functor_arg env mty =
let exclude _id p =
try ignore (Env.find_module p env); true with Not_found -> false
in
let _, mty =
remove_aliases_mty env {modified=false; exclude; scrape} Mp_present mty
in
mty
let scrape_for_type_of ~remove_aliases env mty =
if remove_aliases then begin
let excl = collect_arg_paths mty in
let exclude id _p = Ident.Set.mem id excl in
let scrape _ mty = mty in
let _, mty =
remove_aliases_mty env {modified=false; exclude; scrape} Mp_present mty
in
mty
end else begin
let _, mty = scrape_for_type_of env Mp_present mty in
mty
end
(* Lower non-generalizable type variables *)
let lower_nongen nglev mty =
let open Btype in
with_type_mark begin fun mark ->
let super = type_iterators mark in
let it_do_type_expr it ty =
match get_desc ty with
Tvar _ ->
let level = get_level ty in
if level < generic_level && level > nglev then set_level ty nglev
| _ ->
super.it_do_type_expr it ty
in
let it = {super with it_do_type_expr} in
it.it_module_type it mty
end
|