@@ -66,28 +66,31 @@ let spec_of_ptyp (nolabel : bool) (ptyp : Parsetree.core_type) :
6666 if nolabel then Extern_unit else Nothing
6767 | _ -> Nothing )
6868
69+ let arg_cst_of_as_const :
70+ Ast_attributes. as_const_payload -> External_arg_spec. attr = function
71+ | Int i ->
72+ (* This type is used in obj only to construct obj type*)
73+ Arg_cst (External_arg_spec. cst_int i)
74+ | Str s -> Arg_cst (External_arg_spec. cst_string s)
75+ | Json s -> Arg_cst (External_arg_spec. cst_json s)
76+
6977(* is_optional = false
7078*)
7179let refine_arg_type ~(nolabel : bool ) (ptyp : Ast_core_type.t ) :
7280 External_arg_spec. attr =
73- if ptyp.ptyp_desc = Ptyp_any then
81+ if ptyp.ptyp_desc = Ptyp_any then (
7482 let ptyp_attrs = ptyp.ptyp_attributes in
7583 let payload = Ast_attributes. as_const ptyp_attrs in
7684 match payload with
7785 | None -> spec_of_ptyp nolabel ptyp
78- | Some cst -> (
86+ | Some cst ->
7987 (* (_[@as ])*)
8088 (* when ppx start dropping attributes
8189 we should warn, there is a trade off whether
8290 we should warn dropped non bs attribute or not
8391 *)
8492 Bs_ast_invariant. warn_discarded_unused_attributes ptyp_attrs;
85- match cst with
86- | Int i ->
87- (* This type is used in obj only to construct obj type*)
88- Arg_cst (External_arg_spec. cst_int i)
89- | Str s -> Arg_cst (External_arg_spec. cst_string s)
90- | Json s -> Arg_cst (External_arg_spec. cst_json s))
93+ arg_cst_of_as_const cst)
9194 else (* ([`a|`b] [@string]) *)
9295 spec_of_ptyp nolabel ptyp
9396
@@ -103,14 +106,7 @@ let refine_obj_arg_type ~(nolabel : bool) (ptyp : Ast_core_type.t) :
103106 Bs_ast_invariant. warn_discarded_unused_attributes ptyp_attrs;
104107 match payload with
105108 | None -> Bs_syntaxerr. err ptyp.ptyp_loc Invalid_underscore_type_in_external
106- | Some (Int i ) ->
107- (* @as(24) *)
108- (* This type is used in obj only to construct obj type *)
109- Arg_cst (External_arg_spec. cst_int i)
110- | Some (Str s ) ->
111- (* @as("foo") *)
112- Arg_cst (External_arg_spec. cst_string s)
113- | Some (Json s ) -> Arg_cst (External_arg_spec. cst_json s))
109+ | Some cst -> arg_cst_of_as_const cst)
114110 else (* ([`a|`b] [@string]) *)
115111 spec_of_ptyp nolabel ptyp
116112
@@ -403,6 +399,26 @@ type response = {
403399 no_inline_cross_module : bool ;
404400}
405401
402+ (* A fixed argument means what [@as(value) _] means, so decode it with the
403+ same reader. *)
404+ let fixed_arg_type
405+ ((value , loc ) : Parsetree. fixed_value Location. loc * Location. t ) =
406+ match Ast_attributes. as_const [Ast_fixed_arg. to_attribute ~loc value] with
407+ | Some cst -> arg_cst_of_as_const cst
408+ | None -> Bs_syntaxerr. err loc Expect_int_or_string_or_json_literal
409+
410+ (* The label and type of an argument. A fixed argument is typed as its [_], so
411+ the checks for [@as(value) _] apply to it unchanged. Its location ends with
412+ that [_]. *)
413+ let arg_parts (arg : Parsetree.arg ) =
414+ match arg with
415+ | Parg_type {attrs; lbl; typ} -> (lbl, attrs, typ, None )
416+ | Parg_fixed {attrs; lbl; value; loc} ->
417+ ( lbl,
418+ attrs,
419+ Ast_helper.Typ. any ~loc: (Ast_fixed_arg. underscore_loc loc) () ,
420+ Some (value, loc) )
421+
406422let process_obj (loc : Location.t ) (st : external_desc ) (prim_name : string )
407423 (arg_types_ty : Parsetree.arg list ) (result_type : Ast_core_type.t ) :
408424 int * Parsetree. core_type * External_ffi_types. t =
@@ -434,8 +450,7 @@ let process_obj (loc : Location.t) (st : external_desc) (prim_name : string)
434450 param_type
435451 (arg_labels , (arg_types : Parsetree.arg list ), result_types )
436452 ->
437- let arg_label = param_type.lbl in
438- let ty = param_type.typ in
453+ let arg_label, param_attrs, ty, fixed = arg_parts param_type in
439454 let new_arg_label, new_arg_types, output_tys =
440455 match arg_label with
441456 | Nolabel -> (
@@ -449,11 +464,15 @@ let process_obj (loc : Location.t) (st : external_desc) (prim_name : string)
449464 " expect label, optional, or unit here" )
450465 | Labelled {txt = label } -> (
451466 let field_name =
452- match Ast_attributes. as_string param_type.attrs with
467+ match Ast_attributes. as_string param_attrs with
453468 | Some alias -> alias
454469 | None -> label
455470 in
456- let obj_arg_type = refine_obj_arg_type ~nolabel: false ty in
471+ let obj_arg_type =
472+ match fixed with
473+ | Some fixed -> fixed_arg_type fixed
474+ | None -> refine_obj_arg_type ~nolabel: false ty
475+ in
457476 match obj_arg_type with
458477 | Ignore ->
459478 ( External_arg_spec. empty_kind obj_arg_type,
@@ -506,7 +525,7 @@ let process_obj (loc : Location.t) (st : external_desc) (prim_name : string)
506525 " %@obj label %s does not support %@unwrap arguments" label)
507526 | Optional {txt = label } -> (
508527 let field_name =
509- match Ast_attributes. as_string param_type.attrs with
528+ match Ast_attributes. as_string param_attrs with
510529 | Some alias -> alias
511530 | None -> label
512531 in
@@ -914,8 +933,7 @@ let handle_attributes (loc : Location.t) (type_annotation : Parsetree.core_type)
914933 Ext_list. fold_right arg_types_ty
915934 (([] , [] , 0 ) : External_arg_spec. params * Parsetree. arg list * int )
916935 (fun param_type (arg_type_specs , arg_types , i ) ->
917- let arg_label = param_type.lbl in
918- let ty = param_type.typ in
936+ let arg_label, _, ty, fixed = arg_parts param_type in
919937 (if i = 0 && splice then
920938 match arg_label with
921939 | Optional _ ->
@@ -948,14 +966,22 @@ let handle_attributes (loc : Location.t) (type_annotation : Parsetree.core_type)
948966 s
949967 | _ -> (Arg_optional , arg_type, param_type :: arg_types))
950968 | Labelled _ -> (
951- let arg_type = refine_arg_type ~nolabel: false ty in
969+ let arg_type =
970+ match fixed with
971+ | Some fixed -> fixed_arg_type fixed
972+ | None -> refine_arg_type ~nolabel: false ty
973+ in
952974 ( Arg_label ,
953975 arg_type,
954976 match arg_type with
955977 | Arg_cst _ -> arg_types
956978 | _ -> param_type :: arg_types ))
957979 | Nolabel -> (
958- let arg_type = refine_arg_type ~nolabel: true ty in
980+ let arg_type =
981+ match fixed with
982+ | Some fixed -> fixed_arg_type fixed
983+ | None -> refine_arg_type ~nolabel: true ty
984+ in
959985 ( Arg_empty ,
960986 arg_type,
961987 match arg_type with
@@ -977,7 +1003,9 @@ let handle_attributes (loc : Location.t) (type_annotation : Parsetree.core_type)
9771003 (Location. mkloc (Longident. Lident " unit" ) loc)
9781004 []
9791005 in
980- let unit_arg = {Parsetree. attrs = [] ; lbl = Nolabel ; typ = unit_type} in
1006+ let unit_arg =
1007+ Parsetree. Parg_type {attrs = [] ; lbl = Nolabel ; typ = unit_type}
1008+ in
9811009 ( [unit_arg],
9821010 arg_type_specs
9831011 @ [{External_arg_spec. arg_label = Arg_empty ; arg_type = Extern_unit }]
0 commit comments