2222 * along with this program; if not, write to the Free Software
2323 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424
25+ (* Resolve an FFI external that [validate] accepted. That cannot fail, and
26+ its warnings were reported then, so they are not repeated. *)
27+ let resolve_validated (value_desc : Parsetree.value_description )
28+ (prim_name : string ) =
29+ Warnings. without_warnings (fun () ->
30+ Ast_external_process. resolve value_desc.pval_loc value_desc.pval_type
31+ value_desc.pval_attributes prim_name)
32+
2533let resolve (value_desc : Parsetree.value_description ) (prim_name : string ) :
2634 Primitive. resolved_external =
2735 if prim_name = Ast_external_mk. inline_const_prim then
@@ -37,15 +45,11 @@ let resolve (value_desc : Parsetree.value_description) (prim_name : string) :
3745 Location. raise_errorf ~loc: value_desc.pval_loc
3846 " \" %s\" is reserved for %@inline constants" prim_name
3947 else if
40- Ast_attributes. rs_externals value_desc.pval_attributes value_desc.pval_prim
48+ Ast_attributes. is_ffi_external value_desc.pval_attributes
49+ value_desc.pval_prim
4150 then
42- (* [handle_external_in_sig] and [handle_external_in_stru] resolved this
43- declaration already and reported its warnings; resolve it again for
44- the specification without repeating them. *)
4551 let {Ast_external_process. pval_type; spec; pval_attributes} =
46- Warnings. without_warnings (fun () ->
47- Ast_external_process. resolve value_desc.pval_loc value_desc.pval_type
48- value_desc.pval_attributes prim_name)
52+ resolve_validated value_desc prim_name
4953 in
5054 {
5155 resolved_type = pval_type;
@@ -65,13 +69,21 @@ let resolve (value_desc : Parsetree.value_description) (prim_name : string) :
6569 this module, so the type checker always finds the resolver registered. *)
6670let () = Primitive. resolve_external := resolve
6771
72+ let resolved_val (value_desc : Parsetree.value_description )
73+ ({pval_type; pval_attributes} : Ast_external_process.resolution ) :
74+ Parsetree. value_description =
75+ {value_desc with pval_type; pval_prim = None ; pval_attributes}
76+
77+ let resolved_ffi_external (value_desc : Parsetree.value_description ) =
78+ match value_desc.pval_prim with
79+ | None -> value_desc
80+ | Some {txt = prim_name } ->
81+ resolved_val value_desc (resolve_validated value_desc prim_name)
82+
6883(* Resolve an FFI external now to report its errors and warnings, and keep it
6984 as written for the type checker, which resolves it again through
70- [resolve]. Returns the external as written, its resolved form as a [val],
71- and whether a relative [@module] makes it unfit for cross-module inlining.
72- Such an external is exported as that [val], which the AST checks then see
73- like any other; otherwise its resolved form is checked here, since the
74- checks skip the unresolved one. *)
85+ [resolve]. Returns the external as written, and its resolved form as a
86+ [val] when a relative [@module] makes it unfit for cross-module inlining. *)
7587let validate (self : Ast_mapper.mapper ) (prim : Parsetree.value_description ) =
7688 let loc = prim.pval_loc in
7789 let pval_type = self.typ self prim.pval_type in
@@ -82,37 +94,28 @@ let validate (self : Ast_mapper.mapper) (prim : Parsetree.value_description) =
8294 let resolution =
8395 Ast_external_process. resolve loc pval_type pval_attributes prim_name
8496 in
85- let resolved_val : Parsetree.value_description =
86- {
87- prim with
88- pval_type = resolution.pval_type;
89- pval_prim = None ;
90- pval_attributes = resolution.pval_attributes;
91- }
92- in
93- if not resolution.no_inline_cross_module then
94- Bs_ast_invariant. check_resolved_external resolved_val;
9597 ( {prim with pval_type; pval_attributes},
96- resolved_val,
97- resolution.no_inline_cross_module )
98+ if resolution.no_inline_cross_module then
99+ Some (resolved_val prim resolution)
100+ else None )
98101
99102let handle_external_in_sig (self : Ast_mapper.mapper )
100103 (prim : Parsetree.value_description ) (sigi : Parsetree.signature_item ) :
101104 Parsetree. signature_item =
102- let external_, resolved_val, no_inline_cross_module = validate self prim in
105+ let external_, not_inlined = validate self prim in
103106 {
104107 sigi with
105- psig_desc =
106- Psig_value (if no_inline_cross_module then resolved_val else external_);
108+ psig_desc = Psig_value (Option. value not_inlined ~default: external_);
107109 }
108110
109111let handle_external_in_stru (self : Ast_mapper.mapper )
110112 (prim : Parsetree.value_description ) (str : Parsetree.structure_item ) :
111113 Parsetree. structure_item =
112- let external_, resolved_val, no_inline_cross_module = validate self prim in
114+ let external_, not_inlined = validate self prim in
113115 let external_result = {str with pstr_desc = Pstr_primitive external_} in
114- if not no_inline_cross_module then external_result
115- else
116+ match not_inlined with
117+ | None -> external_result
118+ | Some resolved_val ->
116119 let loc = prim.pval_loc in
117120 let open Ast_helper in
118121 Str. include_ ~loc
0 commit comments