Skip to content

Commit b82bd9c

Browse files
authored
Merge pull request #1505 from cryspen/ocaml-bridge-reviewable
Rust Engine: OCaml bridge for the AST (OCaml AST -> Rust AST)
2 parents 641e147 + 62836ce commit b82bd9c

File tree

7 files changed

+692
-1
lines changed

7 files changed

+692
-1
lines changed

engine/lib/concrete_ident/concrete_ident.ml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ module Fresh_module : sig
2727

2828
val to_mod_path : t -> View.ModPath.t
2929
(** Compute a module path for a fresh module. *)
30+
31+
val to_rust_ast : t -> Rust_engine_types.fresh_module
32+
val from_rust_ast : Rust_engine_types.fresh_module -> t
3033
end = struct
3134
open View
3235

@@ -111,6 +114,21 @@ end = struct
111114
|> snd |> Option.value_exn
112115

113116
let get_path_hints { hints; _ } = hints
117+
118+
let to_rust_ast ({ id; hints; label } : t) : Rust_engine_types.fresh_module =
119+
{
120+
id = Int.to_string id;
121+
hints = List.map ~f:Explicit_def_id.to_rust_ast hints;
122+
label;
123+
}
124+
125+
let from_rust_ast ({ id; hints; label } : Rust_engine_types.fresh_module) : t
126+
=
127+
{
128+
id = Int.of_string id;
129+
hints = List.map ~f:Explicit_def_id.from_rust_ast hints;
130+
label;
131+
}
114132
end
115133

116134
type reserved_suffix = [ `Cast | `Pre | `Post ]
@@ -743,3 +761,27 @@ let matches_namespace (ns : Types.namespace) (did : t) : bool =
743761
| _ -> false
744762
in
745763
aux ns.chunks path
764+
765+
let to_rust_ast ({ def_id; moved; suffix } : t) : Rust_engine_types.concrete_id
766+
=
767+
let moved = Option.map ~f:Fresh_module.to_rust_ast moved in
768+
let suffix =
769+
Option.map
770+
~f:(fun s ->
771+
match s with
772+
| `Cast -> Rust_engine_types.Cast
773+
| `Pre -> Rust_engine_types.Pre
774+
| `Post -> Rust_engine_types.Post)
775+
suffix
776+
in
777+
{ def_id = Explicit_def_id.to_rust_ast def_id; moved; suffix }
778+
779+
let from_rust_ast ({ def_id; moved; suffix } : Rust_engine_types.concrete_id) :
780+
t =
781+
let moved = Option.map ~f:Fresh_module.from_rust_ast moved in
782+
let suffix =
783+
Option.map
784+
~f:(fun s -> match s with Cast -> `Cast | Pre -> `Pre | Post -> `Post)
785+
suffix
786+
in
787+
{ def_id = Explicit_def_id.from_rust_ast def_id; moved; suffix }

engine/lib/concrete_ident/concrete_ident.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,5 @@ module ImplInfoStore : sig
104104
end
105105

106106
val matches_namespace : Types.namespace -> t -> bool
107+
val to_rust_ast : t -> Rust_engine_types.concrete_id
108+
val from_rust_ast : Rust_engine_types.concrete_id -> t

engine/lib/concrete_ident/explicit_def_id.ml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,46 @@ module ImplInfoStore = struct
125125
let lookup_raw (impl_def_id : t) : Types.impl_infos option =
126126
find (to_def_id impl_def_id)
127127
end
128+
129+
module ToRustAST = struct
130+
module A = Types
131+
module B = Rust_engine_types
132+
133+
let rec def_id_contents_to_rust_ast
134+
({ krate; path; parent; kind; _ } : A.def_id_contents) : B.def_id =
135+
let f (o : A.def_id) = def_id_contents_to_rust_ast o.contents.value in
136+
let parent = Option.map ~f parent in
137+
{ krate; path; parent; kind }
138+
139+
let to_rust_ast ({ is_constructor; def_id } : t) : B.explicit_def_id =
140+
{ is_constructor; def_id = def_id_contents_to_rust_ast def_id }
141+
end
142+
143+
module FromRustAST = struct
144+
module A = Rust_engine_types
145+
module B = Types
146+
147+
let rec def_id_contents_to_rust_ast
148+
({ krate; path; parent; kind; _ } : A.def_id) : B.def_id_contents =
149+
let f (o : A.def_id) : B.def_id =
150+
let contents : B.node_for__def_id_contents =
151+
{ value = def_id_contents_to_rust_ast o; id = Int64.of_int (-1) }
152+
in
153+
{ contents }
154+
in
155+
let parent = Option.map ~f parent in
156+
{
157+
krate;
158+
path;
159+
parent;
160+
kind;
161+
index = (Int64.of_int (-1), Int64.of_int (-1), None);
162+
is_local = false;
163+
}
164+
165+
let to_rust_ast ({ is_constructor; def_id } : A.explicit_def_id) : t =
166+
{ is_constructor; def_id = def_id_contents_to_rust_ast def_id }
167+
end
168+
169+
let to_rust_ast = ToRustAST.to_rust_ast
170+
let from_rust_ast = FromRustAST.to_rust_ast

engine/lib/concrete_ident/explicit_def_id.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,6 @@ module ImplInfoStore : sig
8181
engine, this function may return [None] even though the supplied
8282
identifier points to an [Impl] block. *)
8383
end
84+
85+
val to_rust_ast : t -> Rust_engine_types.explicit_def_id
86+
val from_rust_ast : Rust_engine_types.explicit_def_id -> t

0 commit comments

Comments
 (0)