Skip to content

Commit adc0a85

Browse files
authored
Merge pull request #509 from Nadrieril/use-crate-in-envs
2 parents 13fd465 + 0be4542 commit adc0a85

7 files changed

Lines changed: 33 additions & 77 deletions

File tree

charon-ml/src/LlbcAst.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ type fun_body = expr_body [@@deriving show]
1010
type fun_decl = statement gfun_decl [@@deriving show]
1111

1212
(** LLBC crate *)
13-
type crate = statement gcrate
13+
type crate = statement gcrate [@@deriving show]

charon-ml/src/NameMatcher.ml

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -220,37 +220,12 @@ end
220220
module VarMap = Collections.MakeMap (VarOrderedType)
221221

222222
(** Context to lookup definitions *)
223-
type ctx = {
224-
type_decls : T.type_decl T.TypeDeclId.Map.t;
225-
global_decls : LlbcAst.global_decl T.GlobalDeclId.Map.t;
226-
fun_decls : A.fun_decl A.FunDeclId.Map.t;
227-
trait_decls : A.trait_decl T.TraitDeclId.Map.t;
228-
trait_impls : A.trait_impl T.TraitImplId.Map.t;
229-
}
223+
type ctx = { crate : A.crate }
230224

231225
let ctx_to_fmt_env (ctx : ctx) : PrintLlbcAst.fmt_env =
232-
{
233-
type_decls = ctx.type_decls;
234-
fun_decls = ctx.fun_decls;
235-
global_decls = ctx.global_decls;
236-
trait_decls = ctx.trait_decls;
237-
trait_impls = ctx.trait_impls;
238-
generics = [];
239-
locals = [];
240-
}
226+
{ crate = ctx.crate; generics = []; locals = [] }
241227

242-
let ctx_from_crate (crate : LlbcAst.crate) : ctx =
243-
let {
244-
LlbcAst.type_decls;
245-
fun_decls;
246-
global_decls;
247-
trait_decls;
248-
trait_impls;
249-
_;
250-
} =
251-
crate
252-
in
253-
{ type_decls; global_decls; trait_decls; fun_decls; trait_impls }
228+
let ctx_from_crate (crate : LlbcAst.crate) : ctx = { crate }
254229

