@@ -349,6 +349,13 @@ type response = {
349
349
no_inline_cross_module : bool ;
350
350
}
351
351
352
+ let get_maybe_obj_field_alias (attributes ) =
353
+ attributes |> List. find_map (fun (attr : Parsetree.attribute ) -> match attr with
354
+ | ({txt = "as" ; _} , PStr [ { pstr_desc = Pstr_eval ({ pexp_desc = Pexp_constant (Pconst_string (alias , _ )); _} , _ ); _ } ]) -> Some (alias)
355
+ | _ -> None
356
+ )
357
+
358
+
352
359
let process_obj (loc : Location.t ) (st : external_desc ) (prim_name : string )
353
360
(arg_types_ty : Ast_core_type.param_type list )
354
361
(result_type : Ast_core_type.t ) : Parsetree. core_type * External_ffi_types. t
@@ -398,7 +405,11 @@ let process_obj (loc : Location.t) (st : external_desc) (prim_name : string)
398
405
| _ ->
399
406
Location. raise_errorf ~loc
400
407
" expect label, optional, or unit here" )
401
- | Labelled name -> (
408
+ | Labelled label -> (
409
+ let fieldName = match get_maybe_obj_field_alias param_type.attr with
410
+ | Some (alias ) -> alias
411
+ | None -> label
412
+ in
402
413
let obj_arg_type = refine_obj_arg_type ~nolabel: false ty in
403
414
match obj_arg_type with
404
415
| Ignore ->
@@ -407,39 +418,39 @@ let process_obj (loc : Location.t) (st : external_desc) (prim_name : string)
407
418
result_types )
408
419
| Arg_cst _ ->
409
420
( {
410
- obj_arg_label = External_arg_spec. obj_label name ;
421
+ obj_arg_label = External_arg_spec. obj_label fieldName ;
411
422
obj_arg_type;
412
423
},
413
424
arg_types,
414
425
(* ignored in [arg_types], reserved in [result_types] *)
415
426
result_types )
416
427
| Nothing ->
417
428
( {
418
- obj_arg_label = External_arg_spec. obj_label name ;
429
+ obj_arg_label = External_arg_spec. obj_label fieldName ;
419
430
obj_arg_type;
420
431
},
421
432
param_type :: arg_types,
422
- Parsetree. Otag ({Asttypes. txt = name ; loc}, [] , ty)
433
+ Parsetree. Otag ({Asttypes. txt = fieldName ; loc}, [] , ty)
423
434
:: result_types )
424
435
| Int _ ->
425
436
( {
426
- obj_arg_label = External_arg_spec. obj_label name ;
437
+ obj_arg_label = External_arg_spec. obj_label fieldName ;
427
438
obj_arg_type;
428
439
},
429
440
param_type :: arg_types,
430
441
Otag
431
- ( {Asttypes. txt = name ; loc},
442
+ ( {Asttypes. txt = fieldName ; loc},
432
443
[] ,
433
444
Ast_literal. type_int ~loc () )
434
445
:: result_types )
435
446
| Poly_var_string _ ->
436
447
( {
437
- obj_arg_label = External_arg_spec. obj_label name ;
448
+ obj_arg_label = External_arg_spec. obj_label fieldName ;
438
449
obj_arg_type;
439
450
},
440
451
param_type :: arg_types,
441
452
Otag
442
- ( {Asttypes. txt = name ; loc},
453
+ ( {Asttypes. txt = fieldName ; loc},
443
454
[] ,
444
455
Ast_literal. type_string ~loc () )
445
456
:: result_types )
@@ -449,11 +460,15 @@ let process_obj (loc : Location.t) (st : external_desc) (prim_name : string)
449
460
| Extern_unit -> assert false
450
461
| Poly_var _ ->
451
462
Location. raise_errorf ~loc
452
- " %@obj label %s does not support such arg type" name
463
+ " %@obj label %s does not support such arg type" label
453
464
| Unwrap ->
454
465
Location. raise_errorf ~loc
455
- " %@obj label %s does not support %@unwrap arguments" name)
456
- | Optional name -> (
466
+ " %@obj label %s does not support %@unwrap arguments" label)
467
+ | Optional label -> (
468
+ let fieldName = match get_maybe_obj_field_alias param_type.attr with
469
+ | Some (alias ) -> alias
470
+ | None -> label
471
+ in
457
472
let obj_arg_type = get_opt_arg_type ~nolabel: false ty in
458
473
match obj_arg_type with
459
474
| Ignore ->
@@ -469,35 +484,35 @@ let process_obj (loc : Location.t) (st : external_desc) (prim_name : string)
469
484
in
470
485
( {
471
486
obj_arg_label =
472
- External_arg_spec. optional for_sure_not_nested name ;
487
+ External_arg_spec. optional for_sure_not_nested fieldName ;
473
488
obj_arg_type;
474
489
},
475
490
param_type :: arg_types,
476
491
Parsetree. Otag
477
- ( {Asttypes. txt = name ; loc},
492
+ ( {Asttypes. txt = fieldName ; loc},
478
493
[] ,
479
494
Ast_comb. to_undefined_type loc ty )
480
495
:: result_types )
481
496
| Int _ ->
482
497
( {
483
- obj_arg_label = External_arg_spec. optional true name ;
498
+ obj_arg_label = External_arg_spec. optional true fieldName ;
484
499
obj_arg_type;
485
500
},
486
501
param_type :: arg_types,
487
502
Otag
488
- ( {Asttypes. txt = name ; loc},
503
+ ( {Asttypes. txt = fieldName ; loc},
489
504
[] ,
490
505
Ast_comb. to_undefined_type loc
491
506
@@ Ast_literal. type_int ~loc () )
492
507
:: result_types )
493
508
| Poly_var_string _ ->
494
509
( {
495
- obj_arg_label = External_arg_spec. optional true name ;
510
+ obj_arg_label = External_arg_spec. optional true fieldName ;
496
511
obj_arg_type;
497
512
},
498
513
param_type :: arg_types,
499
514
Otag
500
- ( {Asttypes. txt = name ; loc},
515
+ ( {Asttypes. txt = fieldName ; loc},
501
516
[] ,
502
517
Ast_comb. to_undefined_type loc
503
518
@@ Ast_literal. type_string ~loc () )
@@ -511,10 +526,10 @@ let process_obj (loc : Location.t) (st : external_desc) (prim_name : string)
511
526
| Extern_unit -> assert false
512
527
| Poly_var _ ->
513
528
Location. raise_errorf ~loc
514
- " %@obj label %s does not support such arg type" name
529
+ " %@obj label %s does not support such arg type" label
515
530
| Unwrap ->
516
531
Location. raise_errorf ~loc
517
- " %@obj label %s does not support %@unwrap arguments" name )
532
+ " %@obj label %s does not support %@unwrap arguments" label )
518
533
in
519
534
(new_arg_label :: arg_labels, new_arg_types, output_tys))
520
535
in
0 commit comments