| 1 |
open Base |
| 2 |
open Ppxlib |
| 3 |
open Ast_builder.Default |
| 4 |
|
| 5 |
module Attrs = struct |
| 6 |
let ignore_label_declaration = |
| 7 |
Attribute.declare |
| 8 |
"hash.ignore" |
| 9 |
Attribute.Context.label_declaration |
| 10 |
Ast_pattern.(pstr nil) |
| 11 |
() |
| 12 |
;; |
| 13 |
|
| 14 |
let ignore_core_type = |
| 15 |
Attribute.declare "hash.ignore" Attribute.Context.core_type Ast_pattern.(pstr nil) () |
| 16 |
;; |
| 17 |
|
| 18 |
let no_hashing_label_declaration = |
| 19 |
Attribute.declare |
| 20 |
"hash.no_hashing" |
| 21 |
Attribute.Context.label_declaration |
| 22 |
Ast_pattern.(pstr nil) |
| 23 |
() |
| 24 |
;; |
| 25 |
end |
| 26 |
|
| 27 |
let str_attributes = |
| 28 |
[ Attribute.T Attrs.ignore_core_type |
| 29 |
; Attribute.T Attrs.ignore_label_declaration |
| 30 |
; Attribute.T Attrs.no_hashing_label_declaration |
| 31 |
] |
| 32 |
;; |
| 33 |
|
| 34 |
let is_ignored_gen attrs t = |
| 35 |
List.exists attrs ~f:(fun attr -> Option.is_some (Attribute.get attr t)) |
| 36 |
;; |
| 37 |
|
| 38 |
let core_type_is_ignored ct = |
| 39 |
is_ignored_gen |
| 40 |
[ Attrs.ignore_core_type; Ppx_compare_expander.Compare.Attrs.ignore_core_type ] |
| 41 |
ct |
| 42 |
;; |
| 43 |
|
| 44 |
let should_ignore_label_declaration ld = |
| 45 |
let warning = "[@hash.no_hashing] is deprecated. Use [@hash.ignore]." in |
| 46 |
let is_ignored = |
| 47 |
is_ignored_gen |
| 48 |
[ Attrs.ignore_label_declaration |
| 49 |
; Ppx_compare_expander.Compare.Attrs.ignore_label_declaration |
| 50 |
] |
| 51 |
ld |
| 52 |
|
| 53 |
|
| 54 |
|| core_type_is_ignored ld.pld_type |
| 55 |
in |
| 56 |
match Attribute.get Attrs.no_hashing_label_declaration ld with |
| 57 |
| None -> (if is_ignored then `ignore else `incorporate), None |
| 58 |
| Some () -> `ignore, Some (attribute_of_warning ld.pld_loc warning) |
| 59 |
;; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
let hash_state_t ~loc = [%type: Ppx_hash_lib.Std.Hash.state] |
| 66 |
|
| 67 |
let hash_fold_type ~loc ty = |
| 68 |
let loc = { loc with loc_ghost = true } in |
| 69 |
[%type: [%t hash_state_t ~loc] -> [%t ty] -> [%t hash_state_t ~loc]] |
| 70 |
;; |
| 71 |
|
| 72 |
let hash_type ~loc ty = |
| 73 |
let loc = { loc with loc_ghost = true } in |
| 74 |
[%type: [%t ty] -> Ppx_hash_lib.Std.Hash.hash_value] |
| 75 |
;; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
type expr = expression |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
module Hsv_expr : sig |
| 86 |
type t |
| 87 |
|
| 88 |
val identity : loc:location -> t |
| 89 |
val invoke_hash_fold_t : loc:location -> hash_fold_t:expr -> t:expr -> t |
| 90 |
val compose : loc:location -> t -> t -> t |
| 91 |
val compile_error : loc:location -> string -> t |
| 92 |
|
| 93 |
|
| 94 |
val of_expression_unchecked : expr -> t |
| 95 |
|
| 96 |
|
| 97 |
val to_expression : loc:location -> t -> pattern * expression |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
type case |
| 102 |
|
| 103 |
val compile_error_case : loc:location -> string -> case |
| 104 |
val pexp_match : loc:location -> expr -> case list -> t |
| 105 |
|
| 106 |
|
| 107 |
val case : lhs:pattern -> guard:expr option -> rhs:t -> case |
| 108 |
|
| 109 |
|
| 110 |
val pexp_let : loc:location -> rec_flag -> value_binding list -> t -> t |
| 111 |
val with_attributes : f:(attribute list -> attribute list) -> t -> t |
| 112 |
end = struct |
| 113 |
type t = expression |
| 114 |
type nonrec case = case |
| 115 |
|
| 116 |
let invoke_hash_fold_t ~loc ~hash_fold_t ~t = eapply ~loc hash_fold_t [ [%expr hsv]; t ] |
| 117 |
let identity ~loc = [%expr hsv] |
| 118 |
|
| 119 |
let compose ~loc a b = |
| 120 |
[%expr |
| 121 |
let hsv = [%e a] in |
| 122 |
[%e b]] |
| 123 |
;; |
| 124 |
|
| 125 |
let to_expression ~loc x = [%pat? hsv], x |
| 126 |
let of_expression_unchecked x = x |
| 127 |
let pexp_match = pexp_match |
| 128 |
let case = case |
| 129 |
let pexp_let = pexp_let |
| 130 |
let with_attributes ~f x = { x with pexp_attributes = f x.pexp_attributes } |
| 131 |
|
| 132 |
let compile_error ~loc s = |
| 133 |
pexp_extension ~loc (Location.Error.to_extension (Location.Error.createf ~loc "%s" s)) |
| 134 |
;; |
| 135 |
|
| 136 |
let compile_error_case ~loc s = |
| 137 |
case ~lhs:(ppat_any ~loc) ~guard:None ~rhs:(compile_error ~loc s) |
| 138 |
;; |
| 139 |
end |
| 140 |
|
| 141 |
let hash_fold_int_expr ~loc i_expr : Hsv_expr.t = |
| 142 |
Hsv_expr.invoke_hash_fold_t |
| 143 |
~loc |
| 144 |
~hash_fold_t:[%expr Ppx_hash_lib.Std.Hash.fold_int] |
| 145 |
~t:i_expr |
| 146 |
;; |
| 147 |
|
| 148 |
let hash_fold_int ~loc i : Hsv_expr.t = hash_fold_int_expr ~loc (eint ~loc i) |
| 149 |
|
| 150 |
let special_case_types_named_t = function |
| 151 |
| `hash_fold -> false |
| 152 |
| `hash -> true |
| 153 |
;; |
| 154 |
|
| 155 |
let hash_fold_ tn = |
| 156 |
match tn with |
| 157 |
| "t" when special_case_types_named_t `hash_fold -> "hash_fold" |
| 158 |
| _ -> "hash_fold_" ^ tn |
| 159 |
;; |
| 160 |
|
| 161 |
let hash_ tn = |
| 162 |
match tn with |
| 163 |
| "t" when special_case_types_named_t `hash -> "hash" |
| 164 |
| _ -> "hash_" ^ tn |
| 165 |
;; |
| 166 |
|
| 167 |
|
| 168 |
let rigid_type_var ~type_name x = |
| 169 |
let prefix = "rigid_" in |
| 170 |
if String.equal x type_name || String.is_prefix x ~prefix |
| 171 |
then prefix ^ x ^ "_of_type_" ^ type_name |
| 172 |
else x |
| 173 |
;; |
| 174 |
|
| 175 |
let make_type_rigid ~type_name = |
| 176 |
let map = |
| 177 |
object |
| 178 |
inherit Ast_traverse.map as super |
| 179 |
|
| 180 |
method! core_type ty = |
| 181 |
let ptyp_desc = |
| 182 |
let () = |
| 183 |
|
| 184 |
match ty.ptyp_desc with |
| 185 |
| Ptyp_constr (name, _args) -> |
| 186 |
(match name.txt with |
| 187 |
| Ldot _ | Lapply _ -> () |
| 188 |
| Lident name -> |
| 189 |
if not (String.equal name type_name) |
| 190 |
then |
| 191 |
Location.raise_errorf |
| 192 |
~loc:ty.ptyp_loc |
| 193 |
"ppx_hash: make_type_rigid: unexpected type %S. expected to only \ |
| 194 |
find %S" |
| 195 |
(string_of_core_type ty) |
| 196 |
type_name; |
| 197 |
()) |
| 198 |
| _ -> () |
| 199 |
in |
| 200 |
match ty.ptyp_desc with |
| 201 |
| Ptyp_var s -> |
| 202 |
Ptyp_constr (Located.lident ~loc:ty.ptyp_loc (rigid_type_var ~type_name s), []) |
| 203 |
| desc -> super#core_type_desc desc |
| 204 |
in |
| 205 |
{ ty with ptyp_desc } |
| 206 |
end |
| 207 |
in |
| 208 |
map#core_type |
| 209 |
;; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
let tp_name n = Printf.sprintf "_hash_fold_%s" n |
| 214 |
|
| 215 |
let with_tuple loc (value : expr) xs (f : (expr * core_type) list -> Hsv_expr.t) |
| 216 |
: Hsv_expr.t |
| 217 |
= |
| 218 |
let names = List.mapi ~f:(fun i lt -> Printf.sprintf "e%d" i, lt) xs in |
| 219 |
let pattern = |
| 220 |
let l = List.map ~f:(fun (n, (lbl, _)) -> lbl, pvar ~loc n) names in |
| 221 |
Ppxlib_jane.Jane_syntax.Pattern.pat_of ~loc ~attrs:[] (Jpat_tuple (l, Closed)) |
| 222 |
in |
| 223 |
let e = f (List.map ~f:(fun (n, (_lbl, t)) -> evar ~loc n, t) names) in |
| 224 |
let binding = value_binding ~loc ~pat:pattern ~expr:value in |
| 225 |
Hsv_expr.pexp_let ~loc Nonrecursive [ binding ] e |
| 226 |
;; |
| 227 |
|
| 228 |
let hash_ignore ~loc value = |
| 229 |
Hsv_expr.pexp_let |
| 230 |
~loc |
| 231 |
Nonrecursive |
| 232 |
[ value_binding ~loc ~pat:[%pat? _] ~expr:value ] |
| 233 |
(Hsv_expr.identity ~loc) |
| 234 |
;; |
| 235 |
|
| 236 |
let ghostify_located (t : 'a loc) : 'a loc = |
| 237 |
{ t with loc = { t.loc with loc_ghost = true } } |
| 238 |
;; |
| 239 |
|
| 240 |
let rec hash_applied ty value = |
| 241 |
let loc = { ty.ptyp_loc with loc_ghost = true } in |
| 242 |
match ty.ptyp_desc with |
| 243 |
| Ptyp_constr (name, ta) -> |
| 244 |
let args = List.map ta ~f:(hash_fold_of_ty_fun ~type_constraint:false) in |
| 245 |
Hsv_expr.invoke_hash_fold_t |
| 246 |
~loc |
| 247 |
~hash_fold_t:(type_constr_conv ~loc name ~f:hash_fold_ args) |
| 248 |
~t:value |
| 249 |
| _ -> assert false |
| 250 |
|
| 251 |
and hash_fold_of_tuple ~loc tys value = |
| 252 |
with_tuple loc value tys (fun elems1 -> |
| 253 |
List.fold_right |
| 254 |
elems1 |
| 255 |
~init:(Hsv_expr.identity ~loc) |
| 256 |
~f:(fun (v, t) (result : Hsv_expr.t) -> |
| 257 |
Hsv_expr.compose ~loc (hash_fold_of_ty t v) result)) |
| 258 |
|
| 259 |
and hash_variant ~loc row_fields value = |
| 260 |
let map row = |
| 261 |
match row.prf_desc with |
| 262 |
| Rtag ({ txt = cnstr; _ }, true, _) | Rtag ({ txt = cnstr; _ }, _, []) -> |
| 263 |
Hsv_expr.case |
| 264 |
~guard:None |
| 265 |
~lhs:(ppat_variant ~loc cnstr None) |
| 266 |
~rhs:(hash_fold_int ~loc (Ocaml_common.Btype.hash_variant cnstr)) |
| 267 |
| Rtag ({ txt = cnstr; _ }, false, tp :: _) -> |
| 268 |
let v = "_v" in |
| 269 |
let body = |
| 270 |
Hsv_expr.compose |
| 271 |
~loc |
| 272 |
(hash_fold_int ~loc (Ocaml_common.Btype.hash_variant cnstr)) |
| 273 |
(hash_fold_of_ty tp (evar ~loc v)) |
| 274 |
in |
| 275 |
Hsv_expr.case |
| 276 |
~guard:None |
| 277 |
~lhs:(ppat_variant ~loc cnstr (Some (pvar ~loc v))) |
| 278 |
~rhs:body |
| 279 |
| Rinherit ({ ptyp_desc = Ptyp_constr (id, _); _ } as ty) -> |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
let v = "_v" in |
| 285 |
Hsv_expr.case |
| 286 |
~guard:None |
| 287 |
~lhs:(ppat_alias ~loc (ppat_type ~loc (ghostify_located id)) (Located.mk ~loc v)) |
| 288 |
~rhs:(hash_applied ty (evar ~loc v)) |
| 289 |
| Rinherit ty -> |
| 290 |
let s = string_of_core_type ty in |
| 291 |
Hsv_expr.compile_error_case |
| 292 |
~loc |
| 293 |
(Printf.sprintf "ppx_hash: impossible variant case: %s" s) |
| 294 |
in |
| 295 |
Hsv_expr.pexp_match ~loc value (List.map ~f:map row_fields) |
| 296 |
|
| 297 |
and branch_of_sum hsv ~loc cd = |
| 298 |
match cd.pcd_args with |
| 299 |
| Pcstr_tuple [] -> |
| 300 |
let pcnstr = pconstruct cd None in |
| 301 |
Hsv_expr.case ~guard:None ~lhs:pcnstr ~rhs:hsv |
| 302 |
| Pcstr_tuple tps -> |
| 303 |
let ids_ty = List.mapi tps ~f:(fun i ty -> Printf.sprintf "_a%d" i, ty) in |
| 304 |
let lpatt = List.map ids_ty ~f:(fun (l, _ty) -> pvar ~loc l) |> ppat_tuple ~loc |
| 305 |
and body = |
| 306 |
List.fold_left ids_ty ~init:(Hsv_expr.identity ~loc) ~f:(fun expr (l, ty) -> |
| 307 |
Hsv_expr.compose ~loc expr (hash_fold_of_ty ty (evar ~loc l))) |
| 308 |
in |
| 309 |
Hsv_expr.case |
| 310 |
~guard:None |
| 311 |
~lhs:(pconstruct cd (Some lpatt)) |
| 312 |
~rhs:(Hsv_expr.compose ~loc hsv body) |
| 313 |
| Pcstr_record lds -> |
| 314 |
let arg = "_ir" in |
| 315 |
let pat = pvar ~loc arg in |
| 316 |
let v = evar ~loc arg in |
| 317 |
let body = hash_fold_of_record ~loc lds v in |
| 318 |
Hsv_expr.case |
| 319 |
~guard:None |
| 320 |
~lhs:(pconstruct cd (Some pat)) |
| 321 |
~rhs:(Hsv_expr.compose ~loc hsv body) |
| 322 |
|
| 323 |
and branches_of_sum = function |
| 324 |
| [ cd ] -> |
| 325 |
|
| 326 |
|
| 327 |
let loc = cd.pcd_loc in |
| 328 |
[ branch_of_sum (Hsv_expr.identity ~loc) ~loc cd ] |
| 329 |
| cds -> |
| 330 |
List.mapi cds ~f:(fun i cd -> |
| 331 |
let loc = cd.pcd_loc in |
| 332 |
let hsv = hash_fold_int ~loc i in |
| 333 |
branch_of_sum hsv ~loc cd) |
| 334 |
|
| 335 |
and hash_sum_special_case_for_enums ~loc cds value = |
| 336 |
let cd_has_payload (cd : constructor_declaration) = |
| 337 |
match cd.pcd_args with |
| 338 |
| Pcstr_tuple [] -> false |
| 339 |
| _ -> true |
| 340 |
in |
| 341 |
match cds with |
| 342 |
| [ _ ] -> |
| 343 |
|
| 344 |
|
| 345 |
None |
| 346 |
| _ -> |
| 347 |
(match List.exists cds ~f:(fun cd -> cd_has_payload cd) with |
| 348 |
| true -> None |
| 349 |
| false -> |
| 350 |
Some |
| 351 |
(let tag_expr = |
| 352 |
pexp_match |
| 353 |
~loc |
| 354 |
value |
| 355 |
(List.mapi cds ~f:(fun i cd -> |
| 356 |
let loc = cd.pcd_loc in |
| 357 |
let tag = eint ~loc i in |
| 358 |
let pcnstr = pconstruct cd None in |
| 359 |
case ~guard:None ~lhs:pcnstr ~rhs:tag)) |
| 360 |
in |
| 361 |
hash_fold_int_expr ~loc tag_expr)) |
| 362 |
|
| 363 |
and hash_sum ~loc cds value = |
| 364 |
match hash_sum_special_case_for_enums ~loc cds value with |
| 365 |
| Some v -> v |
| 366 |
| None -> Hsv_expr.pexp_match ~loc value (branches_of_sum cds) |
| 367 |
|
| 368 |
and hash_fold_of_ty ty value = |
| 369 |
let loc = { ty.ptyp_loc with loc_ghost = true } in |
| 370 |
if core_type_is_ignored ty |
| 371 |
then hash_ignore ~loc value |
| 372 |
else ( |
| 373 |
match Ppxlib_jane.Jane_syntax.Core_type.of_ast ty with |
| 374 |
| Some (Jtyp_tuple fields, _attrs) -> hash_fold_of_tuple ~loc fields value |
| 375 |
| Some (Jtyp_layout _, _) | None -> |
| 376 |
(match ty.ptyp_desc with |
| 377 |
| Ptyp_constr _ -> hash_applied ty value |
| 378 |
| Ptyp_tuple tys -> |
| 379 |
hash_fold_of_tuple ~loc (List.map ~f:(fun t -> None, t) tys) value |
| 380 |
| Ptyp_var name -> |
| 381 |
Hsv_expr.invoke_hash_fold_t ~loc ~hash_fold_t:(evar ~loc (tp_name name)) ~t:value |
| 382 |
| Ptyp_arrow _ -> |
| 383 |
Hsv_expr.compile_error ~loc "ppx_hash: functions can not be hashed." |
| 384 |
| Ptyp_variant (row_fields, Closed, _) -> hash_variant ~loc row_fields value |
| 385 |
| _ -> |
| 386 |
let s = string_of_core_type ty in |
| 387 |
Hsv_expr.compile_error ~loc (Printf.sprintf "ppx_hash: unsupported type: %s" s))) |
| 388 |
|
| 389 |
and hash_fold_of_ty_fun ~type_constraint ty = |
| 390 |
let loc = { ty.ptyp_loc with loc_ghost = true } in |
| 391 |
let arg = "arg" in |
| 392 |
let maybe_constrained_arg = |
| 393 |
if type_constraint then ppat_constraint ~loc (pvar ~loc arg) ty else pvar ~loc arg |
| 394 |
in |
| 395 |
let hsv_pat, hsv_expr = |
| 396 |
Hsv_expr.to_expression ~loc (hash_fold_of_ty ty (evar ~loc arg)) |
| 397 |
in |
| 398 |
eta_reduce_if_possible |
| 399 |
[%expr fun [%p hsv_pat] [%p maybe_constrained_arg] -> [%e hsv_expr]] |
| 400 |
|
| 401 |
and hash_fold_of_record ~loc lds value = |
| 402 |
let is_evar = function |
| 403 |
| { pexp_desc = Pexp_ident _; _ } -> true |
| 404 |
| _ -> false |
| 405 |
in |
| 406 |
assert (is_evar value); |
| 407 |
List.fold_left lds ~init:(Hsv_expr.identity ~loc) ~f:(fun hsv ld -> |
| 408 |
Hsv_expr.compose |
| 409 |
~loc |
| 410 |
hsv |
| 411 |
(let loc = ld.pld_loc in |
| 412 |
let label = Located.map lident ld.pld_name in |
| 413 |
let should_ignore, should_warn = should_ignore_label_declaration ld in |
| 414 |
let field_handling = |
| 415 |
match ld.pld_mutable, should_ignore with |
| 416 |
| Mutable, `incorporate -> |
| 417 |
`error "require [@hash.ignore] or [@compare.ignore] on mutable record field" |
| 418 |
| (Mutable | Immutable), `ignore -> `ignore |
| 419 |
| Immutable, `incorporate -> `incorporate |
| 420 |
in |
| 421 |
let hsv = |
| 422 |
match field_handling with |
| 423 |
| `error s -> Hsv_expr.compile_error ~loc (Printf.sprintf "ppx_hash: %s" s) |
| 424 |
| `incorporate -> hash_fold_of_ty ld.pld_type (pexp_field ~loc value label) |
| 425 |
| `ignore -> Hsv_expr.identity ~loc |
| 426 |
in |
| 427 |
match should_warn with |
| 428 |
| None -> hsv |
| 429 |
| Some attribute -> |
| 430 |
Hsv_expr.with_attributes ~f:(fun attributes -> attribute :: attributes) hsv)) |
| 431 |
;; |
| 432 |
|
| 433 |
let hash_fold_of_abstract ~loc type_name value = |
| 434 |
let str = |
| 435 |
Printf.sprintf |
| 436 |
"hash called on the type %s, which is abstract in an implementation." |
| 437 |
type_name |
| 438 |
in |
| 439 |
Hsv_expr.of_expression_unchecked |
| 440 |
[%expr |
| 441 |
let _ = hsv in |
| 442 |
let _ = [%e value] in |
| 443 |
failwith [%e estring ~loc str]] |
| 444 |
;; |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
let eta_expand ~loc f = |
| 450 |
[%expr |
| 451 |
let func = [%e f] in |
| 452 |
fun x -> func x] |
| 453 |
;; |
| 454 |
|
| 455 |
let recognize_simple_type ty = |
| 456 |
match ty.ptyp_desc with |
| 457 |
| Ptyp_constr (lident, []) -> Some lident |
| 458 |
| _ -> None |
| 459 |
;; |
| 460 |
|
| 461 |
let hash_of_ty_fun ~special_case_simple_types ~type_constraint ty = |
| 462 |
let loc = { ty.ptyp_loc with loc_ghost = true } in |
| 463 |
let arg = "arg" in |
| 464 |
let maybe_constrained_arg = |
| 465 |
if type_constraint then ppat_constraint ~loc (pvar ~loc arg) ty else pvar ~loc arg |
| 466 |
in |
| 467 |
match recognize_simple_type ty with |
| 468 |
| Some lident when special_case_simple_types -> |
| 469 |
unapplied_type_constr_conv ~loc lident ~f:hash_ |
| 470 |
| _ -> |
| 471 |
let hsv_pat, hsv_expr = |
| 472 |
Hsv_expr.to_expression ~loc (hash_fold_of_ty ty (evar ~loc arg)) |
| 473 |
in |
| 474 |
[%expr |
| 475 |
fun [%p maybe_constrained_arg] -> |
| 476 |
Ppx_hash_lib.Std.Hash.get_hash_value |
| 477 |
(let [%p hsv_pat] = Ppx_hash_lib.Std.Hash.create () in |
| 478 |
[%e hsv_expr])] |
| 479 |
;; |
| 480 |
|
| 481 |
let hash_structure_item_of_td td = |
| 482 |
let loc = td.ptype_loc in |
| 483 |
match td.ptype_params with |
| 484 |
| _ :: _ -> [] |
| 485 |
| [] -> |
| 486 |
[ (let bnd = pvar ~loc (hash_ td.ptype_name.txt) in |
| 487 |
let typ = combinator_type_of_type_declaration td ~f:hash_type in |
| 488 |
let pat = ppat_constraint ~loc bnd typ in |
| 489 |
let expected_scope, expr = |
| 490 |
let is_simple_type ty = |
| 491 |
match recognize_simple_type ty with |
| 492 |
| Some _ -> true |
| 493 |
| None -> false |
| 494 |
in |
| 495 |
match td.ptype_kind, td.ptype_manifest with |
| 496 |
| Ptype_abstract, Some ty when is_simple_type ty -> |
| 497 |
( `uses_rhs |
| 498 |
, hash_of_ty_fun ~special_case_simple_types:true ~type_constraint:false ty ) |
| 499 |
| _ -> |
| 500 |
( `uses_hash_fold_t_being_defined |
| 501 |
, hash_of_ty_fun |
| 502 |
~special_case_simple_types:false |
| 503 |
~type_constraint:false |
| 504 |
{ ptyp_loc = loc |
| 505 |
; ptyp_loc_stack = [] |
| 506 |
; ptyp_attributes = [] |
| 507 |
; ptyp_desc = Ptyp_constr ({ loc; txt = Lident td.ptype_name.txt }, []) |
| 508 |
} ) |
| 509 |
in |
| 510 |
expected_scope, value_binding ~loc ~pat ~expr:(eta_expand ~loc expr)) |
| 511 |
] |
| 512 |
;; |
| 513 |
|
| 514 |
let hash_fold_structure_item_of_td td ~rec_flag = |
| 515 |
let loc = { td.ptype_loc with loc_ghost = true } in |
| 516 |
let arg = "arg" in |
| 517 |
let body = |
| 518 |
let v = evar ~loc arg in |
| 519 |
match td.ptype_kind with |
| 520 |
| Ptype_variant cds -> hash_sum ~loc cds v |
| 521 |
| Ptype_record lds -> hash_fold_of_record ~loc lds v |
| 522 |
| Ptype_open -> Hsv_expr.compile_error ~loc "ppx_hash: open types are not supported" |
| 523 |
| Ptype_abstract -> |
| 524 |
(match td.ptype_manifest with |
| 525 |
| None -> hash_fold_of_abstract ~loc td.ptype_name.txt v |
| 526 |
| Some ty -> |
| 527 |
(match ty.ptyp_desc with |
| 528 |
| Ptyp_variant (_, Open, _) | Ptyp_variant (_, Closed, Some (_ :: _)) -> |
| 529 |
Hsv_expr.compile_error |
| 530 |
~loc:ty.ptyp_loc |
| 531 |
"ppx_hash: cannot hash open polymorphic variant types" |
| 532 |
| Ptyp_variant (row_fields, _, _) -> hash_variant ~loc row_fields v |
| 533 |
| _ -> hash_fold_of_ty ty v)) |
| 534 |
in |
| 535 |
let vars = List.map td.ptype_params ~f:(fun p -> get_type_param_name p) in |
| 536 |
let extra_names = List.map vars ~f:(fun x -> tp_name x.txt) in |
| 537 |
let hsv_pat, hsv_expr = Hsv_expr.to_expression ~loc body in |
| 538 |
let patts = List.map extra_names ~f:(pvar ~loc) @ [ hsv_pat; pvar ~loc arg ] in |
| 539 |
let bnd = pvar ~loc (hash_fold_ td.ptype_name.txt) in |
| 540 |
let scheme = combinator_type_of_type_declaration td ~f:hash_fold_type in |
| 541 |
let pat = ppat_constraint ~loc bnd (ptyp_poly ~loc vars scheme) in |
| 542 |
let expr = |
| 543 |
eta_reduce_if_possible_and_nonrec ~rec_flag (eabstract ~loc patts hsv_expr) |
| 544 |
in |
| 545 |
let use_rigid_variables = |
| 546 |
match td.ptype_kind with |
| 547 |
| Ptype_variant _ -> true |
| 548 |
| _ -> false |
| 549 |
in |
| 550 |
let expr = |
| 551 |
if use_rigid_variables |
| 552 |
then ( |
| 553 |
let type_name = td.ptype_name.txt in |
| 554 |
List.fold_right |
| 555 |
vars |
| 556 |
~f:(fun s -> |
| 557 |
pexp_newtype ~loc { txt = rigid_type_var ~type_name s.txt; loc = s.loc }) |
| 558 |
~init:(pexp_constraint ~loc expr (make_type_rigid ~type_name scheme))) |
| 559 |
else expr |
| 560 |
in |
| 561 |
value_binding ~loc ~pat ~expr |
| 562 |
;; |
| 563 |
|
| 564 |
let pstr_value ~loc rec_flag bindings = |
| 565 |
match bindings with |
| 566 |
| [] -> [] |
| 567 |
| nonempty_bindings -> |
| 568 |
|
| 569 |
[ pstr_value ~loc rec_flag nonempty_bindings ] |
| 570 |
;; |
| 571 |
|
| 572 |
let str_type_decl ~loc ~path:_ (rec_flag, tds) = |
| 573 |
let tds = List.map tds ~f:name_type_params_in_td in |
| 574 |
let rec_flag = |
| 575 |
(object |
| 576 |
inherit type_is_recursive rec_flag tds as super |
| 577 |
|
| 578 |
method! label_declaration ld = |
| 579 |
match fst (should_ignore_label_declaration ld) with |
| 580 |
| `ignore -> () |
| 581 |
| `incorporate -> super#label_declaration ld |
| 582 |
|
| 583 |
method! core_type ty = if core_type_is_ignored ty then () else super#core_type ty |
| 584 |
end) |
| 585 |
#go |
| 586 |
() |
| 587 |
in |
| 588 |
let hash_fold_bindings = List.map ~f:(hash_fold_structure_item_of_td ~rec_flag) tds in |
| 589 |
let hash_bindings = List.concat (List.map ~f:hash_structure_item_of_td tds) in |
| 590 |
match rec_flag with |
| 591 |
| Recursive -> |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
pstr_value ~loc Recursive (hash_fold_bindings @ List.map ~f:snd hash_bindings) |
| 598 |
| Nonrecursive -> |
| 599 |
let rely_on_hash_fold_t, use_rhs = |
| 600 |
List.partition_map hash_bindings ~f:(function |
| 601 |
| `uses_hash_fold_t_being_defined, binding -> First binding |
| 602 |
| `uses_rhs, binding -> Second binding) |
| 603 |
in |
| 604 |
pstr_value ~loc Nonrecursive (hash_fold_bindings @ use_rhs) |
| 605 |
@ pstr_value ~loc Nonrecursive rely_on_hash_fold_t |
| 606 |
;; |
| 607 |
|
| 608 |
let mk_sig ~loc:_ ~path:_ (_rec_flag, tds) = |
| 609 |
List.concat |
| 610 |
(List.map tds ~f:(fun td -> |
| 611 |
let monomorphic = List.is_empty td.ptype_params in |
| 612 |
let definition ~f_type ~f_name = |
| 613 |
let type_ = combinator_type_of_type_declaration td ~f:f_type in |
| 614 |
let name = |
| 615 |
let tn = td.ptype_name.txt in |
| 616 |
f_name tn |
| 617 |
in |
| 618 |
let loc = td.ptype_loc in |
| 619 |
psig_value |
| 620 |
~loc |
| 621 |
(value_description |
| 622 |
~loc |
| 623 |
~name:{ td.ptype_name with txt = name } |
| 624 |
~type_ |
| 625 |
~prim:[]) |
| 626 |
in |
| 627 |
List.concat |
| 628 |
[ [ definition ~f_type:hash_fold_type ~f_name:hash_fold_ ] |
| 629 |
; (if monomorphic then [ definition ~f_type:hash_type ~f_name:hash_ ] else []) |
| 630 |
])) |
| 631 |
;; |
| 632 |
|
| 633 |
let sig_type_decl ~loc ~path (rec_flag, tds) = |
| 634 |
match |
| 635 |
mk_named_sig |
| 636 |
~loc |
| 637 |
~sg_name:"Ppx_hash_lib.Hashable.S" |
| 638 |
~handle_polymorphic_variant:true |
| 639 |
tds |
| 640 |
with |
| 641 |
| Some include_info -> [ psig_include ~loc include_info ] |
| 642 |
| None -> mk_sig ~loc ~path (rec_flag, tds) |
| 643 |
;; |
| 644 |
|
| 645 |
let hash_fold_core_type ty = hash_fold_of_ty_fun ~type_constraint:true ty |
| 646 |
|
| 647 |
let hash_core_type ty = |
| 648 |
hash_of_ty_fun ~special_case_simple_types:true ~type_constraint:true ty |
| 649 |
;; |