255230
(** Match configuration *)
256231
type match_config = {
@@ -512,7 +487,7 @@ and match_pattern_with_type_id (ctx : ctx) (c : match_config) (m : maps)
512487
match id with
513488
| TAdtId id ->
514489
(* Lookup the type decl and match the name *)
515-
let d = T.TypeDeclId.Map.find id ctx.type_decls in
490+
let d = T.TypeDeclId.Map.find id ctx.crate.type_decls in
516491
match_name_with_generics ctx c ~m pid d.item_meta.name generics
517492
| TTuple -> false
518493
| TBuiltin id -> (
@@ -574,10 +549,10 @@ and match_expr_with_ty (ctx : ctx) (c : match_config) (m : maps) (pty : expr)
574549
and match_expr_with_trait_impl_id (ctx : ctx) (c : match_config) (ptr : expr)
575550
(impl_id : T.TraitImplId.id) : bool =
576551
(* Lookup the trait implementation *)
577-
let impl = T.TraitImplId.Map.find impl_id ctx.trait_impls in
552+
let impl = T.TraitImplId.Map.find impl_id ctx.crate.trait_impls in
578553
(* Lookup the trait declaration *)
579554
let d =
580-
T.TraitDeclId.Map.find impl.impl_trait.trait_decl_id ctx.trait_decls
555+
T.TraitDeclId.Map.find impl.impl_trait.trait_decl_id ctx.crate.trait_decls
581556
in
582557
(* Match *)
583558
match ptr with
@@ -590,7 +565,7 @@ and match_trait_decl_ref (ctx : ctx) (c : match_config) (m : maps)
590565
(pid : pattern) (tr : T.trait_decl_ref T.region_binder) : bool =
591566
(* Lookup the trait declaration *)
592567
let d =
593-
T.TraitDeclId.Map.find tr.binder_value.trait_decl_id ctx.trait_decls
568+
T.TraitDeclId.Map.find tr.binder_value.trait_decl_id ctx.crate.trait_decls
594569
in
595570
(* Push a region group in the map, if necessary - TODO: make this more precise *)
596571
let m = maps_push_bound_regions_group_if_nonempty m tr.binder_regions in
@@ -680,7 +655,7 @@ and match_expr_with_const_generic (ctx : ctx) (c : match_config) (m : maps)
680655
| EVar pv, _ -> opt_update_cmap c m pv cg
681656
| EComp pat, CgGlobal gid ->
682657
(* Lookup the decl and match the name *)
683-
let d = T.GlobalDeclId.Map.find gid ctx.global_decls in
658+
let d = T.GlobalDeclId.Map.find gid ctx.crate.global_decls in
684659
match_name ctx c pat d.item_meta.name
685660
| _ -> false
686661

@@ -733,7 +708,7 @@ let match_fn_ptr (ctx : ctx) (c : match_config) (p : pattern) (func : E.fn_ptr)
733708
let name = builtin_fun_id_to_string fid in
734709
match_name_with_generics ctx c p (to_name [ name ]) func.generics)
735710
| FunId (FRegular fid) ->
736-
let d = A.FunDeclId.Map.find fid ctx.fun_decls in
711+
let d = A.FunDeclId.Map.find fid ctx.crate.fun_decls in
737712
(* Match the pattern on the name of the function. *)
738713
let match_function_name =
739714
match_name_with_generics ctx c p d.item_meta.name func.generics
@@ -948,7 +923,7 @@ and impl_elem_to_pattern (ctx : ctx) (c : to_pat_config) (impl : T.impl_elem) :
948923
| ImplElemTy bound_ty ->
949924
PImpl (ty_to_pattern ctx c bound_ty.binder_params bound_ty.binder_value)
950925
| ImplElemTrait impl_id ->
951-
let impl = T.TraitImplId.Map.find impl_id ctx.trait_impls in
926+
let impl = T.TraitImplId.Map.find impl_id ctx.crate.trait_impls in
952927
PImpl (trait_decl_ref_to_pattern ctx c impl.generics impl.impl_trait)
953928

954929
and trait_decl_ref_to_pattern (ctx : ctx) (c : to_pat_config)
@@ -958,7 +933,7 @@ and trait_decl_ref_to_pattern (ctx : ctx) (c : to_pat_config)
958933
let { T.trait_decl_id; decl_generics } = tr in
959934
let generics = generic_args_to_pattern ctx c m decl_generics in
960935
(* Lookup the declaration *)
961-
let d = T.TraitDeclId.Map.find trait_decl_id ctx.trait_decls in
936+
let d = T.TraitDeclId.Map.find trait_decl_id ctx.crate.trait_decls in
962937
EComp
963938
(name_with_generic_args_to_pattern_aux ctx c d.item_meta.name
964939
(Some generics))
@@ -971,7 +946,7 @@ and ty_to_pattern_aux (ctx : ctx) (c : to_pat_config) (m : constraints)
971946
match id with
972947
| TAdtId id ->
973948
(* Lookup the declaration *)
974-
let d = T.TypeDeclId.Map.find id ctx.type_decls in
949+
let d = T.TypeDeclId.Map.find id ctx.crate.type_decls in
975950
EComp
976951
(name_with_generic_args_to_pattern_aux ctx c d.item_meta.name
977952
(Some generics))
@@ -1017,7 +992,7 @@ and trait_ref_item_with_generics_to_pattern (ctx : ctx) (c : to_pat_config)
1017992
let trait_decl_ref = trait_ref.trait_decl_ref in
1018993
let d =
1019994
T.TraitDeclId.Map.find trait_decl_ref.binder_value.trait_decl_id
1020-
ctx.trait_decls
995+
ctx.crate.trait_decls
1021996
in
1022997
(* Push a regions map if necessary - TODO: make this more precise *)
1023998
let m =
@@ -1048,7 +1023,7 @@ and const_generic_to_pattern (ctx : ctx) (c : to_pat_config) (m : constraints)
10481023
| CgVar v -> GExpr (EVar (const_generic_var_to_pattern m v))
10491024
| CgValue v -> GValue (literal_to_pattern c v)
10501025
| CgGlobal gid ->
1051-
let d = T.GlobalDeclId.Map.find gid ctx.global_decls in
1026+
let d = T.GlobalDeclId.Map.find gid ctx.crate.global_decls in
10521027
let n = name_to_pattern_aux ctx c d.item_meta.name in
10531028
GExpr (EComp n)
10541029

@@ -1138,7 +1113,7 @@ let fn_ptr_to_pattern (ctx : ctx) (c : to_pat_config)
11381113
let fid = builtin_fun_id_to_string fid in
11391114
[ PIdent (fid, args) ])
11401115
| FunId (FRegular fid) ->
1141-
let d = A.FunDeclId.Map.find fid ctx.fun_decls in
1116+
let d = A.FunDeclId.Map.find fid ctx.crate.fun_decls in
11421117
name_with_generic_args_to_pattern_aux ctx c d.item_meta.name (Some args)
11431118
| TraitMethod (tr, method_name, _) ->
11441119
trait_ref_item_with_generics_to_pattern ctx c m tr method_name

