forked from dafny-lang/dafny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoogieGenerator.ExpressionTranslator.cs
2349 lines (2200 loc) · 131 KB
/
BoogieGenerator.ExpressionTranslator.cs
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
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Numerics;
using Microsoft.BaseTypes;
using Microsoft.Boogie;
using static Microsoft.Dafny.Util;
namespace Microsoft.Dafny {
public partial class BoogieGenerator {
public class ExpressionTranslator {
private DafnyOptions options;
// HeapExpr == null ==> translation of pure (no-heap) expression
readonly Boogie.Expr _the_heap_expr;
public Boogie.Expr HeapExpr {
// The increment of Statistics_HeapUses in the following line is a hack and not entirely a good idea.
// Not only does one need to be careful not to mention HeapExpr in contracts (in particular, in ObjectInvariant()
// below), but also, the debugger may invoke HeapExpr and that will cause an increment as well.
get { Statistics_HeapUses++; return _the_heap_expr; }
}
/// <summary>
/// Return HeapExpr as an IdentifierExpr.
/// CAUTION: This getter should be used only if the caller "knows" that HeapExpr really is an IdentifierExpr.
/// </summary>
public Boogie.IdentifierExpr HeapCastToIdentifierExpr {
get {
Contract.Assume(HeapExpr is Boogie.IdentifierExpr);
return (Boogie.IdentifierExpr)HeapExpr;
}
}
public readonly PredefinedDecls predef;
public readonly BoogieGenerator BoogieGenerator;
public readonly string This;
public readonly string readsFrame; // the name of the context's frame variable for reading state.
// May be null to indicate the context's reads frame is * and doesn't require any reads checks.
public readonly string modifiesFrame; // the name of the context's frame variable for writing state.
readonly Function applyLimited_CurrentFunction;
internal readonly FuelSetting layerInterCluster;
internal readonly FuelSetting layerIntraCluster = null; // a value of null says to do the same as for inter-cluster calls
public int Statistics_CustomLayerFunctionCount = 0;
public int Statistics_HeapAsQuantifierCount = 0;
public int Statistics_HeapUses = 0;
public readonly bool stripLits = false;
[ContractInvariantMethod]
void ObjectInvariant() {
// In the following line, it is important to use _the_heap_expr directly, rather than HeapExpr, because
// the HeapExpr getter has a side effect on Statistics_HeapUses.
Contract.Invariant(_the_heap_expr == null || _the_heap_expr is Boogie.OldExpr || _the_heap_expr is Boogie.IdentifierExpr);
Contract.Invariant(predef != null);
Contract.Invariant(BoogieGenerator != null);
Contract.Invariant(This != null);
Contract.Invariant(modifiesFrame != null);
Contract.Invariant(layerInterCluster != null);
Contract.Invariant(0 <= Statistics_CustomLayerFunctionCount);
}
/// <summary>
/// This is the most general constructor. It is private and takes all the parameters. Whenever
/// one ExpressionTranslator is constructed from another, unchanged parameters are just copied in.
/// </summary>
ExpressionTranslator(BoogieGenerator boogieGenerator, PredefinedDecls predef, Boogie.Expr heap, string thisVar,
Function applyLimited_CurrentFunction, FuelSetting layerInterCluster, FuelSetting layerIntraCluster, string readsFrame, string modifiesFrame, bool stripLits) {
Contract.Requires(boogieGenerator != null);
Contract.Requires(predef != null);
Contract.Requires(thisVar != null);
Contract.Requires(readsFrame != null);
Contract.Requires(modifiesFrame != null);
this.BoogieGenerator = boogieGenerator;
this.predef = predef;
this._the_heap_expr = heap;
this.This = thisVar;
this.applyLimited_CurrentFunction = applyLimited_CurrentFunction;
this.layerInterCluster = layerInterCluster;
if (layerIntraCluster == null) {
this.layerIntraCluster = layerInterCluster;
} else {
this.layerIntraCluster = layerIntraCluster;
}
this.readsFrame = readsFrame;
this.modifiesFrame = modifiesFrame;
this.stripLits = stripLits;
this.options = boogieGenerator.options;
}
public static Boogie.IdentifierExpr HeapIdentifierExpr(PredefinedDecls predef, Boogie.IToken heapToken) {
return new Boogie.IdentifierExpr(heapToken, predef.HeapVarName, predef.HeapType);
}
public ExpressionTranslator(BoogieGenerator boogieGenerator, PredefinedDecls predef, Boogie.IToken heapToken)
: this(boogieGenerator, predef, HeapIdentifierExpr(predef, heapToken)) {
Contract.Requires(boogieGenerator != null);
Contract.Requires(predef != null);
Contract.Requires(heapToken != null);
}
public ExpressionTranslator(BoogieGenerator boogieGenerator, PredefinedDecls predef, Boogie.Expr heap)
: this(boogieGenerator, predef, heap, "this") {
Contract.Requires(boogieGenerator != null);
Contract.Requires(predef != null);
}
public ExpressionTranslator(BoogieGenerator boogieGenerator, PredefinedDecls predef, Boogie.Expr heap, Boogie.Expr oldHeap)
: this(boogieGenerator, predef, heap, "this") {
Contract.Requires(boogieGenerator != null);
Contract.Requires(predef != null);
Contract.Requires(oldHeap != null);
var old = new ExpressionTranslator(boogieGenerator, predef, oldHeap);
old.oldEtran = old;
this.oldEtran = old;
}
public ExpressionTranslator(BoogieGenerator boogieGenerator, PredefinedDecls predef, Boogie.Expr heap, string thisVar)
: this(boogieGenerator, predef, heap, thisVar, null, new FuelSetting(boogieGenerator, 1), null, "$_ReadsFrame", "$_ModifiesFrame", false) {
Contract.Requires(boogieGenerator != null);
Contract.Requires(predef != null);
Contract.Requires(thisVar != null);
}
public ExpressionTranslator(ExpressionTranslator etran, Boogie.Expr heap)
: this(etran.BoogieGenerator, etran.predef, heap, etran.This, etran.applyLimited_CurrentFunction, etran.layerInterCluster, etran.layerIntraCluster, etran.readsFrame, etran.modifiesFrame, etran.stripLits) {
Contract.Requires(etran != null);
}
public ExpressionTranslator WithReadsFrame(string newReadsFrame) {
return new ExpressionTranslator(BoogieGenerator, predef, HeapExpr, This, applyLimited_CurrentFunction, layerInterCluster, layerIntraCluster, newReadsFrame, modifiesFrame, stripLits);
}
public ExpressionTranslator WithModifiesFrame(string newModifiesFrame) {
return new ExpressionTranslator(BoogieGenerator, predef, HeapExpr, This, applyLimited_CurrentFunction, layerInterCluster, layerIntraCluster, readsFrame, newModifiesFrame, stripLits);
}
internal IToken GetToken(Expression expression) {
return BoogieGenerator.GetToken(expression);
}
ExpressionTranslator oldEtran;
public ExpressionTranslator Old {
get {
Contract.Ensures(Contract.Result<ExpressionTranslator>() != null);
if (oldEtran == null) {
oldEtran = new ExpressionTranslator(BoogieGenerator, predef, new Boogie.OldExpr(HeapExpr.tok, HeapExpr), This, applyLimited_CurrentFunction, layerInterCluster, layerIntraCluster, readsFrame, modifiesFrame, stripLits);
oldEtran.oldEtran = oldEtran;
}
return oldEtran;
}
}
public ExpressionTranslator OldAt(Label/*?*/ label) {
Contract.Ensures(Contract.Result<ExpressionTranslator>() != null);
if (label == null) {
return Old;
}
var heapAt = new Boogie.IdentifierExpr(Token.NoToken, "$Heap_at_" + label.AssignUniqueId(BoogieGenerator.CurrentIdGenerator), predef.HeapType);
return new ExpressionTranslator(BoogieGenerator, predef, heapAt, This, applyLimited_CurrentFunction, layerInterCluster, layerIntraCluster, readsFrame, modifiesFrame, stripLits);
}
public bool UsesOldHeap {
get {
return HeapExpr is Boogie.OldExpr || (HeapExpr is Boogie.IdentifierExpr ide && ide.Name.StartsWith("$Heap_at_"));
}
}
public ExpressionTranslator WithLayer(Boogie.Expr layerArgument) {
// different layer and 0 fuel amount.
Contract.Requires(layerArgument != null);
Contract.Ensures(Contract.Result<ExpressionTranslator>() != null);
return CloneExpressionTranslator(this, BoogieGenerator, predef, HeapExpr, This, null, new FuelSetting(BoogieGenerator, 0, layerArgument), new FuelSetting(BoogieGenerator, 0, layerArgument), readsFrame, modifiesFrame, stripLits);
}
internal ExpressionTranslator WithCustomFuelSetting(CustomFuelSettings customSettings) {
// Use the existing layers but with some per-function customizations
Contract.Requires(customSettings != null);
Contract.Ensures(Contract.Result<ExpressionTranslator>() != null);
return CloneExpressionTranslator(this, BoogieGenerator, predef, HeapExpr, This, null, layerInterCluster.WithContext(customSettings), layerIntraCluster.WithContext(customSettings), readsFrame, modifiesFrame, stripLits);
}
public ExpressionTranslator ReplaceLayer(Boogie.Expr layerArgument) {
// different layer with same fuel amount.
Contract.Requires(layerArgument != null);
Contract.Ensures(Contract.Result<ExpressionTranslator>() != null);
return CloneExpressionTranslator(this, BoogieGenerator, predef, HeapExpr, This, applyLimited_CurrentFunction, layerInterCluster.WithLayer(layerArgument), layerIntraCluster.WithLayer(layerArgument), readsFrame, modifiesFrame, stripLits);
}
public ExpressionTranslator WithNoLits() {
Contract.Ensures(Contract.Result<ExpressionTranslator>() != null);
return CloneExpressionTranslator(this, BoogieGenerator, predef, HeapExpr, This, applyLimited_CurrentFunction, layerInterCluster, layerIntraCluster, readsFrame, modifiesFrame, true);
}
public ExpressionTranslator LimitedFunctions(Function applyLimited_CurrentFunction, Boogie.Expr layerArgument) {
Contract.Requires(applyLimited_CurrentFunction != null);
Contract.Requires(layerArgument != null);
Contract.Ensures(Contract.Result<ExpressionTranslator>() != null);
return CloneExpressionTranslator(this, BoogieGenerator, predef, HeapExpr, This, applyLimited_CurrentFunction, /* layerArgument */ layerInterCluster, new FuelSetting(BoogieGenerator, 0, layerArgument), readsFrame, modifiesFrame, stripLits);
}
public ExpressionTranslator LayerOffset(int offset) {
Contract.Requires(0 <= offset);
Contract.Ensures(Contract.Result<ExpressionTranslator>() != null);
return CloneExpressionTranslator(this, BoogieGenerator, predef, HeapExpr, This, applyLimited_CurrentFunction, layerInterCluster.Offset(offset), layerIntraCluster, readsFrame, modifiesFrame, stripLits);
}
public ExpressionTranslator DecreaseFuel(int offset) {
Contract.Requires(0 <= offset);
Contract.Ensures(Contract.Result<ExpressionTranslator>() != null);
return CloneExpressionTranslator(this, BoogieGenerator, predef, HeapExpr, This, applyLimited_CurrentFunction, layerInterCluster.Decrease(offset), layerIntraCluster, readsFrame, modifiesFrame, stripLits);
}
private static ExpressionTranslator CloneExpressionTranslator(ExpressionTranslator orig,
BoogieGenerator boogieGenerator, PredefinedDecls predef, Boogie.Expr heap, string thisVar,
Function applyLimited_CurrentFunction, FuelSetting layerInterCluster, FuelSetting layerIntraCluster, string readsFrame, string modifiesFrame, bool stripLits) {
var et = new ExpressionTranslator(boogieGenerator, predef, heap, thisVar, applyLimited_CurrentFunction, layerInterCluster, layerIntraCluster, readsFrame, modifiesFrame, stripLits);
if (orig.oldEtran != null) {
var etOld = new ExpressionTranslator(boogieGenerator, predef, orig.Old.HeapExpr, thisVar, applyLimited_CurrentFunction, layerInterCluster, layerIntraCluster, readsFrame, modifiesFrame, stripLits);
etOld.oldEtran = etOld;
et.oldEtran = etOld;
}
return et;
}
public Boogie.IdentifierExpr ReadsFrame(IToken tok) {
Contract.Requires(tok != null);
Contract.Ensures(Contract.Result<Boogie.IdentifierExpr>() != null);
Contract.Ensures(Contract.Result<Boogie.IdentifierExpr>().Type != null);
if (readsFrame == null) {
throw new ArgumentException();
}
return Frame(tok, readsFrame);
}
public Boogie.IdentifierExpr ModifiesFrame(IToken tok) {
Contract.Requires(tok != null);
Contract.Ensures(Contract.Result<Boogie.IdentifierExpr>() != null);
Contract.Ensures(Contract.Result<Boogie.IdentifierExpr>().Type != null);
return Frame(tok, modifiesFrame);
}
private Boogie.IdentifierExpr Frame(IToken tok, string frameName) {
Contract.Requires(tok != null);
Contract.Ensures(Contract.Result<Boogie.IdentifierExpr>() != null);
Contract.Ensures(Contract.Result<Boogie.IdentifierExpr>().Type != null);
Boogie.TypeVariable alpha = new Boogie.TypeVariable(tok, "beta");
Boogie.Type fieldAlpha = predef.FieldName(tok, alpha);
Boogie.Type ty = new Boogie.MapType(tok, new List<TypeVariable> { alpha }, new List<Boogie.Type> { predef.RefType, fieldAlpha }, Boogie.Type.Bool);
return new Boogie.IdentifierExpr(tok, frameName, ty);
}
public Boogie.IdentifierExpr ArbitraryBoxValue() {
Contract.Ensures(Contract.Result<Boogie.IdentifierExpr>() != null);
return new Boogie.IdentifierExpr(Token.NoToken, "$ArbitraryBoxValue", predef.BoxType);
}
public Boogie.Expr ArbitraryValue(Type type) {
Contract.Ensures(Contract.Result<Boogie.Expr>() != null);
var bx = ArbitraryBoxValue();
if (!ModeledAsBoxType(type)) {
return BoogieGenerator.FunctionCall(Token.NoToken, BuiltinFunction.Unbox, BoogieGenerator.TrType(type), bx);
} else {
return bx;
}
}
public Boogie.IdentifierExpr FunctionContextHeight() {
Contract.Ensures(Contract.Result<Boogie.IdentifierExpr>().Type != null);
return new Boogie.IdentifierExpr(Token.NoToken, "$FunctionContextHeight", Boogie.Type.Int);
}
public Boogie.Expr HeightContext(ICallable m) {
Contract.Requires(m != null);
// free requires fh == FunctionContextHeight;
var module = m.EnclosingModule;
Boogie.Expr context =
Boogie.Expr.Eq(Boogie.Expr.Literal(module.CallGraph.GetSCCRepresentativePredecessorCount(m)), FunctionContextHeight());
return context;
}
public Expression GetSubstitutedBody(LetExpr e) {
Contract.Requires(e != null);
Contract.Requires(e.Exact);
Contract.Assert(e.LHSs.Count == e.RHSs.Count); // checked by resolution
var substMap = new Dictionary<IVariable, Expression>();
for (int i = 0; i < e.LHSs.Count; i++) {
BoogieGenerator.AddCasePatternVarSubstitutions(e.LHSs[i], TrExpr(e.RHSs[i]), substMap);
}
return BoogieGenerator.Substitute(e.Body, null, substMap);
}
public Expr MaybeLit(Expr expr, Boogie.Type type) {
return stripLits ? expr : BoogieGenerator.Lit(expr, type);
}
public Expr MaybeLit(Expr expr) {
return stripLits ? expr : BoogieGenerator.Lit(expr);
}
/// <summary>
/// Translates Dafny expression "expr" into a Boogie expression. If the type of "expr" can be a boolean, then the
/// token (source location) of the resulting expression is filled in (it wouldn't hurt if the token were always
/// filled in, but it is really necessary for anything that may show up in a Boogie assert, since that location may
/// then show up in an error message).
/// </summary>
public Boogie.Expr TrExpr(Expression expr) {
Contract.Requires(expr != null);
Contract.Requires(predef != null);
switch (expr) {
case LiteralExpr literalExpr: {
LiteralExpr e = literalExpr;
if (e.Value == null) {
return predef.Null;
} else if (e.Value is bool) {
return MaybeLit(new Boogie.LiteralExpr(GetToken(e), (bool)e.Value));
} else if (e is CharLiteralExpr) {
// we expect e.Value to be a string representing exactly one char
Boogie.Expr rawElement = null; // assignment to please compiler's definite assignment rule
foreach (var ch in Util.UnescapedCharacters(options, (string)e.Value, false)) {
Contract.Assert(rawElement == null); // we should get here only once
rawElement = BoogieGenerator.FunctionCall(GetToken(literalExpr), BuiltinFunction.CharFromInt, null, Boogie.Expr.Literal(ch));
}
Contract.Assert(rawElement != null); // there should have been an iteration of the loop above
return MaybeLit(rawElement, predef.CharType);
} else if (e is StringLiteralExpr) {
var str = (StringLiteralExpr)e;
Boogie.Expr seq = BoogieGenerator.FunctionCall(GetToken(literalExpr), BuiltinFunction.SeqEmpty, predef.BoxType);
foreach (var ch in Util.UnescapedCharacters(options, (string)e.Value, str.IsVerbatim)) {
var rawElement = BoogieGenerator.FunctionCall(GetToken(literalExpr), BuiltinFunction.CharFromInt, null, Boogie.Expr.Literal(ch));
Boogie.Expr elt = BoxIfNecessary(GetToken(literalExpr), rawElement, Type.Char);
seq = BoogieGenerator.FunctionCall(GetToken(literalExpr), BuiltinFunction.SeqBuild, predef.BoxType, seq, elt);
}
return MaybeLit(seq, BoogieGenerator.TrType(new SeqType(Type.Char)));
} else if (e.Value is BigInteger) {
var n = Microsoft.BaseTypes.BigNum.FromBigInt((BigInteger)e.Value);
if (e.Type is BitvectorType) {
return MaybeLit(BoogieGenerator.BplBvLiteralExpr(GetToken(e), n, e.Type.AsBitVectorType));
} else if (e.Type.IsBigOrdinalType) {
var fromNat = FunctionCall(GetToken(literalExpr), "ORD#FromNat", predef.BigOrdinalType, Boogie.Expr.Literal(n));
return MaybeLit(fromNat, predef.BigOrdinalType);
} else {
return MaybeLit(Boogie.Expr.Literal(n));
}
} else if (e.Value is BaseTypes.BigDec) {
return MaybeLit(Boogie.Expr.Literal((BaseTypes.BigDec)e.Value));
} else {
Contract.Assert(false); throw new cce.UnreachableException(); // unexpected literal
}
}
case ThisExpr:
return new Boogie.IdentifierExpr(GetToken(expr), This, BoogieGenerator.TrType(expr.Type));
case IdentifierExpr identifierExpr: {
IdentifierExpr e = identifierExpr;
Contract.Assert(e.Var != null);
return BoogieGenerator.TrVar(GetToken(identifierExpr), e.Var);
}
case BoogieWrapper wrapper: {
var e = wrapper;
return e.Expr;
}
case BoogieFunctionCall call: {
var e = call;
var id = new Boogie.IdentifierExpr(GetToken(e), e.FunctionName, BoogieGenerator.TrType(e.Type));
var args = new List<Boogie.Expr>();
foreach (var arg in e.TyArgs) {
args.Add(BoogieGenerator.TypeToTy(arg));
}
if (e.UsesHeap) {
args.Add(HeapExpr);
}
if (e.UsesOldHeap) {
args.Add(Old.HeapExpr);
}
foreach (var heapAtLabel in e.HeapAtLabels) {
var bv = BplBoundVar("$Heap_at_" + heapAtLabel.AssignUniqueId(BoogieGenerator.CurrentIdGenerator), BoogieGenerator.predef.HeapType, out var ve);
args.Add(ve);
}
foreach (var arg in e.Args) {
args.Add(TrExpr(arg));
}
return new Boogie.NAryExpr(GetToken(e), new Boogie.FunctionCall(id), args);
}
case SetDisplayExpr displayExpr: {
SetDisplayExpr e = displayExpr;
Boogie.Expr s = BoogieGenerator.FunctionCall(GetToken(displayExpr), e.Finite ? BuiltinFunction.SetEmpty : BuiltinFunction.ISetEmpty, predef.BoxType);
var isLit = true;
foreach (Expression ee in e.Elements) {
var rawElement = TrExpr(ee);
isLit = isLit && BoogieGenerator.IsLit(rawElement);
Boogie.Expr ss = BoxIfNecessary(GetToken(displayExpr), rawElement, cce.NonNull(ee.Type));
s = BoogieGenerator.FunctionCall(GetToken(displayExpr), e.Finite ? BuiltinFunction.SetUnionOne : BuiltinFunction.ISetUnionOne, predef.BoxType, s, ss);
}
if (isLit) {
// Lit-lifting: All elements are lit, so the set is Lit too
s = MaybeLit(s, predef.BoxType);
}
return s;
}
case MultiSetDisplayExpr displayExpr: {
MultiSetDisplayExpr e = displayExpr;
Boogie.Expr s = BoogieGenerator.FunctionCall(GetToken(displayExpr), BuiltinFunction.MultiSetEmpty, predef.BoxType);
var isLit = true;
foreach (Expression ee in e.Elements) {
var rawElement = TrExpr(ee);
isLit = isLit && BoogieGenerator.IsLit(rawElement);
Boogie.Expr ss = BoxIfNecessary(GetToken(displayExpr), rawElement, cce.NonNull(ee.Type));
s = BoogieGenerator.FunctionCall(GetToken(displayExpr), BuiltinFunction.MultiSetUnionOne, predef.BoxType, s, ss);
}
if (isLit) {
// Lit-lifting: All elements are lit, so the multiset is Lit too
s = MaybeLit(s, predef.BoxType);
}
return s;
}
case SeqDisplayExpr displayExpr: {
SeqDisplayExpr e = displayExpr;
// Note: a LiteralExpr(string) is really another kind of SeqDisplayExpr
Boogie.Expr s = BoogieGenerator.FunctionCall(GetToken(displayExpr), BuiltinFunction.SeqEmpty, predef.BoxType);
var isLit = true;
foreach (Expression ee in e.Elements) {
var rawElement = TrExpr(ee);
isLit = isLit && BoogieGenerator.IsLit(rawElement);
Boogie.Expr elt = BoxIfNecessary(GetToken(displayExpr), rawElement, ee.Type);
s = BoogieGenerator.FunctionCall(GetToken(displayExpr), BuiltinFunction.SeqBuild, predef.BoxType, s, elt);
}
if (isLit) {
// Lit-lifting: All elements are lit, so the sequence is Lit too
s = MaybeLit(s, predef.BoxType);
}
return s;
}
case MapDisplayExpr displayExpr: {
MapDisplayExpr e = displayExpr;
Boogie.Type maptype = predef.MapType(GetToken(displayExpr), e.Finite, predef.BoxType, predef.BoxType);
Boogie.Expr s = BoogieGenerator.FunctionCall(GetToken(displayExpr), e.Finite ? BuiltinFunction.MapEmpty : BuiltinFunction.IMapEmpty, predef.BoxType);
var isLit = true;
foreach (ExpressionPair p in e.Elements) {
var rawA = TrExpr(p.A);
var rawB = TrExpr(p.B);
isLit = isLit && BoogieGenerator.IsLit(rawA) && BoogieGenerator.IsLit(rawB);
Boogie.Expr elt = BoxIfNecessary(GetToken(displayExpr), rawA, cce.NonNull(p.A.Type));
Boogie.Expr elt2 = BoxIfNecessary(GetToken(displayExpr), rawB, cce.NonNull(p.B.Type));
s = FunctionCall(GetToken(displayExpr), e.Finite ? "Map#Build" : "IMap#Build", maptype, s, elt, elt2);
}
if (isLit) {
// Lit-lifting: All keys and values are lit, so the map is Lit too
s = MaybeLit(s, predef.BoxType);
}
return s;
}
case MemberSelectExpr selectExpr: {
var e = selectExpr;
return e.MemberSelectCase(
field => {
var useSurrogateLocal = BoogieGenerator.inBodyInitContext && Expression.AsThis(e.Obj) != null && !field.IsInstanceIndependentConstant;
var fType = BoogieGenerator.TrType(field.Type);
if (useSurrogateLocal) {
return new Boogie.IdentifierExpr(GetToken(expr), BoogieGenerator.SurrogateName(field), fType);
} else if (field is ConstantField) {
var typeMap = e.TypeArgumentSubstitutionsWithParents();
var args = GetTypeParams(field.EnclosingClass).ConvertAll(tp => BoogieGenerator.TypeToTy(typeMap[tp]));
Boogie.Expr result;
if (field.IsStatic) {
result = new Boogie.NAryExpr(GetToken(expr), new Boogie.FunctionCall(BoogieGenerator.GetReadonlyField(field)), args);
} else {
Boogie.Expr obj = BoogieGenerator.BoxifyForTraitParent(e.tok, TrExpr(e.Obj), e.Member, e.Obj.Type);
args.Add(obj);
result = new Boogie.NAryExpr(GetToken(expr), new Boogie.FunctionCall(BoogieGenerator.GetReadonlyField(field)), args);
}
result = BoogieGenerator.CondApplyUnbox(GetToken(expr), result, field.Type, expr.Type);
return result;
} else {
Boogie.Expr obj = TrExpr(e.Obj);
Boogie.Expr result;
if (field.IsMutable) {
result = ReadHeap(GetToken(expr), HeapExpr, obj, new Boogie.IdentifierExpr(GetToken(expr), BoogieGenerator.GetField(field)), fType);
return BoogieGenerator.CondApplyUnbox(GetToken(expr), result, field.Type, expr.Type);
} else {
result = new Boogie.NAryExpr(GetToken(expr), new Boogie.FunctionCall(BoogieGenerator.GetReadonlyField(field)),
new List<Boogie.Expr> { obj });
result = BoogieGenerator.CondApplyUnbox(GetToken(expr), result, field.Type, expr.Type);
if (BoogieGenerator.IsLit(obj)) {
result = MaybeLit(result, BoogieGenerator.TrType(expr.Type));
}
return result;
}
}
},
fn => {
var typeMap = e.TypeArgumentSubstitutionsWithParents();
var args = GetTypeParams(fn).ConvertAll(tp => BoogieGenerator.TypeToTy(typeMap[tp]));
if (fn.IsFuelAware()) {
args.Add(this.layerInterCluster.GetFunctionFuel(fn));
}
if (fn.IsOpaque || fn.IsMadeImplicitlyOpaque(options)) {
args.Add(BoogieGenerator.GetRevealConstant(fn));
}
if (fn is TwoStateFunction) {
args.Add(Old.HeapExpr);
}
if (!fn.IsStatic) {
args.Add(/* translator.BoxIfUnboxed */(TrExpr(e.Obj)/*, e.Type */));
}
return FunctionCall(GetToken(e), BoogieGenerator.FunctionHandle(fn), predef.HandleType, args);
});
}
case SeqSelectExpr selectExpr: {
SeqSelectExpr e = selectExpr;
Boogie.Expr seq = TrExpr(e.Seq);
var seqType = e.Seq.Type.NormalizeExpand();
Type elmtType = null;
Type domainType = null;
Contract.Assert(seqType != null); // the expression has been successfully resolved
if (seqType.IsArrayType) {
domainType = Type.Int;
elmtType = UserDefinedType.ArrayElementType(seqType);
} else if (seqType is SeqType) {
domainType = Type.Int;
elmtType = ((SeqType)seqType).Arg;
} else if (seqType is MapType) {
domainType = ((MapType)seqType).Domain;
elmtType = ((MapType)seqType).Range;
} else if (seqType is MultiSetType) {
domainType = ((MultiSetType)seqType).Arg;
elmtType = Type.Int;
} else { Contract.Assert(false); }
Boogie.Type elType = BoogieGenerator.TrType(elmtType);
Boogie.Type dType = BoogieGenerator.TrType(domainType);
Boogie.Expr e0 = e.E0 == null ? null : TrExpr(e.E0);
if (e0 != null && e.E0.Type.IsBitVectorType) {
e0 = BoogieGenerator.ConvertExpression(GetToken(e.E0), e0, e.E0.Type, Type.Int);
}
Boogie.Expr e1 = e.E1 == null ? null : TrExpr(e.E1);
if (e1 != null && e.E1.Type.IsBitVectorType) {
e1 = BoogieGenerator.ConvertExpression(GetToken(e.E1), e1, e.E1.Type, Type.Int);
}
if (e.SelectOne) {
Contract.Assert(e1 == null);
Boogie.Expr x;
if (seqType.IsArrayType) {
Boogie.Expr fieldName = BoogieGenerator.FunctionCall(GetToken(selectExpr), BuiltinFunction.IndexField, null, e0);
x = ReadHeap(GetToken(selectExpr), HeapExpr, TrExpr(e.Seq), fieldName);
} else if (seqType is SeqType) {
x = BoogieGenerator.FunctionCall(GetToken(selectExpr), BuiltinFunction.SeqIndex, predef.BoxType, seq, e0);
} else if (seqType is MapType) {
bool finite = ((MapType)seqType).Finite;
var f = finite ? BuiltinFunction.MapElements : BuiltinFunction.IMapElements;
x = BoogieGenerator.FunctionCall(GetToken(selectExpr), f, predef.MapType(GetToken(e), finite, predef.BoxType, predef.BoxType), seq);
x = Boogie.Expr.Select(x, BoxIfNecessary(GetToken(e), e0, domainType));
} else if (seqType is MultiSetType) {
x = Boogie.Expr.SelectTok(GetToken(selectExpr), TrExpr(e.Seq), BoxIfNecessary(GetToken(selectExpr), e0, domainType));
} else { Contract.Assert(false); x = null; }
if (!ModeledAsBoxType(elmtType) && !(seqType is MultiSetType)) {
x = BoogieGenerator.FunctionCall(GetToken(selectExpr), BuiltinFunction.Unbox, elType, x);
}
return x;
} else {
if (seqType.IsArrayType) {
seq = BoogieGenerator.FunctionCall(GetToken(selectExpr), BuiltinFunction.SeqFromArray, elType, HeapExpr, seq);
}
var isLit = BoogieGenerator.IsLit(seq);
if (e1 != null) {
isLit = isLit && BoogieGenerator.IsLit(e1);
seq = BoogieGenerator.FunctionCall(GetToken(selectExpr), BuiltinFunction.SeqTake, elType, seq, e1);
}
if (e0 != null) {
isLit = isLit && BoogieGenerator.IsLit(e0);
seq = BoogieGenerator.FunctionCall(GetToken(selectExpr), BuiltinFunction.SeqDrop, elType, seq, e0);
}
// if e0 == null && e1 == null, then we have the identity operation seq[..] == seq;
if (isLit && (e0 != null || e1 != null)) {
// Lit-lift the expression
seq = MaybeLit(seq, BoogieGenerator.TrType(selectExpr.Type));
}
return seq;
}
}
case SeqUpdateExpr updateExpr: {
SeqUpdateExpr e = updateExpr;
Boogie.Expr seq = TrExpr(e.Seq);
var seqType = e.Seq.Type.NormalizeExpand();
if (seqType is SeqType) {
Boogie.Expr index = TrExpr(e.Index);
index = BoogieGenerator.ConvertExpression(GetToken(e.Index), index, e.Index.Type, Type.Int);
Boogie.Expr val = BoxIfNecessary(GetToken(updateExpr), TrExpr(e.Value), e.Value.Type);
return BoogieGenerator.FunctionCall(GetToken(updateExpr), BuiltinFunction.SeqUpdate, predef.BoxType, seq, index, val);
} else if (seqType is MapType) {
MapType mt = (MapType)seqType;
Boogie.Type maptype = predef.MapType(GetToken(updateExpr), mt.Finite, predef.BoxType, predef.BoxType);
Boogie.Expr index = BoxIfNecessary(GetToken(updateExpr), TrExpr(e.Index), mt.Domain);
Boogie.Expr val = BoxIfNecessary(GetToken(updateExpr), TrExpr(e.Value), mt.Range);
return FunctionCall(GetToken(updateExpr), mt.Finite ? "Map#Build" : "IMap#Build", maptype, seq, index, val);
} else if (seqType is MultiSetType) {
Type elmtType = cce.NonNull((MultiSetType)seqType).Arg;
Boogie.Expr index = BoxIfNecessary(GetToken(updateExpr), TrExpr(e.Index), elmtType);
Boogie.Expr val = TrExpr(e.Value);
return Boogie.Expr.StoreTok(GetToken(updateExpr), seq, index, val);
} else {
Contract.Assert(false);
throw new cce.UnreachableException();
}
}
case MultiSelectExpr selectExpr: {
MultiSelectExpr e = selectExpr;
Type elmtType = UserDefinedType.ArrayElementType(e.Array.Type); ;
Boogie.Type elType = BoogieGenerator.TrType(elmtType);
Boogie.Expr fieldName = GetArrayIndexFieldName(GetToken(selectExpr), e.Indices);
Boogie.Expr x = ReadHeap(GetToken(selectExpr), HeapExpr, TrExpr(e.Array), fieldName);
if (!ModeledAsBoxType(elmtType)) {
x = BoogieGenerator.FunctionCall(GetToken(selectExpr), BuiltinFunction.Unbox, elType, x);
}
return x;
}
case ApplyExpr applyExpr: {
ApplyExpr e = applyExpr;
int arity = e.Args.Count;
var tt = e.Function.Type.AsArrowType;
Contract.Assert(tt != null);
Contract.Assert(tt.Arity == arity);
{
// optimisation: if this could have just as well been a FunctionCallExpr, call it as such!
var con = e.Function as ConcreteSyntaxExpression;
var recv = con == null ? e.Function : con.Resolved;
var mem = recv as MemberSelectExpr;
var fn = mem == null ? null : mem.Member as Function;
if (fn != null) {
return TrExpr(new FunctionCallExpr(e.tok, fn.Name, mem.Obj, e.tok, e.CloseParen, e.Args) {
Function = fn,
Type = e.Type,
TypeApplication_AtEnclosingClass = mem.TypeApplication_AtEnclosingClass,
TypeApplication_JustFunction = mem.TypeApplication_JustMember
});
}
}
Func<Expression, Boogie.Expr> TrArg = arg => BoogieGenerator.BoxIfUnboxed(TrExpr(arg), arg.Type);
var applied = FunctionCall(GetToken(applyExpr), BoogieGenerator.Apply(arity), predef.BoxType,
Concat(Map(tt.TypeArgs, BoogieGenerator.TypeToTy),
Cons(HeapExpr, Cons(TrExpr(e.Function), e.Args.ConvertAll(arg => TrArg(arg))))));
return BoogieGenerator.UnboxIfBoxed(applied, tt.Result);
}
case FunctionCallExpr callExpr: {
FunctionCallExpr e = callExpr;
if (e.Function is SpecialFunction) {
return TrExprSpecialFunctionCall(e);
} else {
Boogie.Expr layerArgument;
Boogie.Expr revealArgument;
var etran = this;
if (e.Function.ContainsQuantifier && BoogieGenerator.stmtContext == StmtType.ASSUME && BoogieGenerator.adjustFuelForExists) {
// we need to increase fuel functions that contain quantifier expr in the assume context.
etran = etran.LayerOffset(1);
BoogieGenerator.adjustFuelForExists = false;
}
if (e.Function.IsFuelAware()) {
Statistics_CustomLayerFunctionCount++;
ModuleDefinition module = e.Function.EnclosingClass.EnclosingModuleDefinition;
if (etran.applyLimited_CurrentFunction != null &&
etran.layerIntraCluster != null &&
ModuleDefinition.InSameSCC(e.Function, applyLimited_CurrentFunction)) {
layerArgument = etran.layerIntraCluster.GetFunctionFuel(e.Function);
} else {
layerArgument = etran.layerInterCluster.GetFunctionFuel(e.Function);
}
} else {
layerArgument = null;
}
if (e.Function.IsOpaque || e.Function.IsMadeImplicitlyOpaque(options)) {
revealArgument = BoogieGenerator.GetRevealConstant(e.Function);
} else {
revealArgument = null;
}
var ty = BoogieGenerator.TrType(e.Type);
var id = new Boogie.IdentifierExpr(GetToken(e), e.Function.FullSanitizedName, ty);
var args = FunctionInvocationArguments(e, layerArgument, revealArgument, false, out var argsAreLit);
Expr result = new Boogie.NAryExpr(GetToken(e), new Boogie.FunctionCall(id), args);
result = BoogieGenerator.CondApplyUnbox(GetToken(e), result, e.Function.ResultType, e.Type);
bool callIsLit = argsAreLit
&& BoogieGenerator.FunctionBodyIsAvailable(e.Function, BoogieGenerator.currentModule, BoogieGenerator.currentScope, true)
&& !e.Function.Reads.Expressions.Any(); // Function could depend on external values
if (callIsLit) {
result = MaybeLit(result, ty);
}
return result;
}
}
case DatatypeValue value: {
DatatypeValue dtv = value;
Contract.Assert(dtv.Ctor != null); // since dtv has been successfully resolved
List<Boogie.Expr> args = new List<Boogie.Expr>();
bool argsAreLit = true;
for (int i = 0; i < dtv.Arguments.Count; i++) {
Expression arg = dtv.Arguments[i];
Type t = dtv.Ctor.Formals[i].Type;
var bArg = TrExpr(arg);
argsAreLit = argsAreLit && BoogieGenerator.IsLit(bArg);
args.Add(BoogieGenerator.CondApplyBox(GetToken(value), bArg, cce.NonNull(arg.Type), t));
}
Boogie.IdentifierExpr id = new Boogie.IdentifierExpr(GetToken(dtv), dtv.Ctor.FullName, predef.DatatypeType);
Boogie.Expr ret = new Boogie.NAryExpr(GetToken(dtv), new Boogie.FunctionCall(id), args);
if (argsAreLit) {
// If all arguments are Lit, so is the whole expression
ret = MaybeLit(ret, predef.DatatypeType);
}
return ret;
}
case SeqConstructionExpr constructionExpr: {
var e = constructionExpr;
var eType = e.Type.AsSeqType.Arg.NormalizeExpand();
return FunctionCall(GetToken(constructionExpr), "Seq#Create", predef.SeqType(GetToken(e), predef.BoxType), BoogieGenerator.TypeToTy(eType), HeapExpr, TrExpr(e.N), TrExpr(e.Initializer));
}
case MultiSetFormingExpr formingExpr: {
MultiSetFormingExpr e = formingExpr;
var eType = e.E.Type.NormalizeExpand();
if (eType is SetType) {
return BoogieGenerator.FunctionCall(GetToken(formingExpr), BuiltinFunction.MultiSetFromSet, BoogieGenerator.TrType(cce.NonNull((SetType)eType).Arg), TrExpr(e.E));
} else if (eType is SeqType) {
return BoogieGenerator.FunctionCall(GetToken(formingExpr), BuiltinFunction.MultiSetFromSeq, BoogieGenerator.TrType(cce.NonNull((SeqType)eType).Arg), TrExpr(e.E));
} else {
Contract.Assert(false); throw new cce.UnreachableException();
}
}
case OldExpr oldExpr: {
var e = oldExpr;
return OldAt(e.AtLabel).TrExpr(e.E);
}
case UnchangedExpr unchangedExpr: {
var e = unchangedExpr;
return BoogieGenerator.FrameCondition(GetToken(e), e.Frame, false, FrameExpressionUse.Unchanged, OldAt(e.AtLabel), this, this, true);
}
case UnaryOpExpr opExpr: {
var e = opExpr;
Boogie.Expr arg = TrExpr(e.E);
switch (e.ResolvedOp) {
case UnaryOpExpr.ResolvedOpcode.Lit:
return MaybeLit(arg);
case UnaryOpExpr.ResolvedOpcode.BVNot:
var bvWidth = opExpr.Type.AsBitVectorType.Width;
var bvType = BoogieGenerator.BplBvType(bvWidth);
Boogie.Expr r = FunctionCall(GetToken(opExpr), "not_bv" + bvWidth, bvType, arg);
if (BoogieGenerator.IsLit(arg)) {
r = MaybeLit(r, bvType);
}
return r;
case UnaryOpExpr.ResolvedOpcode.BoolNot:
return Boogie.Expr.Unary(GetToken(opExpr), UnaryOperator.Opcode.Not, arg);
case UnaryOpExpr.ResolvedOpcode.SeqLength:
Contract.Assert(e.E.Type.NormalizeExpand() is SeqType);
return BoogieGenerator.FunctionCall(GetToken(opExpr), BuiltinFunction.SeqLength, null, arg);
case UnaryOpExpr.ResolvedOpcode.SetCard:
Contract.Assert(e.E.Type.NormalizeExpand() is SetType { Finite: true });
return BoogieGenerator.FunctionCall(GetToken(opExpr), BuiltinFunction.SetCard, null, arg);
case UnaryOpExpr.ResolvedOpcode.MultiSetCard:
Contract.Assert(e.E.Type.NormalizeExpand() is MultiSetType);
return BoogieGenerator.FunctionCall(GetToken(opExpr), BuiltinFunction.MultiSetCard, null, arg);
case UnaryOpExpr.ResolvedOpcode.MapCard:
Contract.Assert(e.E.Type.NormalizeExpand() is MapType { Finite: true });
return BoogieGenerator.FunctionCall(GetToken(opExpr), BuiltinFunction.MapCard, null, arg);
case UnaryOpExpr.ResolvedOpcode.Fresh:
var freshLabel = ((FreshExpr)e).AtLabel;
var eeType = e.E.Type.NormalizeExpand();
if (eeType is SetType) {
// generate: (forall $o: ref :: { $o != null } X[Box($o)] ==> $o != null) &&
// (forall $o: ref :: { X[Box($o)] } X[Box($o)] ==> !old($Heap)[$o,alloc])
// OR, if X[Box($o)] is rewritten into smaller parts, use the less good trigger old($Heap)[$o,alloc]
Boogie.Variable oVar = new Boogie.BoundVariable(GetToken(opExpr), new Boogie.TypedIdent(GetToken(opExpr), "$o", predef.RefType));
Boogie.Expr o = new Boogie.IdentifierExpr(GetToken(opExpr), oVar);
Boogie.Expr oNotNull = Boogie.Expr.Neq(o, predef.Null);
Boogie.Expr oInSet = TrInSet(GetToken(opExpr), o, e.E, ((SetType)eeType).Arg, true, out var performedInSetRewrite);
Boogie.Expr oNotFresh = OldAt(freshLabel).IsAlloced(GetToken(opExpr), o);
Boogie.Expr oIsFresh = Boogie.Expr.Not(oNotFresh);
Boogie.Expr notNullBody = Boogie.Expr.Imp(oInSet, oNotNull);
Boogie.Expr freshBody = Boogie.Expr.Imp(oInSet, oIsFresh);
var notNullTrigger = BplTrigger(oNotNull);
var notNullPred = new Boogie.ForallExpr(GetToken(opExpr), new List<Variable> { oVar }, notNullTrigger, notNullBody);
var freshTrigger = BplTrigger(performedInSetRewrite ? oNotFresh : oInSet);
var freshPred = new Boogie.ForallExpr(GetToken(opExpr), new List<Variable> { oVar }, freshTrigger, freshBody);
return Boogie.Expr.And(notNullPred, freshPred);
} else if (eeType is SeqType) {
// generate: (forall $i: int :: 0 <= $i && $i < Seq#Length(X) ==> Unbox(Seq#Index(X,$i)) != null && !old($Heap)[Unbox(Seq#Index(X,$i)),alloc])
Boogie.Variable iVar = new Boogie.BoundVariable(GetToken(opExpr), new Boogie.TypedIdent(GetToken(opExpr), "$i", Boogie.Type.Int));
Boogie.Expr i = new Boogie.IdentifierExpr(GetToken(opExpr), iVar);
Boogie.Expr iBounds = BoogieGenerator.InSeqRange(GetToken(opExpr), i, Type.Int, TrExpr(e.E), true, null, false);
Boogie.Expr XsubI = BoogieGenerator.FunctionCall(GetToken(opExpr), BuiltinFunction.SeqIndex, predef.RefType, TrExpr(e.E), i);
XsubI = BoogieGenerator.FunctionCall(GetToken(opExpr), BuiltinFunction.Unbox, predef.RefType, XsubI);
Boogie.Expr oNotFresh = OldAt(freshLabel).IsAlloced(GetToken(opExpr), XsubI);
Boogie.Expr oIsFresh = Boogie.Expr.Not(oNotFresh);
Boogie.Expr xsubiNotNull = Boogie.Expr.Neq(XsubI, predef.Null);
Boogie.Expr body = Boogie.Expr.Imp(iBounds, Boogie.Expr.And(xsubiNotNull, oIsFresh));
//TRIGGERS: Does this make sense? dafny0\SmallTests
// BROKEN // NEW_TRIGGER
//TRIG (forall $i: int :: 0 <= $i && $i < Seq#Length(Q#0) && $Unbox(Seq#Index(Q#0, $i)): ref != null ==> !read(old($Heap), $Unbox(Seq#Index(Q#0, $i)): ref, alloc))
return new Boogie.ForallExpr(GetToken(opExpr), new List<Variable> { iVar }, body);
} else {
// generate: x != null && !old($Heap)[x]
Boogie.Expr oNull = Boogie.Expr.Neq(TrExpr(e.E), predef.Null);
Boogie.Expr oIsFresh = Boogie.Expr.Not(OldAt(freshLabel).IsAlloced(GetToken(opExpr), TrExpr(e.E)));
return Boogie.Expr.Binary(GetToken(opExpr), BinaryOperator.Opcode.And, oNull, oIsFresh);
}
case UnaryOpExpr.ResolvedOpcode.Allocated:
// Translate with $IsAllocBox, even if it requires boxing the argument. This has the effect of giving
// both the $IsAllocBox and $IsAlloc forms, because the axioms that connects these two is triggered
// by $IsAllocBox.
return BoogieGenerator.MkIsAllocBox(BoxIfNecessary(e.E.tok, TrExpr(e.E), e.E.Type), e.E.Type, HeapExpr);
default:
Contract.Assert(false); throw new cce.UnreachableException(); // unexpected unary expression
}
}
case ConversionExpr conversionExpr: {
var e = conversionExpr;
return BoogieGenerator.ConvertExpression(GetToken(e), TrExpr(e.E), e.E.Type, e.ToType);
}
case TypeTestExpr testExpr: {
var e = testExpr;
return BoogieGenerator.GetSubrangeCheck(TrExpr(e.E), e.E.Type, e.ToType, out var _) ?? Boogie.Expr.True;
}
case BinaryExpr binaryExpr: {
BinaryExpr e = binaryExpr;
bool isReal = e.E0.Type.IsNumericBased(Type.NumericPersuasion.Real);
int bvWidth = e.E0.Type.IsBitVectorType ? e.E0.Type.AsBitVectorType.Width : -1; // -1 indicates "not a bitvector type"
Boogie.Expr e0 = TrExpr(e.E0);
if (e.ResolvedOp == BinaryExpr.ResolvedOpcode.InSet) {
return TrInSet(GetToken(binaryExpr), e0, e.E1, cce.NonNull(e.E0.Type), false, out var pr); // let TrInSet translate e.E1
} else if (e.ResolvedOp == BinaryExpr.ResolvedOpcode.NotInSet) {
Boogie.Expr arg = TrInSet(GetToken(binaryExpr), e0, e.E1, cce.NonNull(e.E0.Type), false, out var pr); // let TrInSet translate e.E1
return Boogie.Expr.Unary(GetToken(binaryExpr), UnaryOperator.Opcode.Not, arg);
} else if (e.ResolvedOp == BinaryExpr.ResolvedOpcode.InMultiSet) {
return TrInMultiSet(GetToken(binaryExpr), e0, e.E1, cce.NonNull(e.E0.Type), false); // let TrInMultiSet translate e.E1
} else if (e.ResolvedOp == BinaryExpr.ResolvedOpcode.NotInMultiSet) {
Boogie.Expr arg = TrInMultiSet(GetToken(binaryExpr), e0, e.E1, cce.NonNull(e.E0.Type), false); // let TrInMultiSet translate e.E1
return Boogie.Expr.Unary(GetToken(binaryExpr), UnaryOperator.Opcode.Not, arg);
}
Boogie.Expr e1 = TrExpr(e.E1);
BinaryOperator.Opcode bOpcode;
Boogie.Type typ;
var oe0 = e0;
var oe1 = e1;
var lit0 = BoogieGenerator.GetLit(e0);
var lit1 = BoogieGenerator.GetLit(e1);
bool liftLit = BoogieGenerator.IsLit(e0) && BoogieGenerator.IsLit(e1);
// NOTE(namin): We usually avoid keeping literals, because their presence might mess up triggers that do not expect them.
// Still for equality-related operations, it's useful to keep them instead of lifting them, so that they can be propagated.
bool keepLits = false;
if (lit0 != null) {
e0 = lit0;
}
if (lit1 != null) {
e1 = lit1;
}
switch (e.ResolvedOp) {
case BinaryExpr.ResolvedOpcode.Iff:
typ = Boogie.Type.Bool;
bOpcode = BinaryOperator.Opcode.Iff; break;
case BinaryExpr.ResolvedOpcode.Imp:
typ = Boogie.Type.Bool;
bOpcode = BinaryOperator.Opcode.Imp; break;
case BinaryExpr.ResolvedOpcode.And:
typ = Boogie.Type.Bool;
bOpcode = BinaryOperator.Opcode.And; break;
case BinaryExpr.ResolvedOpcode.Or:
typ = Boogie.Type.Bool;
bOpcode = BinaryOperator.Opcode.Or; break;
case BinaryExpr.ResolvedOpcode.EqCommon:
keepLits = true;
if (ModeledAsBoxType(e.E0.Type)) {
e1 = BoxIfNecessary(expr.tok, e1, e.E1.Type);
oe1 = BoxIfNecessary(expr.tok, oe1, e.E1.Type);
} else if (ModeledAsBoxType(e.E1.Type)) {
e0 = BoxIfNecessary(expr.tok, e0, e.E0.Type);
oe0 = BoxIfNecessary(expr.tok, oe0, e.E0.Type);
}
var cot = e.E0.Type.AsCoDatatype;
if (cot != null) {
var e0args = e.E0.Type.NormalizeExpand().TypeArgs;
var e1args = e.E1.Type.NormalizeExpand().TypeArgs;
return BoogieGenerator.CoEqualCall(cot, e0args, e1args, null, this.layerInterCluster.LayerN((int)FuelSetting.FuelAmount.HIGH), e0, e1, GetToken(binaryExpr));
}
if (e.E0.Type.IsIndDatatype) {
return BoogieGenerator.TypeSpecificEqual(GetToken(binaryExpr), e.E0.Type, e0, e1);
}
typ = Boogie.Type.Bool;
bOpcode = BinaryOperator.Opcode.Eq; break;
case BinaryExpr.ResolvedOpcode.NeqCommon:
if (ModeledAsBoxType(e.E0.Type)) {
e1 = BoxIfNecessary(expr.tok, e1, e.E1.Type);
oe1 = BoxIfNecessary(expr.tok, oe1, e.E1.Type);
} else if (ModeledAsBoxType(e.E1.Type)) {
e0 = BoxIfNecessary(expr.tok, e0, e.E0.Type);
oe0 = BoxIfNecessary(expr.tok, oe0, e.E0.Type);
}
var cotx = e.E0.Type.AsCoDatatype;
if (cotx != null) {
var e0args = e.E0.Type.NormalizeExpand().TypeArgs;
var e1args = e.E1.Type.NormalizeExpand().TypeArgs;
var eq = BoogieGenerator.CoEqualCall(cotx, e0args, e1args, null, this.layerInterCluster.LayerN((int)FuelSetting.FuelAmount.HIGH), e0, e1, GetToken(binaryExpr));
return Boogie.Expr.Unary(GetToken(binaryExpr), UnaryOperator.Opcode.Not, eq);
}
if (e.E0.Type.IsIndDatatype) {
var eq = BoogieGenerator.TypeSpecificEqual(GetToken(binaryExpr), e.E0.Type, e0, e1);
return Boogie.Expr.Unary(GetToken(binaryExpr), UnaryOperator.Opcode.Not, eq);
}
typ = Boogie.Type.Bool;
bOpcode = BinaryOperator.Opcode.Neq; break;
case BinaryExpr.ResolvedOpcode.Lt:
if (0 <= bvWidth) {
return TrToFunctionCall(GetToken(binaryExpr), "lt_bv" + bvWidth, Boogie.Type.Bool, e0, e1, liftLit);
} else if (e.E0.Type.IsBigOrdinalType) {
return FunctionCall(GetToken(binaryExpr), "ORD#Less", Boogie.Type.Bool, e0, e1);
} else if (isReal || !BoogieGenerator.DisableNonLinearArithmetic) {
typ = Boogie.Type.Bool;
bOpcode = BinaryOperator.Opcode.Lt;
break;
} else {
return TrToFunctionCall(GetToken(binaryExpr), "INTERNAL_lt_boogie", Boogie.Type.Bool, e0, e1, liftLit);
}
case BinaryExpr.ResolvedOpcode.LessThanLimit:
return FunctionCall(GetToken(binaryExpr), "ORD#LessThanLimit", Boogie.Type.Bool, e0, e1);
case BinaryExpr.ResolvedOpcode.Le:
keepLits = true;
if (0 <= bvWidth) {
return TrToFunctionCall(GetToken(binaryExpr), "le_bv" + bvWidth, Boogie.Type.Bool, e0, e1, false);
} else if (e.E0.Type.IsBigOrdinalType) {
var less = FunctionCall(GetToken(binaryExpr), "ORD#Less", Boogie.Type.Bool, e0, e1);
var eq = Boogie.Expr.Eq(e0, e1);
return BplOr(eq, less);
} else if (isReal || !BoogieGenerator.DisableNonLinearArithmetic) {
typ = Boogie.Type.Bool;
bOpcode = BinaryOperator.Opcode.Le;
break;
} else {
return TrToFunctionCall(GetToken(binaryExpr), "INTERNAL_le_boogie", Boogie.Type.Bool, e0, e1, false);
}
case BinaryExpr.ResolvedOpcode.Ge:
keepLits = true;
if (0 <= bvWidth) {
return TrToFunctionCall(GetToken(binaryExpr), "ge_bv" + bvWidth, Boogie.Type.Bool, e0, e1, false);
} else if (e.E0.Type.IsBigOrdinalType) {
var less = FunctionCall(GetToken(binaryExpr), "ORD#Less", Boogie.Type.Bool, e1, e0);
var eq = Boogie.Expr.Eq(e1, e0);
return BplOr(eq, less);
} else if (isReal || !BoogieGenerator.DisableNonLinearArithmetic) {
typ = Boogie.Type.Bool;
bOpcode = BinaryOperator.Opcode.Ge;
break;
} else {
return TrToFunctionCall(GetToken(binaryExpr), "INTERNAL_ge_boogie", Boogie.Type.Bool, e0, e1, false);
}
case BinaryExpr.ResolvedOpcode.Gt:
if (0 <= bvWidth) {
return TrToFunctionCall(GetToken(binaryExpr), "gt_bv" + bvWidth, Boogie.Type.Bool, e0, e1, liftLit);
} else if (e.E0.Type.IsBigOrdinalType) {
return FunctionCall(GetToken(binaryExpr), "ORD#Less", Boogie.Type.Bool, e1, e0);
} else if (isReal || !BoogieGenerator.DisableNonLinearArithmetic) {
typ = Boogie.Type.Bool;
bOpcode = BinaryOperator.Opcode.Gt;
break;
} else {
return TrToFunctionCall(GetToken(binaryExpr), "INTERNAL_gt_boogie", Boogie.Type.Bool, e0, e1, liftLit);
}
case BinaryExpr.ResolvedOpcode.Add:
if (0 <= bvWidth) {
return TrToFunctionCall(GetToken(binaryExpr), "add_bv" + bvWidth, BoogieGenerator.BplBvType(bvWidth), e0, e1, liftLit);
} else if (e.E0.Type.IsBigOrdinalType) {
return TrToFunctionCall(GetToken(binaryExpr), "ORD#Plus", predef.BigOrdinalType, e0, e1, liftLit);
} else if (e.E0.Type.IsCharType) {
return TrToFunctionCall(GetToken(binaryExpr), "char#Plus", predef.CharType, e0, e1, liftLit);
} else if (!isReal && BoogieGenerator.DisableNonLinearArithmetic) {
return TrToFunctionCall(GetToken(binaryExpr), "INTERNAL_add_boogie", Boogie.Type.Int, e0, e1, liftLit);
} else if (!isReal && (options.ArithMode == 2 || 5 <= options.ArithMode)) {
return TrToFunctionCall(GetToken(binaryExpr), "Add", Boogie.Type.Int, oe0, oe1, liftLit);
} else {
typ = isReal ? Boogie.Type.Real : Boogie.Type.Int;
bOpcode = BinaryOperator.Opcode.Add;
break;
}