Skip to content

Commit dddac02

Browse files
authored
Merge pull request #1088 from soteria-tools/issue-982
Add `Extern` and `Intrinsic` body types
2 parents 8658da7 + aabfaf4 commit dddac02

192 files changed

Lines changed: 1194 additions & 827 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

charon-ml/src/CharonVersion.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
(* This is an automatically generated file, generated from `charon/Cargo.toml`. *)
22
(* To re-generate this file, rune `make` in the root directory *)
3-
let supported_charon_version = "0.1.180"
3+
let supported_charon_version = "0.1.181"

charon-ml/src/GAst.ml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ type trait_declaration_group = TraitDeclId.id g_declaration_group
2626
type trait_impl_group = TraitImplId.id g_declaration_group [@@deriving show]
2727
type mixed_declaration_group = item_id g_declaration_group [@@deriving show]
2828

29+
(* Hand-written because the rust equivalent isn't generic *)
30+
type 'body body =
31+
| Body of 'body gexpr_body
32+
| TraitMethodWithoutDefault
33+
| Extern of string
34+
| Intrinsic of { name : string; arg_names : string list }
35+
| TargetDispatch of (string * fun_decl_ref) list
36+
| Opaque
37+
| Missing
38+
| Error of error
39+
[@@deriving show]
40+
2941
(* Hand-written because the rust equivalent isn't generic *)
3042
type 'body gfun_decl = {
3143
def_id : FunDeclId.id;
@@ -34,7 +46,7 @@ type 'body gfun_decl = {
3446
signature : fun_sig;
3547
src : item_source;
3648
is_global_initializer : GlobalDeclId.id option;
37-
body : 'body gexpr_body option;
49+
body : 'body body;
3850
}
3951
[@@deriving show]
4052

charon-ml/src/GAstOfJson.ml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ let option_list_of_json of_json = list_of_json (option_of_json of_json)
2020

2121
(* This is written by hand because the corresponding rust type is not type-generic. *)
2222
let rec gfun_decl_of_json
23-
(body_of_json :
24-
of_json_ctx -> json -> ('body gexpr_body option, string) result)
23+
(body_of_json : of_json_ctx -> json -> ('body body, string) result)
2524
(ctx : of_json_ctx) (js : json) : ('body gfun_decl, string) result =
2625
combine_error_msgs js __FUNCTION__
2726
(match js with
@@ -84,8 +83,7 @@ and id_to_file_of_json (ctx : of_json_ctx) (js : json) :
8483
type-generic. Note: because of hash-cons deduplication, we must make sure to
8584
deserialize in the exact same order as the rust side. *)
8685
and gtranslated_crate_of_json
87-
(body_of_json :
88-
of_json_ctx -> json -> ('body gexpr_body option, string) result)
86+
(body_of_json : of_json_ctx -> json -> ('body body, string) result)
8987
(js : json) : ('body gcrate, string) result =
9088
combine_error_msgs js __FUNCTION__
9189
(match js with
@@ -172,8 +170,7 @@ and gtranslated_crate_of_json
172170
| _ -> Error "")
173171

174172
and gcrate_of_json
175-
(body_of_json :
176-
of_json_ctx -> json -> ('body gexpr_body option, string) result)
173+
(body_of_json : of_json_ctx -> json -> ('body body, string) result)
177174
(js : json) : ('body gcrate, string) result =
178175
match js with
179176
| `Assoc [ ("charon_version", charon_version); ("translated", translated) ]

charon-ml/src/LlbcAst.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ include GAst
66
include Generated_LlbcAst
77

88
type expr_body = block gexpr_body [@@deriving show]
9-
type fun_body = expr_body [@@deriving show]
9+
type fun_body = block body [@@deriving show]
1010
type fun_decl = block gfun_decl [@@deriving show]
1111

1212
(** LLBC crate *)

charon-ml/src/LlbcAstUtils.ml

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ let fun_decl_list_from_crate (crate : crate) : fun_decl list =
1515
returns None *)
1616
let get_fun_args (fun_decl : fun_decl) : local list option =
1717
match fun_decl.body with
18-
| Some body -> Some (GAstUtils.locals_get_input_vars body.locals)
19-
| None -> None
18+
| Body body -> Some (GAstUtils.locals_get_input_vars body.locals)
19+
| _ -> None
2020

2121
(** Check if a {!type:Charon.LlbcAst.statement} contains loops *)
2222
let block_has_loops (blk : block) : bool =
@@ -34,8 +34,8 @@ let block_has_loops (blk : block) : bool =
3434
(** Check if a {!type:Charon.LlbcAst.fun_decl} contains loops *)
3535
let fun_decl_has_loops (fd : fun_decl) : bool =
3636
match fd.body with
37-
| Some body -> block_has_loops body.body
38-
| None -> false
37+
| Body body -> block_has_loops body.body
38+
| _ -> false
3939

4040
let crate_get_item_meta (m : crate) (id : item_id) : Types.item_meta option =
4141
match id with
@@ -91,7 +91,27 @@ class ['self] map_crate =
9191
let is_global_initializer =
9292
self#visit_option self#visit_global_decl_id env is_global_initializer
9393
in
94-
let body = self#visit_option self#visit_expr_body env body in
94+
let body =
95+
match body with
96+
| Body b -> Body (self#visit_expr_body env b)
97+
| TraitMethodWithoutDefault -> TraitMethodWithoutDefault
98+
| Extern sym -> Extern (self#visit_string env sym)
99+
| Intrinsic { name; arg_names } ->
100+
Intrinsic
101+
{
102+
name = self#visit_string env name;
103+
arg_names = List.map (self#visit_string env) arg_names;
104+
}
105+
| TargetDispatch targets ->
106+
TargetDispatch
107+
(self#visit_list
108+
(fun env (tgt, fref) ->
109+
(self#visit_string env tgt, self#visit_fun_decl_ref env fref))
110+
env targets)
111+
| Opaque -> Opaque
112+
| Missing -> Missing
113+
| Error err -> Error (self#visit_error env err)
114+
in
95115
{
96116
def_id;
97117
item_meta;
@@ -218,7 +238,22 @@ class ['self] iter_crate =
218238
self#visit_fun_sig env signature;
219239
self#visit_item_source env src;
220240
self#visit_option self#visit_global_decl_id env is_global_initializer;
221-
self#visit_option self#visit_expr_body env body
241+
match body with
242+
| Body b -> self#visit_expr_body env b
243+
| TraitMethodWithoutDefault -> ()
244+
| Extern sym -> self#visit_string env sym
245+
| Intrinsic { name; arg_names } ->
246+
self#visit_string env name;
247+
List.iter (self#visit_string env) arg_names
248+
| TargetDispatch targets ->
249+
self#visit_list
250+
(fun env (tgt, fref) ->
251+
self#visit_string env tgt;
252+
self#visit_fun_decl_ref env fref)
253+
env targets
254+
| Opaque -> ()
255+
| Missing -> ()
256+
| Error err -> self#visit_error env err
222257

223258
method visit_declaration_group env (g : declaration_group) : unit =
224259
match g with

charon-ml/src/LlbcOfJson.ml

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,38 @@ include GAstOfJson
99
include Generated_LlbcOfJson
1010

1111
let expr_body_of_json (ctx : of_json_ctx) (js : json) :
12-
(expr_body option, string) result =
12+
(fun_body, string) result =
1313
combine_error_msgs js __FUNCTION__
1414
(match js with
1515
| `Assoc [ ("Structured", body) ] ->
1616
let* body = gexpr_body_of_json block_of_json ctx body in
17-
Ok (Some body)
18-
| _ -> Ok None)
17+
Ok (Body body)
18+
| `Assoc [ ("Unstructured", _) ] ->
19+
(* Some .llbc bodies are emitted in ULLBC mode (e.g. UNIT_METADATA). *)
20+
Ok Opaque
21+
| `String "TraitMethodWithoutDefault" -> Ok TraitMethodWithoutDefault
22+
| `Assoc [ ("Extern", sym) ] ->
23+
let* sym = string_of_json ctx sym in
24+
Ok (Extern sym)
25+
| `Assoc
26+
[ ("Intrinsic", `Assoc [ ("name", name); ("arg_names", arg_names) ]) ]
27+
->
28+
let* name = string_of_json ctx name in
29+
let* arg_names = list_of_json string_of_json ctx arg_names in
30+
Ok (Intrinsic { name; arg_names })
31+
| `Assoc [ ("TargetDispatch", targets) ] ->
32+
let* targets =
33+
list_of_json
34+
(key_value_pair_of_json string_of_json fun_decl_ref_of_json)
35+
ctx targets
36+
in
37+
Ok (TargetDispatch targets)
38+
| `String "Opaque" -> Ok Opaque
39+
| `String "Missing" -> Ok Missing
40+
| `Assoc [ ("Error", e) ] ->
41+
let* e = error_of_json ctx e in
42+
Ok (GAst.Error e)
43+
| _ -> Error "")
1944

2045
let crate_of_json (js : json) : (crate, string) result =
2146
gcrate_of_json expr_body_of_json js

charon-ml/src/OfJsonBasic.ml

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,30 @@ let combine_error_msgs (js : json) (msg : string) (res : ('a, string) result) :
1313
('a, string) result =
1414
match res with
1515
| Ok x -> Ok x
16-
| Error e -> Error ("[" ^ msg ^ "]" ^ " failed on: " ^ show js ^ "\n\n" ^ e)
16+
| Error e ->
17+
Error ("[" ^ msg ^ "]" ^ " failed on: " ^ to_string js ^ "\n\n" ^ e)
1718

1819
let bool_of_json (ctx : 'ctx) (js : json) : (bool, string) result =
1920
match js with
2021
| `Bool b -> Ok b
21-
| _ -> Error ("bool_of_json: not a bool: " ^ show js)
22+
| _ -> Error ("bool_of_json: not a bool: " ^ to_string js)
2223

2324
let int_of_json (ctx : 'ctx) (js : json) : (int, string) result =
2425
match js with
2526
| `Int i -> Ok i
26-
| _ -> Error ("int_of_json: not an int: " ^ show js)
27+
| _ -> Error ("int_of_json: not an int: " ^ to_string js)
2728

2829
let char_of_json (ctx : 'ctx) (js : json) : (Uchar.t, string) result =
2930
match js with
3031
| `String c ->
3132
if String.length c > 4 then
32-
Error ("char_of_json: stricly more than four bytes in: " ^ show js)
33+
Error ("char_of_json: stricly more than four bytes in: " ^ to_string js)
3334
else
3435
let uchar = String.get_utf_8_uchar c 0 in
3536
if Uchar.utf_decode_is_valid uchar then
3637
Ok (Uchar.utf_decode_uchar uchar)
37-
else Error ("char_of_json: invalid UTF-8 character: " ^ show js)
38-
| _ -> Error ("char_of_json: not a char: " ^ show js)
38+
else Error ("char_of_json: invalid UTF-8 character: " ^ to_string js)
39+
| _ -> Error ("char_of_json: not a char: " ^ to_string js)
3940

4041
let rec of_json_list (a_of_json : 'ctx -> json -> ('a, string) result)
4142
(ctx : 'ctx) (jsl : json list) : ('a list, string) result =
@@ -54,7 +55,7 @@ let pair_of_json (a_of_json : 'ctx -> json -> ('a, string) result)
5455
let* a = a_of_json ctx a in
5556
let* b = b_of_json ctx b in
5657
Ok (a, b)
57-
| _ -> Error ("pair_of_json failed on: " ^ show js)
58+
| _ -> Error ("pair_of_json failed on: " ^ to_string js)
5859

5960
let triple_of_json (a_of_json : 'ctx -> json -> ('a, string) result)
6061
(b_of_json : 'ctx -> json -> ('b, string) result)
@@ -66,19 +67,19 @@ let triple_of_json (a_of_json : 'ctx -> json -> ('a, string) result)
6667
let* b = b_of_json ctx b in
6768
let* c = c_of_json ctx c in
6869
Ok (a, b, c)
69-
| _ -> Error ("triple_of_json failed on: " ^ show js)
70+
| _ -> Error ("triple_of_json failed on: " ^ to_string js)
7071

7172
let list_of_json (a_of_json : 'ctx -> json -> ('a, string) result) (ctx : 'ctx)
7273
(js : json) : ('a list, string) result =
7374
combine_error_msgs js "list_of_json"
7475
(match js with
7576
| `List jsl -> of_json_list a_of_json ctx jsl
76-
| _ -> Error ("not a list: " ^ show js))
77+
| _ -> Error ("not a list: " ^ to_string js))
7778

7879
let string_of_json (ctx : 'ctx) (js : json) : (string, string) result =
7980
match js with
8081
| `String str -> Ok str
81-
| _ -> Error ("string_of_json: not a string: " ^ show js)
82+
| _ -> Error ("string_of_json: not a string: " ^ to_string js)
8283

8384
let option_of_json (a_of_json : 'ctx -> json -> ('a, string) result)
8485
(ctx : 'ctx) (js : json) : ('a option, string) result =
@@ -105,4 +106,4 @@ let key_value_pair_of_json (a_of_json : 'ctx -> json -> ('a, string) result)
105106
let* a = a_of_json ctx a in
106107
let* b = b_of_json ctx b in
107108
Ok (a, b)
108-
| _ -> Error ("key_value_pair_of_json failed on: " ^ show js)
109+
| _ -> Error ("key_value_pair_of_json failed on: " ^ to_string js)

charon-ml/src/PrintGAst.ml

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,38 @@ let gfun_decl_to_string (env : 'a fmt_env) (indent : string)
112112
* (we have access to a body) *)
113113
let sg = bound_fun_sig_of_decl def in
114114
match def.body with
115-
| None ->
115+
| Opaque ->
116116
fun_sig_with_name_to_string env indent indent_incr (Some "opaque")
117117
(Some name) None sg
118-
| Some body ->
118+
| Missing ->
119+
fun_sig_with_name_to_string env indent indent_incr (Some "missing")
120+
(Some name) None sg
121+
| TraitMethodWithoutDefault ->
122+
fun_sig_with_name_to_string env indent indent_incr
123+
(Some "method_without_default_body") (Some name) None sg
124+
| Extern sym ->
125+
fun_sig_with_name_to_string env indent indent_incr
126+
(Some ("extern:" ^ sym))
127+
(Some name) None sg
128+
| Intrinsic { name; _ } ->
129+
fun_sig_with_name_to_string env indent indent_incr
130+
(Some ("intrinsic:" ^ name))
131+
(Some name) None sg
132+
| TargetDispatch targets ->
133+
let targets =
134+
targets
135+
|> List.map (fun (tgt, fref) ->
136+
tgt ^ " => " ^ fun_decl_ref_to_string env fref)
137+
|> String.concat ","
138+
in
139+
fun_sig_with_name_to_string env indent indent_incr
140+
(Some ("target_dispatch(" ^ targets ^ ")"))
141+
(Some name) None sg
142+
| Error err ->
143+
fun_sig_with_name_to_string env indent indent_incr
144+
(Some ("error(\"" ^ err.msg ^ "\")"))
145+
(Some name) None sg
146+
| Body body ->
119147
(* Locally update the environment *)
120148
let locals = List.map (fun v -> (v.index, v.name)) body.locals.locals in
121149
let env = { env with locals } in

charon-ml/src/Substitute.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,8 @@ let block_substitute (subst : subst) (blk : block) : block =
428428

429429
(** Apply a type substitution to a function body. Return the local variables and
430430
the body. *)
431-
let fun_body_substitute_in_body (subst : subst) (body : fun_body) :
432-
local list * block =
431+
let expr_body_substitute (subst : subst) (body : expr_body) : local list * block
432+
=
433433
let locals =
434434
List.map
435435
(fun (v : local) -> { v with local_ty = ty_substitute subst v.local_ty })

charon-ml/src/UllbcAst.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ include GAst
77
include Generated_UllbcAst
88

99
type expr_body = blocks gexpr_body [@@deriving show]
10-
type fun_body = expr_body [@@deriving show]
10+
type fun_body = blocks body [@@deriving show]
1111
type fun_decl = blocks gfun_decl [@@deriving show]
1212

1313
(** ULLBC crate *)

0 commit comments

Comments
 (0)