|
22 | 22 | * along with this program; if not, write to the Free Software |
23 | 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) |
24 | 24 |
|
25 | | -let handle_external_in_sig (self : Ast_mapper.mapper) |
26 | | - (prim : Parsetree.value_description) (sigi : Parsetree.signature_item) : |
27 | | - Parsetree.signature_item = |
| 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 | + |
| 33 | +let resolve (value_desc : Parsetree.value_description) (prim_name : string) : |
| 34 | + Primitive.resolved_external = |
| 35 | + if prim_name = Ast_external_mk.inline_const_prim then |
| 36 | + match Ast_external_mk.inline_const_of_declaration value_desc with |
| 37 | + | Some c -> |
| 38 | + { |
| 39 | + resolved_type = value_desc.pval_type; |
| 40 | + resolved_attributes = []; |
| 41 | + resolved_name = ""; |
| 42 | + resolved_kind = Kind_inline_const c; |
| 43 | + } |
| 44 | + | None -> |
| 45 | + Location.raise_errorf ~loc:value_desc.pval_loc |
| 46 | + "\"%s\" is reserved for %@inline constants" prim_name |
| 47 | + else if |
| 48 | + Ast_attributes.is_ffi_external value_desc.pval_attributes |
| 49 | + value_desc.pval_prim |
| 50 | + then |
| 51 | + let {Ast_external_process.pval_type; spec; pval_attributes} = |
| 52 | + resolve_validated value_desc prim_name |
| 53 | + in |
| 54 | + { |
| 55 | + resolved_type = pval_type; |
| 56 | + resolved_attributes = pval_attributes; |
| 57 | + resolved_name = prim_name; |
| 58 | + resolved_kind = Kind_external spec; |
| 59 | + } |
| 60 | + else |
| 61 | + { |
| 62 | + resolved_type = value_desc.pval_type; |
| 63 | + resolved_attributes = value_desc.pval_attributes; |
| 64 | + resolved_name = prim_name; |
| 65 | + resolved_kind = Kind_intrinsic; |
| 66 | + } |
| 67 | + |
| 68 | +(* The built-in PPX, which every compilation runs before type checking, links |
| 69 | + this module, so the type checker always finds the resolver registered. *) |
| 70 | +let () = Primitive.resolve_external := resolve |
| 71 | + |
| 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 | + |
| 83 | +(* Resolve an FFI external now to report its errors and warnings, and keep it |
| 84 | + as written for the type checker, which resolves it again through |
| 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. *) |
| 87 | +let validate (self : Ast_mapper.mapper) (prim : Parsetree.value_description) = |
28 | 88 | let loc = prim.pval_loc in |
29 | 89 | let pval_type = self.typ self prim.pval_type in |
30 | 90 | let pval_attributes = self.attributes self prim.pval_attributes in |
31 | 91 | match prim.pval_prim with |
32 | 92 | | None -> Location.raise_errorf ~loc "empty primitive string" |
33 | | - | Some (Prim_ffi _ | Prim_inline_const _) -> |
34 | | - Location.raise_errorf ~loc "external declaration already processed" |
35 | | - | Some (Prim_name v) -> ( |
36 | | - match |
37 | | - Ast_external_process.handle_attributes_as_prim loc pval_type |
38 | | - pval_attributes v |
39 | | - with |
40 | | - | {pval_type; pval_prim; pval_attributes; no_inline_cross_module} -> |
41 | | - { |
42 | | - sigi with |
43 | | - psig_desc = |
44 | | - Psig_value |
45 | | - { |
46 | | - prim with |
47 | | - pval_type; |
48 | | - pval_prim = |
49 | | - (if no_inline_cross_module then None else Some pval_prim); |
50 | | - pval_attributes; |
51 | | - }; |
52 | | - }) |
| 93 | + | Some {txt = prim_name} -> |
| 94 | + let resolution = |
| 95 | + Ast_external_process.resolve loc pval_type pval_attributes prim_name |
| 96 | + in |
| 97 | + ( {prim with pval_type; pval_attributes}, |
| 98 | + if resolution.no_inline_cross_module then |
| 99 | + Some (resolved_val prim resolution) |
| 100 | + else None ) |
| 101 | + |
| 102 | +let handle_external_in_sig (self : Ast_mapper.mapper) |
| 103 | + (prim : Parsetree.value_description) (sigi : Parsetree.signature_item) : |
| 104 | + Parsetree.signature_item = |
| 105 | + let external_, not_inlined = validate self prim in |
| 106 | + { |
| 107 | + sigi with |
| 108 | + psig_desc = Psig_value (Option.value not_inlined ~default:external_); |
| 109 | + } |
53 | 110 |
|
54 | 111 | let handle_external_in_stru (self : Ast_mapper.mapper) |
55 | 112 | (prim : Parsetree.value_description) (str : Parsetree.structure_item) : |
56 | 113 | Parsetree.structure_item = |
57 | | - let loc = prim.pval_loc in |
58 | | - let pval_type = self.typ self prim.pval_type in |
59 | | - let pval_attributes = self.attributes self prim.pval_attributes in |
60 | | - match prim.pval_prim with |
61 | | - | None -> Location.raise_errorf ~loc "empty primitive string" |
62 | | - | Some (Prim_ffi _ | Prim_inline_const _) -> |
63 | | - Location.raise_errorf ~loc "external declaration already processed" |
64 | | - | Some (Prim_name v) -> ( |
65 | | - match |
66 | | - Ast_external_process.handle_attributes_as_prim loc pval_type |
67 | | - pval_attributes v |
68 | | - with |
69 | | - | {pval_type; pval_prim; pval_attributes; no_inline_cross_module} -> |
70 | | - let external_result = |
71 | | - { |
72 | | - str with |
73 | | - pstr_desc = |
74 | | - Pstr_primitive |
75 | | - {prim with pval_type; pval_prim = Some pval_prim; pval_attributes}; |
76 | | - } |
77 | | - in |
78 | | - if not no_inline_cross_module then external_result |
79 | | - else |
80 | | - let open Ast_helper in |
81 | | - Str.include_ ~loc |
82 | | - (Incl.mk ~loc |
83 | | - (Mod.constraint_ ~loc |
84 | | - (Mod.structure ~loc [external_result]) |
85 | | - (Mty.signature ~loc |
86 | | - [ |
87 | | - { |
88 | | - psig_desc = |
89 | | - Psig_value |
90 | | - { |
91 | | - prim with |
92 | | - pval_type; |
93 | | - pval_prim = None; |
94 | | - pval_attributes; |
95 | | - }; |
96 | | - psig_loc = loc; |
97 | | - }; |
98 | | - ])))) |
| 114 | + let external_, not_inlined = validate self prim in |
| 115 | + let external_result = {str with pstr_desc = Pstr_primitive external_} in |
| 116 | + match not_inlined with |
| 117 | + | None -> external_result |
| 118 | + | Some resolved_val -> |
| 119 | + let loc = prim.pval_loc in |
| 120 | + let open Ast_helper in |
| 121 | + Str.include_ ~loc |
| 122 | + (Incl.mk ~loc |
| 123 | + (Mod.constraint_ ~loc |
| 124 | + (Mod.structure ~loc [external_result]) |
| 125 | + (Mty.signature ~loc |
| 126 | + [{psig_desc = Psig_value resolved_val; psig_loc = loc}]))) |
0 commit comments