charon-ml/src/PrintLlbcAst.ml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,8 @@ end
143143
module Crate = struct
144144
open Ast
145145

146-
let crate_to_fmt_env (m : crate) : fmt_env =
147-
{
148-
type_decls = m.type_decls;
149-
fun_decls = m.fun_decls;
150-
global_decls = m.global_decls;
151-
trait_decls = m.trait_decls;
152-
trait_impls = m.trait_impls;
153-
generics = [];
154-
locals = [];
155-
}
146+
let crate_to_fmt_env (crate : crate) : fmt_env =
147+
{ crate; generics = []; locals = [] }
156148

157149
let crate_fun_decl_to_string (m : crate) (d : fun_decl) : string =
158150
let env = crate_to_fmt_env m in

charon-ml/src/PrintTypes.ml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,17 @@ let rec type_id_to_string (env : 'a fmt_env) (id : type_id) : string =
177177

178178
and type_decl_id_to_string env def_id =
179179
(* We don't want the printing functions to crash if the crate is partial *)
180-
match TypeDeclId.Map.find_opt def_id env.type_decls with
180+
match TypeDeclId.Map.find_opt def_id env.crate.type_decls with
181181
| None -> type_decl_id_to_pretty_string def_id
182182
| Some def -> name_to_string env def.item_meta.name
183183

184184
and fun_decl_id_to_string (env : 'a fmt_env) (id : FunDeclId.id) : string =
185-
match FunDeclId.Map.find_opt id env.fun_decls with
185+
match FunDeclId.Map.find_opt id env.crate.fun_decls with
186186
| None -> fun_decl_id_to_pretty_string id
187187
| Some def -> name_to_string env def.item_meta.name
188188

189189
and global_decl_id_to_string env def_id =
190-
match GlobalDeclId.Map.find_opt def_id env.global_decls with
190+
match GlobalDeclId.Map.find_opt def_id env.crate.global_decls with
191191
| None -> global_decl_id_to_pretty_string def_id
192192
| Some def -> name_to_string env def.item_meta.name
193193

@@ -198,12 +198,12 @@ and global_decl_ref_to_string (env : 'a fmt_env) (gr : global_decl_ref) : string
198198
global_id ^ generics
199199

200200
and trait_decl_id_to_string env id =
201-
match TraitDeclId.Map.find_opt id env.trait_decls with
201+
match TraitDeclId.Map.find_opt id env.crate.trait_decls with
202202
| None -> trait_decl_id_to_pretty_string id
203203
| Some def -> name_to_string env def.item_meta.name
204204

205205
and trait_impl_id_to_string env id =
206-
match TraitImplId.Map.find_opt id env.trait_impls with
206+
match TraitImplId.Map.find_opt id env.crate.trait_impls with
207207
| None -> trait_impl_id_to_pretty_string id
208208
| Some def -> name_to_string env def.item_meta.name
209209

@@ -321,7 +321,7 @@ and impl_elem_to_string (env : 'a fmt_env) (elem : impl_elem) : string =
321321
let env = fmt_env_update_generics_and_preds env bound_ty.binder_params in
322322
ty_to_string env bound_ty.binder_value
323323
| ImplElemTrait impl_id -> begin
324-
match TraitImplId.Map.find_opt impl_id env.trait_impls with
324+
match TraitImplId.Map.find_opt impl_id env.crate.trait_impls with
325325
| None -> trait_impl_id_to_string env impl_id
326326
| Some impl ->
327327
(* Locally replace the generics and the predicates *)
@@ -483,7 +483,7 @@ let type_decl_to_string (env : 'a fmt_env) (def : type_decl) : string =
483483

484484
let adt_variant_to_string (env : 'a fmt_env) (def_id : TypeDeclId.id)
485485
(variant_id : VariantId.id) : string =
486-
match TypeDeclId.Map.find_opt def_id env.type_decls with
486+
match TypeDeclId.Map.find_opt def_id env.crate.type_decls with
487487
| None ->
488488
type_decl_id_to_pretty_string def_id
489489
^ "::"
@@ -498,7 +498,7 @@ let adt_variant_to_string (env : 'a fmt_env) (def_id : TypeDeclId.id)
498498

499499
let adt_field_names (env : 'a fmt_env) (def_id : TypeDeclId.id)
500500
(opt_variant_id : VariantId.id option) : string list option =
501-
match TypeDeclId.Map.find_opt def_id env.type_decls with
501+
match TypeDeclId.Map.find_opt def_id env.crate.type_decls with
502502
| None -> None
503503
| Some def ->
504504
let fields = type_decl_get_fields def opt_variant_id in
@@ -515,7 +515,7 @@ let adt_field_names (env : 'a fmt_env) (def_id : TypeDeclId.id)
515515
let adt_field_to_string (env : 'a fmt_env) (def_id : TypeDeclId.id)
516516
(opt_variant_id : VariantId.id option) (field_id : FieldId.id) :
517517
string option =
518-
match TypeDeclId.Map.find_opt def_id env.type_decls with
518+
match TypeDeclId.Map.find_opt def_id env.crate.type_decls with
519519
| None -> None
520520
| Some def ->
521521
let fields = type_decl_get_fields def opt_variant_id in

charon-ml/src/PrintUllbcAst.ml

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,11 @@ end
111111
module Crate = struct
112112
open Ast
113113

114+
let crate_to_fmt_env (crate : crate) : fmt_env =
115+
{ crate; generics = []; locals = [] }
116+
114117
let crate_to_string (m : crate) : string =
115-
let env : fmt_env =
116-
{
117-
type_decls = m.type_decls;
118-
fun_decls = m.fun_decls;
119-
global_decls = m.global_decls;
120-
trait_decls = m.trait_decls;
121-
trait_impls = m.trait_impls;
122-
generics = [];
123-
locals = [];
124-
}
125-
in
118+
let env : fmt_env = crate_to_fmt_env m in
126119

127120
(* The types *)
128121
let type_decls =

charon-ml/src/PrintUtils.ml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ let block_id_to_string (id : UllbcAst.BlockId.id) : string =
1414
(for instance we can't find the type variable for a given index) we print
1515
the id in raw format. *)
1616
type 'fun_body fmt_env = {
17-
type_decls : type_decl TypeDeclId.Map.t;
18-
fun_decls : 'fun_body gfun_decl FunDeclId.Map.t;
19-
global_decls : global_decl GlobalDeclId.Map.t;
20-
trait_decls : trait_decl TraitDeclId.Map.t;
21-
trait_impls : trait_impl TraitImplId.Map.t;
17+
crate : 'fun_body gcrate;
2218
generics : generic_params list;
2319
(** We have a stack of generic parameters, because we can dive into
2420
binders (for instance because of the arrow type).

charon-ml/src/UllbcAst.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ type fun_body = expr_body [@@deriving show]
1111
type fun_decl = blocks gfun_decl [@@deriving show]
1212

1313
(** ULLBC crate *)
14-
type crate = blocks gcrate
14+
type crate = blocks gcrate [@@deriving show]

0 commit comments

Comments
 (0)