forked from viperproject/prusti-dev
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmake.rs
More file actions
933 lines (867 loc) · 32.6 KB
/
make.rs
File metadata and controls
933 lines (867 loc) · 32.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
use crate::{
callable_idents::*,
data::*,
debug_info::{DebugInfo, DEBUGINFO_NONE},
gendata::*,
genrefs::*,
refs::*,
typecheck_error, ViperIdent, VirCtxt,
};
use cfg_if::cfg_if;
use prusti_rustc_interface::middle::ty;
use std::fmt::Debug;
macro_rules! const_expr {
($expr_kind:expr) => {
&ExprGenData {
kind: $expr_kind,
debug_info: DEBUGINFO_NONE,
span: None,
}
};
}
cfg_if! {
if #[cfg(debug_assertions)] {
// The functions below conservatively check that local variables bound
// in forall expressions, let-bindings, function arguments etc. have the
// correct type with respect to their usages. It's better to identify
// the relevant errors here so more debug information is available. The
// check is incomplete, namely:
// - Lazy expressions are not typechecked
// - The binding for a local is not always known, usages of unbound
// variables are not checked
// - Unsupported types are not checked
use std::collections::HashMap;
fn check_predicate_app_bindings<'vir, Curr, Next>(
m: &mut HashMap<&'vir str, Type<'vir>>,
e: PredicateAppGen<'vir, Curr, Next>
) {
for arg in e.args.iter() {
check_expr_bindings(m, *arg);
}
if let Some(perm) = e.perm {
check_expr_bindings(m, perm);
}
}
fn check_stmt_bindings<'vir, Curr, Next>(
m: &mut HashMap<&'vir str, Type<'vir>>,
e: StmtGen<'vir, Curr, Next>
) {
match e.kind {
StmtKindGenData::LocalDecl(local, e) => {
if let Some(e) = e {
check_expr_bindings(m, e);
}
m.insert(local.name, local.ty);
}
StmtKindGenData::PureAssign(p) => {
check_expr_bindings(m, p.lhs);
check_expr_bindings(m, p.rhs);
}
StmtKindGenData::Inhale(e) |
StmtKindGenData::Exhale(e) => {
check_expr_bindings(m, e);
}
StmtKindGenData::Unfold(app) | StmtKindGenData::Fold(app) => {
check_predicate_app_bindings(m, app);
}
StmtKindGenData::Package(_wand, stmts) => {
// TODO: check types in wand
for stmt in stmts.iter() {
check_stmt_bindings(m, stmt);
}
}
StmtKindGenData::Apply(_wand) => {
// TODO: check types in wand
}
StmtKindGenData::MethodCall(MethodCallGenData {
args,
..
}) => {
for arg in args.iter() {
check_expr_bindings(m, *arg);
}
}
StmtKindGenData::If(e, thn, els) => {
check_expr_bindings(m, e);
for thn in thn.iter() {
check_stmt_bindings(m, thn);
}
for els in els.iter() {
check_stmt_bindings(m, els);
}
}
StmtKindGenData::Label(_) => {},
StmtKindGenData::Comment(_) => {},
StmtKindGenData::Dummy(_) => todo!(),
}
}
fn check_expr_bindings<'vir, Curr, Next>(
m: &mut HashMap<&'vir str, Type<'vir>>,
e: ExprGen<'vir, Curr, Next>
) {
match e.kind {
ExprKindGenData::Result(..) => (),
ExprKindGenData::Local(LocalData { name, ty, debug_info }) => {
if let Some(bound_ty) = m.get(name) {
if !matches!(bound_ty, TypeData::Unsupported(_)) &&
!matches!(ty, TypeData::Unsupported(_)) &&
bound_ty != ty
{
typecheck_error!(
"Type mismatch for local variable {name}. \
Scope assigns {name} to type {bound_ty:?}, but the actual type is {ty:?}.\
Debug info: {debug_info}"
)
}
}
},
ExprKindGenData::Let(LetGenData { name, val, expr }) => {
check_expr_bindings(m, *val);
if !matches!(val.kind, ExprKindGenData::Lazy(..)) {
m.insert(name, val.ty());
}
check_expr_bindings(m, *expr);
m.remove(name);
},
ExprKindGenData::FuncApp(FuncAppGenData { args, .. }) => {
for arg in args.iter() {
check_expr_bindings(m, *arg);
}
},
ExprKindGenData::Old(OldGenData { expr, .. }) => {
check_expr_bindings(m, *expr);
},
ExprKindGenData::Const(..) | ExprKindGenData::Lazy(..) => {},
ExprKindGenData::PredicateApp(app) => {
check_predicate_app_bindings(m, app);
},
ExprKindGenData::AccField( AccFieldGenData { recv, perm, .. }) => {
check_expr_bindings(m, *recv);
if let Some(perm) = perm {
check_expr_bindings(m, *perm);
}
},
ExprKindGenData::Field(e, _) => {
check_expr_bindings(m, *e);
},
ExprKindGenData::Unfolding(UnfoldingGenData { target, expr }) => {
check_predicate_app_bindings(m, target);
check_expr_bindings(m, *expr);
},
ExprKindGenData::BinOp(BinOpGenData { lhs, rhs, .. }) => {
check_expr_bindings(m, *lhs);
check_expr_bindings(m, *rhs);
},
ExprKindGenData::UnOp(UnOpGenData { expr, .. }) => {
check_expr_bindings(m, *expr);
},
ExprKindGenData::Ternary(TernaryGenData { cond, then, else_}) => {
check_expr_bindings(m, *cond);
check_expr_bindings(m, *then);
check_expr_bindings(m, *else_);
}
ExprKindGenData::Forall(ForallGenData { qvars, triggers, body }) => {
for qvar in qvars.iter() {
m.insert(qvar.name, qvar.ty);
}
for trigger in triggers.iter() {
for expr in trigger.exprs.iter() {
check_expr_bindings(m, *expr);
}
}
check_expr_bindings(m, *body);
for qvar in qvars.iter() {
m.remove(qvar.name);
}
}
ExprKindGenData::Wand(WandGenData { lhs, rhs }) => {
check_expr_bindings(m, *lhs);
check_expr_bindings(m, *rhs);
}
other => todo!("{other:?}")
}
}
}
}
impl<'tcx> VirCtxt<'tcx> {
pub fn mk_local<'vir>(&'vir self, name: &'vir str, ty: Type<'vir>) -> Local<'vir> {
self.alloc(LocalData {
name,
ty,
debug_info: DebugInfo::new(self),
})
}
pub fn mk_local_decl<'vir>(&'vir self, name: &'vir str, ty: Type<'vir>) -> LocalDecl<'vir> {
self.alloc(LocalDeclData { name, ty })
}
pub fn mk_local_decl_local<'vir>(&'vir self, local: Local<'vir>) -> LocalDecl<'vir> {
self.alloc(LocalDeclData {
name: local.name,
ty: local.ty,
})
}
pub fn mk_local_ex_local<'vir, Curr, Next>(
&'vir self,
local: Local<'vir>,
) -> ExprGen<'vir, Curr, Next> {
self.alloc(ExprGenData::new(self.alloc(ExprKindGenData::Local(local))))
}
pub fn mk_local_ex<'vir, Curr, Next>(
&'vir self,
name: &'vir str,
ty: Type<'vir>,
) -> ExprGen<'vir, Curr, Next> {
self.mk_local_ex_local(self.mk_local(name, ty))
}
pub(crate) fn mk_func_app<'vir, Curr, Next>(
&'vir self,
target: &'vir str,
src_args: &[ExprGen<'vir, Curr, Next>],
result_ty: Type<'vir>,
) -> ExprGen<'vir, Curr, Next> {
self.alloc(ExprGenData::new(self.alloc(ExprKindGenData::FuncApp(
self.arena.alloc(FuncAppGenData {
target,
args: self.alloc_slice(src_args),
result_ty,
}),
))))
}
#[allow(clippy::type_complexity)]
pub fn mk_lazy_expr<'vir, Curr, Next>(
&'vir self,
name: &'vir str,
ty: Type<'vir>,
func: Box<dyn for<'a> Fn(&'vir VirCtxt<'a>, Curr) -> Next + 'vir>,
) -> ExprGen<'vir, Curr, Next> {
self.alloc(ExprGenData::new(self.alloc(ExprKindGenData::Lazy(
self.alloc(LazyGenData { name, func, ty }),
))))
}
pub fn mk_ternary_expr<'vir, Curr, Next>(
&'vir self,
cond: ExprGen<'vir, Curr, Next>,
then: ExprGen<'vir, Curr, Next>,
else_: ExprGen<'vir, Curr, Next>,
) -> ExprGen<'vir, Curr, Next> {
self.alloc(ExprGenData::new(self.alloc(ExprKindGenData::Ternary(
self.alloc(TernaryGenData { cond, then, else_ }),
))))
}
pub fn mk_unary_op_expr<'vir, Curr, Next>(
&'vir self,
kind: UnOpKind,
expr: ExprGen<'vir, Curr, Next>,
) -> ExprGen<'vir, Curr, Next> {
self.alloc(ExprGenData::new(self.alloc(ExprKindGenData::UnOp(
self.alloc(UnOpGenData { kind, expr }),
))))
}
pub fn mk_old<'vir, Curr, Next>(
&'vir self,
expr: ExprGen<'vir, Curr, Next>,
label: OldLabel<'vir>,
) -> ExprGen<'vir, Curr, Next> {
self.alloc(ExprGenData::new(self.alloc(ExprKindGenData::Old(
self.alloc(OldGenData { expr, label }),
))))
}
pub fn mk_old_expr<'vir, Curr, Next>(
&'vir self,
expr: ExprGen<'vir, Curr, Next>,
) -> ExprGen<'vir, Curr, Next> {
self.mk_old(expr, OldLabel::None)
}
pub fn mk_old_lhs_expr<'vir, Curr, Next>(
&'vir self,
expr: ExprGen<'vir, Curr, Next>,
) -> ExprGen<'vir, Curr, Next> {
self.mk_old(expr, OldLabel::Lhs)
}
pub fn mk_labelled_old_expr<'vir, Curr, Next>(
&'vir self,
expr: ExprGen<'vir, Curr, Next>,
label: Option<CfgBlockLabelData>,
) -> ExprGen<'vir, Curr, Next> {
self.mk_old(expr, label.map(OldLabel::Block).unwrap_or(OldLabel::None))
}
pub fn mk_local_labelled_old_expr<'vir, Curr, Next>(
&'vir self,
expr: ExprGen<'vir, Curr, Next>,
label: &'vir str,
) -> ExprGen<'vir, Curr, Next> {
self.mk_old(expr, OldLabel::Label(label))
}
pub fn mk_rel_expr<'vir, Curr, Next>(
&'vir self,
expr: ExprGen<'vir, Curr, Next>,
exec: u32,
) -> ExprGen<'vir, Curr, Next> {
let v = self.mk_const_expr(ConstData::Int(exec as u128));
// TODO: the type here is wrong; we cannot just call .typ() though,
// because the arguments may be lazy
self.mk_func_app("rel", &[expr, v], &crate::TypeData::Int)
}
pub fn mk_forall_expr<'vir, Curr, Next>(
&'vir self,
qvars: &'vir [LocalDecl<'vir>],
triggers: &'vir [TriggerGen<'vir, Curr, Next>],
body: ExprGen<'vir, Curr, Next>,
) -> ExprGen<'vir, Curr, Next> {
if qvars.is_empty() {
return body;
}
self.alloc(ExprGenData::new(self.alloc(ExprKindGenData::Forall(
self.alloc(ForallGenData {
qvars,
triggers,
body,
}),
))))
}
pub fn mk_trigger<'vir, Curr, Next>(
&'vir self,
exprs: &[ExprGen<'vir, Curr, Next>],
) -> TriggerGen<'vir, Curr, Next> {
self.alloc(TriggerGenData {
exprs: self.alloc_slice(exprs),
})
}
pub fn mk_let_expr<'vir, Curr, Next>(
&'vir self,
name: &'vir str,
val: ExprGen<'vir, Curr, Next>,
expr: ExprGen<'vir, Curr, Next>,
) -> ExprGen<'vir, Curr, Next> {
let let_expr = self.alloc(ExprGenData::new(self.alloc(ExprKindGenData::Let(
self.alloc(LetGenData { name, val, expr }),
))));
cfg_if! {
if #[cfg(debug_assertions)] {
check_expr_bindings(&mut HashMap::new(), let_expr);
}
}
let_expr
}
pub fn mk_predicate_app_expr<'vir, Curr, Next>(
&'vir self,
pred_app: PredicateAppGen<'vir, Curr, Next>,
) -> ExprGen<'vir, Curr, Next> {
self.alloc(ExprGenData::new(
self.alloc(ExprKindGenData::PredicateApp(pred_app)),
))
}
pub fn mk_wand<'vir, Curr, Next>(
&'vir self,
lhs: ExprGen<'vir, Curr, Next>,
rhs: ExprGen<'vir, Curr, Next>,
) -> WandGen<'vir, Curr, Next> {
self.alloc(WandGenData { lhs, rhs })
}
pub fn mk_wand_expr<'vir, Curr, Next>(
&'vir self,
wand: WandGen<'vir, Curr, Next>,
) -> ExprGen<'vir, Curr, Next> {
self.alloc(ExprGenData::new(self.alloc(ExprKindGenData::Wand(wand))))
}
pub fn mk_bin_op_expr<'vir, Curr, Next>(
&'vir self,
kind: BinOpKind,
lhs: ExprGen<'vir, Curr, Next>,
rhs: ExprGen<'vir, Curr, Next>,
) -> ExprGen<'vir, Curr, Next> {
self.alloc(ExprGenData::new(self.alloc(ExprKindGenData::BinOp(
self.alloc(BinOpGenData { kind, lhs, rhs }),
))))
}
pub fn mk_eq_expr<'vir, Curr, Next>(
&'vir self,
lhs: ExprGen<'vir, Curr, Next>,
rhs: ExprGen<'vir, Curr, Next>,
) -> ExprGen<'vir, Curr, Next> {
self.mk_bin_op_expr(BinOpKind::CmpEq, lhs, rhs)
}
pub fn mk_field_expr<'vir, Curr, Next>(
&'vir self,
recv: ExprGen<'vir, Curr, Next>,
field: Field<'vir>,
) -> ExprGen<'vir, Curr, Next> {
self.alloc(ExprGenData::new(
self.alloc(ExprKindGenData::Field(recv, field)),
))
}
pub fn mk_unfolding_expr<'vir, Curr, Next>(
&'vir self,
target: PredicateAppGen<'vir, Curr, Next>,
expr: ExprGen<'vir, Curr, Next>,
) -> ExprGen<'vir, Curr, Next> {
self.alloc(ExprGenData::new(self.alloc(ExprKindGenData::Unfolding(
self.alloc(UnfoldingGenData { target, expr }),
))))
}
pub fn mk_acc_field_expr<'vir, Curr, Next>(
&'vir self,
recv: ExprGen<'vir, Curr, Next>,
field: Field<'vir>,
perm: Option<ExprGen<'vir, Curr, Next>>,
) -> ExprGen<'vir, Curr, Next> {
self.alloc(ExprGenData::new(self.alloc(ExprKindGenData::AccField(
self.alloc(AccFieldGenData { recv, field, perm }),
))))
}
pub fn mk_const_expr<'vir, Curr, Next>(
&'vir self,
value: ConstData,
) -> ExprGen<'vir, Curr, Next> {
self.alloc(ExprGenData::new(
self.alloc(ExprKindGenData::Const(self.alloc(value))),
))
}
pub fn mk_todo_expr<'vir, Curr, Next>(&'vir self, msg: &'vir str) -> ExprGen<'vir, Curr, Next> {
self.alloc(ExprGenData::new(self.alloc(ExprKindGenData::Todo(msg))))
}
pub const fn mk_bool<'vir, const VALUE: bool>(&'vir self) -> Expr<'vir> {
const_expr!(&ExprKindGenData::Const(&ConstData::Bool(VALUE)))
}
// TODO: can this simply replace mk_bool?
pub const fn mk_bool_gen<'vir, Curr, Next, const VALUE: bool>(
&'vir self,
) -> ExprGen<'vir, Curr, Next> {
const_expr!(&ExprKindGenData::Const(&ConstData::Bool(VALUE)))
}
pub const fn mk_int<'vir, const VALUE: i128>(&'vir self) -> Expr<'vir> {
if VALUE < 0 {
const_expr!(&ExprKindGenData::UnOp(&UnOpData {
kind: UnOpKind::Neg,
expr: const_expr!(&ExprKindGenData::Const(&ConstData::Int((-VALUE) as u128))),
}))
} else {
const_expr!(&ExprKindGenData::Const(&ConstData::Int(VALUE as u128)))
}
}
pub const fn mk_uint<'vir, const VALUE: u128>(&'vir self) -> Expr<'vir> {
const_expr!(&ExprKindGenData::Const(&ConstData::Int(VALUE)))
}
pub const fn mk_wildcard<'vir, Curr, Next>(&'vir self) -> ExprGen<'vir, Curr, Next> {
const_expr!(&ExprKindGenData::Const(&ConstData::Wildcard))
}
pub const fn mk_null<'vir, Curr, Next>(&'vir self) -> ExprGen<'vir, Curr, Next> {
const_expr!(&ExprKindGenData::Const(&ConstData::Null))
}
pub fn mk_result<'vir, Curr, Next>(&'vir self, ty: Type<'vir>) -> ExprGen<'vir, Curr, Next> {
self.alloc(ExprGenData::new(self.alloc(ExprKindGenData::Result(ty))))
}
pub fn mk_field<'vir>(&'vir self, name: &'vir str, ty: Type<'vir>) -> Field<'vir> {
self.alloc(FieldData { name, ty })
}
pub fn mk_domain_axiom<'vir, Curr, Next>(
&'vir self,
name: ViperIdent<'vir>,
expr: ExprGen<'vir, Curr, Next>,
) -> DomainAxiomGen<'vir, Curr, Next> {
self.alloc(DomainAxiomGenData {
name: name.to_str(),
expr,
})
}
pub fn mk_domain_function<'vir, A: Arity<'vir, Arg = Type<'vir>>>(
&'vir self,
ident: FunctionIdent<'vir, A>,
unique: bool,
) -> DomainFunction<'vir> {
self.alloc(DomainFunctionData {
unique,
name: ident.name(),
args: ident.arity().args(),
ret: ident.result_ty(),
})
}
pub fn mk_function<'vir, Curr, Next>(
&'vir self,
name: &'vir str, // TODO: identifiers
args: &'vir [LocalDecl<'vir>],
ret: Type<'vir>,
pres: &'vir [ExprGen<'vir, Curr, Next>],
posts: &'vir [ExprGen<'vir, Curr, Next>],
expr: Option<ExprGen<'vir, Curr, Next>>,
) -> FunctionGen<'vir, Curr, Next> {
// TODO: Typecheck pre and post conditions
if let Some(body) = expr {
if body.ty() != ret {
typecheck_error!(
"Function {} has inconsistent return type. Expected: {:?}, Actual: {:?}",
name,
ret,
body.ty()
);
}
cfg_if! {
if #[cfg(debug_assertions)] {
let mut m = HashMap::new();
for arg in args {
m.insert(arg.name, arg.ty);
}
check_expr_bindings(&mut m, body);
}
}
}
self.alloc(FunctionGenData {
name,
args,
ret,
pres,
posts,
expr,
})
}
pub fn mk_predicate<'vir, Curr, Next, A: Arity<'vir> + CheckTypes<'vir> + Debug>(
&'vir self,
ident: PredicateIdent<'vir, A>,
args: &'vir [LocalDecl<'vir>],
expr: Option<ExprGen<'vir, Curr, Next>>,
) -> PredicateGen<'vir, Curr, Next> {
if !ident.arity().types_match(args) {
typecheck_error!(
"Predicate definition for {} does not match signature.
Signature params: {:?}
Definition params: {:?}
Debug info: {}",
ident.name(),
ident.arity(),
args,
ident.debug_info()
);
}
self.mk_predicate_unchecked(ident.name().to_str(), args, expr)
}
pub fn mk_predicate_unchecked<'vir, Curr, Next>(
&'vir self,
name: &'vir str,
args: &'vir [LocalDecl<'vir>],
expr: Option<ExprGen<'vir, Curr, Next>>,
) -> PredicateGen<'vir, Curr, Next> {
self.alloc(PredicateGenData { name, args, expr })
}
pub fn mk_domain<'vir, Curr, Next>(
&'vir self,
name: ViperIdent<'vir>,
typarams: &'vir [DomainParam<'vir>],
axioms: &'vir [DomainAxiomGen<'vir, Curr, Next>],
functions: &'vir [DomainFunction<'vir>],
) -> DomainGen<'vir, Curr, Next> {
self.alloc(DomainGenData {
name: name.to_str(),
typarams,
axioms,
functions,
})
}
pub fn mk_exhale_stmt<'vir, Curr, Next>(
&'vir self,
expr: ExprGen<'vir, Curr, Next>,
) -> StmtGen<'vir, Curr, Next> {
self.alloc(StmtGenData::new(self.alloc(StmtKindGenData::Exhale(expr))))
}
pub fn mk_unfold_stmt<'vir, Curr, Next>(
&'vir self,
pred_app: PredicateAppGen<'vir, Curr, Next>,
) -> StmtGen<'vir, Curr, Next> {
self.alloc(StmtGenData::new(
self.alloc(StmtKindGenData::Unfold(pred_app)),
))
}
pub fn mk_fold_stmt<'vir, Curr, Next>(
&'vir self,
pred_app: PredicateAppGen<'vir, Curr, Next>,
) -> StmtGen<'vir, Curr, Next> {
self.alloc(StmtGenData::new(
self.alloc(StmtKindGenData::Fold(pred_app)),
))
}
pub fn mk_package_stmt<'vir, Curr, Next>(
&'vir self,
wand: WandGen<'vir, Curr, Next>,
stmts: &'vir [StmtGen<'vir, Curr, Next>],
) -> StmtGen<'vir, Curr, Next> {
self.alloc(StmtGenData::new(
self.alloc(StmtKindGenData::Package(wand, stmts)),
))
}
pub fn mk_apply_stmt<'vir, Curr, Next>(
&'vir self,
wand: WandGen<'vir, Curr, Next>,
) -> StmtGen<'vir, Curr, Next> {
self.alloc(StmtGenData::new(self.alloc(StmtKindGenData::Apply(wand))))
}
pub fn mk_pure_assign_stmt<'vir, Curr, Next>(
&'vir self,
lhs: ExprGen<'vir, Curr, Next>,
rhs: ExprGen<'vir, Curr, Next>,
) -> StmtGen<'vir, Curr, Next> {
if lhs.ty() != rhs.ty() {
typecheck_error!(
"Pure assign statement requires lhs and rhs to have the same type. lhs: {:?}, rhs: {:?}",
lhs.ty(),
rhs.ty()
);
}
self.alloc(StmtGenData::new(self.alloc(StmtKindGenData::PureAssign(
self.alloc(PureAssignGenData { lhs, rhs }),
))))
}
pub fn mk_local_decl_stmt<'vir, Curr, Next>(
&'vir self,
local: LocalDecl<'vir>,
expr: Option<ExprGen<'vir, Curr, Next>>,
) -> StmtGen<'vir, Curr, Next> {
self.alloc(StmtGenData::new(
self.alloc(StmtKindGenData::LocalDecl(local, expr)),
))
}
pub fn mk_if_stmt<'vir, Curr, Next>(
&'vir self,
cond: ExprGen<'vir, Curr, Next>,
then_stmts: &'vir [StmtGen<'vir, Curr, Next>],
else_stmts: &'vir [StmtGen<'vir, Curr, Next>],
) -> StmtGen<'vir, Curr, Next> {
self.alloc(StmtGenData::new(
self.alloc(StmtKindGenData::If(cond, then_stmts, else_stmts)),
))
}
pub fn mk_label_stmt<'vir, Curr, Next>(
&'vir self,
label: &'vir str,
) -> StmtGen<'vir, Curr, Next> {
self.alloc(StmtGenData::new(self.alloc(StmtKindGenData::Label(label))))
}
pub fn mk_assume_false_stmt<'vir, Curr, Next>(
&'vir self,
) -> TerminatorStmtGen<'vir, Curr, Next> {
self.alloc(TerminatorStmtGenData::AssumeFalse)
}
pub fn mk_goto_stmt<'vir, Curr, Next>(
&'vir self,
block: CfgBlockLabel<'vir>,
) -> TerminatorStmtGen<'vir, Curr, Next> {
self.alloc(TerminatorStmtGenData::Goto(block))
}
pub fn mk_dummy_stmt<'vir, Curr, Next>(
&'vir self,
msg: &'vir str,
) -> TerminatorStmtGen<'vir, Curr, Next> {
self.alloc(TerminatorStmtGenData::Dummy(msg))
}
pub fn mk_comment_stmt<'vir, Curr, Next>(
&'vir self,
msg: &'vir str,
) -> StmtGen<'vir, Curr, Next> {
self.alloc(StmtGenData::new(self.alloc(StmtKindGenData::Comment(msg))))
}
pub fn mk_goto_if_stmt<'vir, Curr, Next>(
&'vir self,
value: ExprGen<'vir, Curr, Next>,
targets: &'vir [GotoIfTargetGen<'vir, Curr, Next>],
otherwise: CfgBlockLabel<'vir>,
otherwise_statements: &'vir [StmtGen<'vir, Curr, Next>],
) -> TerminatorStmtGen<'vir, Curr, Next> {
self.alloc(TerminatorStmtGenData::GotoIf(self.alloc(GotoIfGenData {
value,
targets,
otherwise,
otherwise_statements,
})))
}
pub fn mk_goto_if_target<'vir, Curr, Next>(
&'vir self,
value: ExprGen<'vir, Curr, Next>,
label: CfgBlockLabel<'vir>,
statements: &'vir [StmtGen<'vir, Curr, Next>],
) -> GotoIfTargetGen<'vir, Curr, Next> {
self.alloc(GotoIfTargetGenData {
value,
label,
statements,
})
}
pub fn mk_cfg_block<'vir, Curr, Next>(
&'vir self,
label: CfgBlockLabel<'vir>,
invariants: &'vir [ExprGen<'vir, Curr, Next>],
stmts: &'vir [StmtGen<'vir, Curr, Next>],
terminator: TerminatorStmtGen<'vir, Curr, Next>,
) -> CfgBlockGen<'vir, Curr, Next> {
let label = self.alloc(CfgLabelGenData { label, invariants });
self.alloc(CfgBlockGenData {
label,
stmts,
terminator,
})
}
pub fn mk_method<'vir, Curr, Next, A: Arity<'vir> + CheckTypes<'vir> + Debug>(
&'vir self,
ident: MethodIdent<'vir, A>,
args: &'vir [LocalDecl<'vir>],
rets: &'vir [LocalDecl<'vir>],
pres: &'vir [ExprGen<'vir, Curr, Next>],
posts: &'vir [ExprGen<'vir, Curr, Next>],
blocks: Option<&'vir [CfgBlockGen<'vir, Curr, Next>]>, // first one is the entrypoint
) -> MethodGen<'vir, Curr, Next> {
if !ident.arity().types_match(args) {
typecheck_error!(
"Method {} could not be created. Identifier arity: {:?}, Method decls: {args:?}",
ident.name_str(),
ident.arity()
);
}
self.mk_method_unchecked(ident.name().to_str(), args, rets, pres, posts, blocks)
}
pub fn mk_method_unchecked<'vir, Curr, Next>(
&'vir self,
name: &'vir str,
args: &'vir [LocalDecl<'vir>],
rets: &'vir [LocalDecl<'vir>],
pres: &'vir [ExprGen<'vir, Curr, Next>],
posts: &'vir [ExprGen<'vir, Curr, Next>],
blocks: Option<&'vir [CfgBlockGen<'vir, Curr, Next>]>, // first one is the entrypoint
) -> MethodGen<'vir, Curr, Next> {
cfg_if! {
if #[cfg(debug_assertions)] {
if let Some(blocks) = blocks {
let mut m = HashMap::new();
for arg in args {
m.insert(arg.name, arg.ty);
}
for block in blocks {
for stmt in block.stmts {
check_stmt_bindings(&mut m, stmt);
}
}
}
}
}
self.alloc(MethodGenData {
name,
args,
rets,
pres,
posts,
body: blocks.map(|blocks| self.alloc(MethodBodyGenData { blocks })),
})
}
pub fn mk_program<'vir, Curr, Next>(
&'vir self,
fields: &'vir [Field<'vir>],
domains: &'vir [DomainGen<'vir, Curr, Next>],
predicates: &'vir [PredicateGen<'vir, Curr, Next>],
functions: &'vir [FunctionGen<'vir, Curr, Next>],
methods: &'vir [MethodGen<'vir, Curr, Next>],
) -> ProgramGen<'vir, Curr, Next> {
self.alloc(ProgramGenData {
fields,
domains,
predicates,
functions,
methods,
})
}
pub fn mk_conj<'vir, Curr, Next>(
&'vir self,
elems: &[ExprGen<'vir, Curr, Next>],
) -> ExprGen<'vir, Curr, Next> {
elems
.split_last()
.map(|(last, rest)| {
rest.iter()
.rfold(*last, |acc, e| self.mk_bin_op_expr(BinOpKind::And, *e, acc))
})
.unwrap_or_else(|| self.mk_bool_gen::<Curr, Next, true>())
}
pub fn mk_disj<'vir>(&'vir self, elems: &[Expr<'vir>]) -> Expr<'vir> {
elems
.split_last()
.map(|(last, rest)| {
rest.iter()
.rfold(*last, |acc, e| self.mk_bin_op_expr(BinOpKind::Or, *e, acc))
})
.unwrap_or_else(|| self.mk_bool::<false>())
}
const fn get_int_data(rust_ty: &ty::TyKind) -> (u32, bool) {
match rust_ty {
ty::Int(ty::IntTy::Isize) => ((std::mem::size_of::<isize>() * 8) as u32, true),
ty::Int(ty::IntTy::I8) => (8, true),
ty::Int(ty::IntTy::I16) => (16, true),
ty::Int(ty::IntTy::I32) => (32, true),
ty::Int(ty::IntTy::I64) => (64, true),
ty::Int(ty::IntTy::I128) => (128, true),
ty::Uint(ty::UintTy::Usize) => ((std::mem::size_of::<usize>() * 8) as u32, false),
ty::Uint(ty::UintTy::U8) => (8, false),
ty::Uint(ty::UintTy::U16) => (16, false),
ty::Uint(ty::UintTy::U32) => (32, false),
ty::Uint(ty::UintTy::U64) => (64, false),
ty::Uint(ty::UintTy::U128) => (128, false),
_ => unreachable!(),
}
}
pub const fn get_min_int<'vir>(&'vir self, rust_ty: &ty::TyKind) -> Expr<'vir> {
match Self::get_int_data(rust_ty) {
(_, false) => self.mk_uint::<0>(),
(i8::BITS, true) => self.mk_int::<{ i8::MIN as i128 }>(),
(i16::BITS, true) => self.mk_int::<{ i16::MIN as i128 }>(),
(i32::BITS, true) => self.mk_int::<{ i32::MIN as i128 }>(),
(i64::BITS, true) => self.mk_int::<{ i64::MIN as i128 }>(),
(i128::BITS, true) => self.mk_int::<{ i128::MIN }>(),
(_, true) => unreachable!(),
}
}
pub const fn get_max_int<'vir>(&'vir self, rust_ty: &ty::TyKind) -> Expr<'vir> {
match Self::get_int_data(rust_ty) {
(u8::BITS, false) => self.mk_uint::<{ u8::MAX as u128 }>(),
(u16::BITS, false) => self.mk_uint::<{ u16::MAX as u128 }>(),
(u32::BITS, false) => self.mk_uint::<{ u32::MAX as u128 }>(),
(u64::BITS, false) => self.mk_uint::<{ u64::MAX as u128 }>(),
(u128::BITS, false) => self.mk_uint::<{ u128::MAX }>(),
(i8::BITS, true) => self.mk_int::<{ i8::MAX as i128 }>(),
(i16::BITS, true) => self.mk_int::<{ i16::MAX as i128 }>(),
(i32::BITS, true) => self.mk_int::<{ i32::MAX as i128 }>(),
(i64::BITS, true) => self.mk_int::<{ i64::MAX as i128 }>(),
(i128::BITS, true) => self.mk_int::<{ i128::MAX }>(),
_ => unreachable!(),
}
}
pub fn get_modulo_int<'vir>(&'vir self, rust_ty: &ty::TyKind) -> Expr<'vir> {
match Self::get_int_data(rust_ty) {
(u8::BITS, _) => self.mk_uint::<{ 1_u128 << u8::BITS }>(),
(u16::BITS, _) => self.mk_uint::<{ 1_u128 << u16::BITS }>(),
(u32::BITS, _) => self.mk_uint::<{ 1_u128 << u32::BITS }>(),
(u64::BITS, _) => self.mk_uint::<{ 1_u128 << u64::BITS }>(),
(u128::BITS, _) => {
// TODO: make this a `const` once `Expr` isn't invariant in `'vir` so that it can be `'const` instead
let half = self.mk_uint::<{ 1_u128 << u64::BITS }>();
self.mk_bin_op_expr(BinOpKind::Add, half, half)
}
_ => unreachable!(),
}
}
pub fn get_signed_shift_int<'vir>(&'vir self, rust_ty: &ty::TyKind) -> Option<Expr<'vir>> {
let int = match Self::get_int_data(rust_ty) {
(_, false) => return None,
(u8::BITS, true) => self.mk_uint::<{ 1_u128 << (u8::BITS - 1) }>(),
(u16::BITS, true) => self.mk_uint::<{ 1_u128 << (u16::BITS - 1) }>(),
(u32::BITS, true) => self.mk_uint::<{ 1_u128 << (u32::BITS - 1) }>(),
(u64::BITS, true) => self.mk_uint::<{ 1_u128 << (u64::BITS - 1) }>(),
(u128::BITS, true) => self.mk_uint::<{ 1_u128 << (u128::BITS - 1) }>(),
(_, true) => unreachable!(),
};
Some(int)
}
pub fn get_bit_width_int<'vir>(&'vir self, rust_ty: &ty::TyKind) -> Expr<'vir> {
match Self::get_int_data(rust_ty) {
(u8::BITS, _) => self.mk_uint::<{ u8::BITS as u128 }>(),
(u16::BITS, _) => self.mk_uint::<{ u16::BITS as u128 }>(),
(u32::BITS, _) => self.mk_uint::<{ u32::BITS as u128 }>(),
(u64::BITS, _) => self.mk_uint::<{ u64::BITS as u128 }>(),
(u128::BITS, _) => self.mk_uint::<{ u128::BITS as u128 }>(),
_ => unreachable!(),
}
}
}