forked from diffblue/cbmc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstd_expr.h
3671 lines (3171 loc) · 85.9 KB
/
std_expr.h
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
/*******************************************************************\
Module: API to expression classes
Author: Daniel Kroening, [email protected]
\*******************************************************************/
#ifndef CPROVER_UTIL_STD_EXPR_H
#define CPROVER_UTIL_STD_EXPR_H
/// \file util/std_expr.h
/// API to expression classes
#include "expr_cast.h"
#include "invariant.h"
#include "std_types.h"
/// An expression without operands
class nullary_exprt : public expr_protectedt
{
public:
nullary_exprt(const irep_idt &_id, typet _type)
: expr_protectedt(_id, std::move(_type))
{
}
static void check(
const exprt &expr,
const validation_modet vm = validation_modet::INVARIANT)
{
DATA_CHECK(
vm,
expr.operands().size() == 0,
"nullary expression must not have operands");
}
static void validate(
const exprt &expr,
const namespacet &,
const validation_modet vm = validation_modet::INVARIANT)
{
check(expr, vm);
}
/// remove all operand methods
operandst &operands() = delete;
const operandst &operands() const = delete;
const exprt &op0() const = delete;
exprt &op0() = delete;
const exprt &op1() const = delete;
exprt &op1() = delete;
const exprt &op2() const = delete;
exprt &op2() = delete;
const exprt &op3() const = delete;
exprt &op3() = delete;
void copy_to_operands(const exprt &expr) = delete;
void copy_to_operands(const exprt &, const exprt &) = delete;
void copy_to_operands(const exprt &, const exprt &, const exprt &) = delete;
};
/// An expression with three operands
class ternary_exprt : public expr_protectedt
{
public:
// constructor
ternary_exprt(
const irep_idt &_id,
exprt _op0,
exprt _op1,
exprt _op2,
typet _type)
: expr_protectedt(
_id,
std::move(_type),
{std::move(_op0), std::move(_op1), std::move(_op2)})
{
}
// make op0 to op2 public
using exprt::op0;
using exprt::op1;
using exprt::op2;
const exprt &op3() const = delete;
exprt &op3() = delete;
static void check(
const exprt &expr,
const validation_modet vm = validation_modet::INVARIANT)
{
DATA_CHECK(
vm,
expr.operands().size() == 3,
"ternary expression must have three operands");
}
static void validate(
const exprt &expr,
const namespacet &,
const validation_modet vm = validation_modet::INVARIANT)
{
check(expr, vm);
}
};
/// \brief Cast an exprt to a \ref ternary_exprt
///
/// \a expr must be known to be \ref ternary_exprt.
///
/// \param expr: Source expression
/// \return Object of type \ref ternary_exprt
inline const ternary_exprt &to_ternary_expr(const exprt &expr)
{
ternary_exprt::check(expr);
return static_cast<const ternary_exprt &>(expr);
}
/// \copydoc to_ternary_expr(const exprt &)
inline ternary_exprt &to_ternary_expr(exprt &expr)
{
ternary_exprt::check(expr);
return static_cast<ternary_exprt &>(expr);
}
/// Expression to hold a symbol (variable)
class symbol_exprt : public nullary_exprt
{
public:
/// \param type: Type of symbol
explicit symbol_exprt(typet type) : nullary_exprt(ID_symbol, std::move(type))
{
}
/// \param identifier: Name of symbol
/// \param type: Type of symbol
symbol_exprt(const irep_idt &identifier, typet type)
: nullary_exprt(ID_symbol, std::move(type))
{
set_identifier(identifier);
}
/// Generate a symbol_exprt without a proper type. Use if, and only if, the
/// type either cannot be determined just yet (such as during type checking)
/// or when the type truly is immaterial. The latter case may better be dealt
/// with by using just an irep_idt, and not a symbol_exprt.
static symbol_exprt typeless(const irep_idt &id)
{
return symbol_exprt(id, typet());
}
void set_identifier(const irep_idt &identifier)
{
set(ID_identifier, identifier);
}
const irep_idt &get_identifier() const
{
return get(ID_identifier);
}
/// Add the source location from \p location, if it is non-nil.
symbol_exprt &with_source_location(source_locationt location) &
{
if(location.is_not_nil())
add_source_location() = std::move(location);
return *this;
}
/// Add the source location from \p location, if it is non-nil.
symbol_exprt &&with_source_location(source_locationt location) &&
{
if(location.is_not_nil())
add_source_location() = std::move(location);
return std::move(*this);
}
/// Add the source location from \p other, if it has any.
symbol_exprt &with_source_location(const exprt &other) &
{
if(other.source_location().is_not_nil())
add_source_location() = other.source_location();
return *this;
}
/// Add the source location from \p other, if it has any.
symbol_exprt &&with_source_location(const exprt &other) &&
{
if(other.source_location().is_not_nil())
add_source_location() = other.source_location();
return std::move(*this);
}
};
// NOLINTNEXTLINE(readability/namespace)
namespace std
{
template <>
// NOLINTNEXTLINE(readability/identifiers)
struct hash<::symbol_exprt>
{
size_t operator()(const ::symbol_exprt &sym)
{
return irep_id_hash()(sym.get_identifier());
}
};
} // namespace std
/// Expression to hold a symbol (variable) with extra accessors to
/// ID_c_static_lifetime and ID_C_thread_local
class decorated_symbol_exprt:public symbol_exprt
{
public:
/// \param identifier: Name of symbol
/// \param type: Type of symbol
decorated_symbol_exprt(const irep_idt &identifier, typet type)
: symbol_exprt(identifier, std::move(type))
{
}
bool is_static_lifetime() const
{
return get_bool(ID_C_static_lifetime);
}
void set_static_lifetime()
{
return set(ID_C_static_lifetime, true);
}
void clear_static_lifetime()
{
remove(ID_C_static_lifetime);
}
bool is_thread_local() const
{
return get_bool(ID_C_thread_local);
}
void set_thread_local()
{
return set(ID_C_thread_local, true);
}
void clear_thread_local()
{
remove(ID_C_thread_local);
}
};
template <>
inline bool can_cast_expr<symbol_exprt>(const exprt &base)
{
return base.id() == ID_symbol;
}
inline void validate_expr(const symbol_exprt &value)
{
validate_operands(value, 0, "Symbols must not have operands");
}
/// \brief Cast an exprt to a \ref symbol_exprt
///
/// \a expr must be known to be \ref symbol_exprt.
///
/// \param expr: Source expression
/// \return Object of type \ref symbol_exprt
inline const symbol_exprt &to_symbol_expr(const exprt &expr)
{
PRECONDITION(expr.id()==ID_symbol);
const symbol_exprt &ret = static_cast<const symbol_exprt &>(expr);
validate_expr(ret);
return ret;
}
/// \copydoc to_symbol_expr(const exprt &)
inline symbol_exprt &to_symbol_expr(exprt &expr)
{
PRECONDITION(expr.id()==ID_symbol);
symbol_exprt &ret = static_cast<symbol_exprt &>(expr);
validate_expr(ret);
return ret;
}
/// \brief Expression to hold a nondeterministic choice
class nondet_symbol_exprt : public nullary_exprt
{
public:
/// \param identifier: Name of symbol
/// \param type: Type of symbol
nondet_symbol_exprt(const irep_idt &identifier, typet type)
: nullary_exprt(ID_nondet_symbol, std::move(type))
{
set_identifier(identifier);
}
/// \param identifier: name of symbol
/// \param type: type of symbol
/// \param location: location from which the symbol originate
nondet_symbol_exprt(
irep_idt identifier,
typet type,
source_locationt location)
: nullary_exprt(ID_nondet_symbol, std::move(type))
{
set_identifier(std::move(identifier));
add_source_location() = std::move(location);
}
void set_identifier(const irep_idt &identifier)
{
set(ID_identifier, identifier);
}
const irep_idt &get_identifier() const
{
return get(ID_identifier);
}
};
template <>
inline bool can_cast_expr<nondet_symbol_exprt>(const exprt &base)
{
return base.id() == ID_nondet_symbol;
}
inline void validate_expr(const nondet_symbol_exprt &value)
{
validate_operands(value, 0, "Symbols must not have operands");
}
/// \brief Cast an exprt to a \ref nondet_symbol_exprt
///
/// \a expr must be known to be \ref nondet_symbol_exprt.
///
/// \param expr: Source expression
/// \return Object of type \ref nondet_symbol_exprt
inline const nondet_symbol_exprt &to_nondet_symbol_expr(const exprt &expr)
{
PRECONDITION(expr.id()==ID_nondet_symbol);
nondet_symbol_exprt::check(expr);
return static_cast<const nondet_symbol_exprt &>(expr);
}
/// \copydoc to_nondet_symbol_expr(const exprt &)
inline nondet_symbol_exprt &to_nondet_symbol_expr(exprt &expr)
{
PRECONDITION(expr.id()==ID_symbol);
nondet_symbol_exprt::check(expr);
return static_cast<nondet_symbol_exprt &>(expr);
}
/// \brief Generic base class for unary expressions
class unary_exprt : public expr_protectedt
{
public:
unary_exprt(const irep_idt &_id, const exprt &_op)
: expr_protectedt(_id, _op.type(), {_op})
{
}
unary_exprt(const irep_idt &_id, exprt _op, typet _type)
: expr_protectedt(_id, std::move(_type), {std::move(_op)})
{
}
static void check(
const exprt &expr,
const validation_modet vm = validation_modet::INVARIANT)
{
DATA_CHECK(
vm,
expr.operands().size() == 1,
"unary expression must have one operand");
}
static void validate(
const exprt &expr,
const namespacet &,
const validation_modet vm = validation_modet::INVARIANT)
{
check(expr, vm);
}
const exprt &op() const
{
return op0();
}
exprt &op()
{
return op0();
}
const exprt &op1() const = delete;
exprt &op1() = delete;
const exprt &op2() const = delete;
exprt &op2() = delete;
const exprt &op3() const = delete;
exprt &op3() = delete;
};
template <>
inline bool can_cast_expr<unary_exprt>(const exprt &base)
{
return base.operands().size() == 1;
}
inline void validate_expr(const unary_exprt &value)
{
unary_exprt::check(value);
}
/// \brief Cast an exprt to a \ref unary_exprt
///
/// \a expr must be known to be \ref unary_exprt.
///
/// \param expr: Source expression
/// \return Object of type \ref unary_exprt
inline const unary_exprt &to_unary_expr(const exprt &expr)
{
unary_exprt::check(expr);
return static_cast<const unary_exprt &>(expr);
}
/// \copydoc to_unary_expr(const exprt &)
inline unary_exprt &to_unary_expr(exprt &expr)
{
unary_exprt::check(expr);
return static_cast<unary_exprt &>(expr);
}
/// \brief Absolute value
class abs_exprt:public unary_exprt
{
public:
explicit abs_exprt(exprt _op) : unary_exprt(ID_abs, std::move(_op))
{
}
};
template <>
inline bool can_cast_expr<abs_exprt>(const exprt &base)
{
return base.id() == ID_abs;
}
inline void validate_expr(const abs_exprt &value)
{
validate_operands(value, 1, "Absolute value must have one operand");
}
/// \brief Cast an exprt to a \ref abs_exprt
///
/// \a expr must be known to be \ref abs_exprt.
///
/// \param expr: Source expression
/// \return Object of type \ref abs_exprt
inline const abs_exprt &to_abs_expr(const exprt &expr)
{
PRECONDITION(expr.id()==ID_abs);
abs_exprt::check(expr);
return static_cast<const abs_exprt &>(expr);
}
/// \copydoc to_abs_expr(const exprt &)
inline abs_exprt &to_abs_expr(exprt &expr)
{
PRECONDITION(expr.id()==ID_abs);
abs_exprt::check(expr);
return static_cast<abs_exprt &>(expr);
}
/// \brief The unary minus expression
class unary_minus_exprt:public unary_exprt
{
public:
unary_minus_exprt(exprt _op, typet _type)
: unary_exprt(ID_unary_minus, std::move(_op), std::move(_type))
{
}
explicit unary_minus_exprt(exprt _op)
: unary_exprt(ID_unary_minus, std::move(_op))
{
}
};
template <>
inline bool can_cast_expr<unary_minus_exprt>(const exprt &base)
{
return base.id() == ID_unary_minus;
}
inline void validate_expr(const unary_minus_exprt &value)
{
validate_operands(value, 1, "Unary minus must have one operand");
}
/// \brief Cast an exprt to a \ref unary_minus_exprt
///
/// \a expr must be known to be \ref unary_minus_exprt.
///
/// \param expr: Source expression
/// \return Object of type \ref unary_minus_exprt
inline const unary_minus_exprt &to_unary_minus_expr(const exprt &expr)
{
PRECONDITION(expr.id()==ID_unary_minus);
unary_minus_exprt::check(expr);
return static_cast<const unary_minus_exprt &>(expr);
}
/// \copydoc to_unary_minus_expr(const exprt &)
inline unary_minus_exprt &to_unary_minus_expr(exprt &expr)
{
PRECONDITION(expr.id()==ID_unary_minus);
unary_minus_exprt::check(expr);
return static_cast<unary_minus_exprt &>(expr);
}
/// \brief The unary plus expression
class unary_plus_exprt : public unary_exprt
{
public:
explicit unary_plus_exprt(exprt op)
: unary_exprt(ID_unary_plus, std::move(op))
{
}
};
template <>
inline bool can_cast_expr<unary_plus_exprt>(const exprt &base)
{
return base.id() == ID_unary_plus;
}
inline void validate_expr(const unary_plus_exprt &value)
{
validate_operands(value, 1, "unary plus must have one operand");
}
/// \brief Cast an exprt to a \ref unary_plus_exprt
///
/// \a expr must be known to be \ref unary_plus_exprt.
///
/// \param expr: Source expression
/// \return Object of type \ref unary_plus_exprt
inline const unary_plus_exprt &to_unary_plus_expr(const exprt &expr)
{
PRECONDITION(expr.id() == ID_unary_plus);
unary_plus_exprt::check(expr);
return static_cast<const unary_plus_exprt &>(expr);
}
/// \copydoc to_unary_minus_expr(const exprt &)
inline unary_plus_exprt &to_unary_plus_expr(exprt &expr)
{
PRECONDITION(expr.id() == ID_unary_plus);
unary_plus_exprt::check(expr);
return static_cast<unary_plus_exprt &>(expr);
}
/// \brief A base class for expressions that are predicates,
/// i.e., Boolean-typed.
class predicate_exprt : public expr_protectedt
{
public:
explicit predicate_exprt(const irep_idt &_id)
: expr_protectedt(_id, bool_typet())
{
}
};
/// \brief A base class for expressions that are predicates,
/// i.e., Boolean-typed, and that take exactly one argument.
class unary_predicate_exprt:public unary_exprt
{
public:
unary_predicate_exprt(const irep_idt &_id, exprt _op)
: unary_exprt(_id, std::move(_op), bool_typet())
{
}
};
/// \brief Sign of an expression
/// Predicate is true if \a _op is negative, false otherwise.
class sign_exprt:public unary_predicate_exprt
{
public:
explicit sign_exprt(exprt _op)
: unary_predicate_exprt(ID_sign, std::move(_op))
{
}
};
template <>
inline bool can_cast_expr<sign_exprt>(const exprt &base)
{
return base.id() == ID_sign;
}
inline void validate_expr(const sign_exprt &expr)
{
validate_operands(expr, 1, "sign expression must have one operand");
}
/// \brief Cast an exprt to a \ref sign_exprt
///
/// \a expr must be known to be a \ref sign_exprt.
///
/// \param expr: Source expression
/// \return Object of type \ref sign_exprt
inline const sign_exprt &to_sign_expr(const exprt &expr)
{
PRECONDITION(expr.id() == ID_sign);
sign_exprt::check(expr);
return static_cast<const sign_exprt &>(expr);
}
/// \copydoc to_sign_expr(const exprt &)
inline sign_exprt &to_sign_expr(exprt &expr)
{
PRECONDITION(expr.id() == ID_sign);
sign_exprt::check(expr);
return static_cast<sign_exprt &>(expr);
}
/// \brief A base class for binary expressions
class binary_exprt : public expr_protectedt
{
public:
binary_exprt(const exprt &_lhs, const irep_idt &_id, exprt _rhs)
: expr_protectedt(_id, _lhs.type(), {_lhs, std::move(_rhs)})
{
}
binary_exprt(exprt _lhs, const irep_idt &_id, exprt _rhs, typet _type)
: expr_protectedt(_id, std::move(_type), {std::move(_lhs), std::move(_rhs)})
{
}
static void check(
const exprt &expr,
const validation_modet vm = validation_modet::INVARIANT)
{
DATA_CHECK(
vm,
expr.operands().size() == 2,
"binary expression must have two operands");
}
static void validate(
const exprt &expr,
const namespacet &,
const validation_modet vm = validation_modet::INVARIANT)
{
check(expr, vm);
}
exprt &lhs()
{
return exprt::op0();
}
const exprt &lhs() const
{
return exprt::op0();
}
exprt &rhs()
{
return exprt::op1();
}
const exprt &rhs() const
{
return exprt::op1();
}
// make op0 and op1 public
using exprt::op0;
using exprt::op1;
const exprt &op2() const = delete;
exprt &op2() = delete;
const exprt &op3() const = delete;
exprt &op3() = delete;
};
template <>
inline bool can_cast_expr<binary_exprt>(const exprt &base)
{
return base.operands().size() == 2;
}
inline void validate_expr(const binary_exprt &value)
{
binary_exprt::check(value);
}
/// \brief Cast an exprt to a \ref binary_exprt
///
/// \a expr must be known to be \ref binary_exprt.
///
/// \param expr: Source expression
/// \return Object of type \ref binary_exprt
inline const binary_exprt &to_binary_expr(const exprt &expr)
{
binary_exprt::check(expr);
return static_cast<const binary_exprt &>(expr);
}
/// \copydoc to_binary_expr(const exprt &)
inline binary_exprt &to_binary_expr(exprt &expr)
{
binary_exprt::check(expr);
return static_cast<binary_exprt &>(expr);
}
/// \brief A base class for expressions that are predicates,
/// i.e., Boolean-typed, and that take exactly two arguments.
class binary_predicate_exprt:public binary_exprt
{
public:
binary_predicate_exprt(exprt _op0, const irep_idt &_id, exprt _op1)
: binary_exprt(std::move(_op0), _id, std::move(_op1), bool_typet())
{
}
static void check(
const exprt &expr,
const validation_modet vm = validation_modet::INVARIANT)
{
binary_exprt::check(expr, vm);
}
static void validate(
const exprt &expr,
const namespacet &ns,
const validation_modet vm = validation_modet::INVARIANT)
{
binary_exprt::validate(expr, ns, vm);
DATA_CHECK(
vm,
expr.is_boolean(),
"result of binary predicate expression should be of type bool");
}
};
/// \brief A base class for relations, i.e., binary predicates whose
/// two operands have the same type
class binary_relation_exprt:public binary_predicate_exprt
{
public:
binary_relation_exprt(exprt _lhs, const irep_idt &_id, exprt _rhs)
: binary_predicate_exprt(std::move(_lhs), _id, std::move(_rhs))
{
}
static void check(
const exprt &expr,
const validation_modet vm = validation_modet::INVARIANT)
{
binary_predicate_exprt::check(expr, vm);
}
static void validate(
const exprt &expr,
const namespacet &ns,
const validation_modet vm = validation_modet::INVARIANT)
{
binary_predicate_exprt::validate(expr, ns, vm);
// we now can safely assume that 'expr' is a binary predicate
const auto &expr_binary = static_cast<const binary_predicate_exprt &>(expr);
// check that the types of the operands are the same
DATA_CHECK(
vm,
expr_binary.op0().type() == expr_binary.op1().type(),
"lhs and rhs of binary relation expression should have same type");
}
};
template <>
inline bool can_cast_expr<binary_relation_exprt>(const exprt &base)
{
return can_cast_expr<binary_exprt>(base);
}
inline void validate_expr(const binary_relation_exprt &value)
{
binary_relation_exprt::check(value);
}
/// \brief Binary greater than operator expression.
class greater_than_exprt : public binary_relation_exprt
{
public:
greater_than_exprt(exprt _lhs, exprt _rhs)
: binary_relation_exprt{std::move(_lhs), ID_gt, std::move(_rhs)}
{
}
};
template <>
inline bool can_cast_expr<greater_than_exprt>(const exprt &base)
{
return base.id() == ID_gt;
}
inline void validate_expr(const greater_than_exprt &value)
{
binary_relation_exprt::check(value);
}
/// \brief Binary greater than or equal operator expression.
class greater_than_or_equal_exprt : public binary_relation_exprt
{
public:
greater_than_or_equal_exprt(exprt _lhs, exprt _rhs)
: binary_relation_exprt{std::move(_lhs), ID_ge, std::move(_rhs)}
{
}
};
template <>
inline bool can_cast_expr<greater_than_or_equal_exprt>(const exprt &base)
{
return base.id() == ID_ge;
}
inline void validate_expr(const greater_than_or_equal_exprt &value)
{
binary_relation_exprt::check(value);
}
/// \brief Binary less than operator expression.
class less_than_exprt : public binary_relation_exprt
{
public:
less_than_exprt(exprt _lhs, exprt _rhs)
: binary_relation_exprt{std::move(_lhs), ID_lt, std::move(_rhs)}
{
}
};
template <>
inline bool can_cast_expr<less_than_exprt>(const exprt &base)
{
return base.id() == ID_lt;
}
inline void validate_expr(const less_than_exprt &value)
{
binary_relation_exprt::check(value);
}
/// \brief Binary less than or equal operator expression.
class less_than_or_equal_exprt : public binary_relation_exprt
{
public:
less_than_or_equal_exprt(exprt _lhs, exprt _rhs)
: binary_relation_exprt{std::move(_lhs), ID_le, std::move(_rhs)}
{
}
};
template <>
inline bool can_cast_expr<less_than_or_equal_exprt>(const exprt &base)
{
return base.id() == ID_le;
}
inline void validate_expr(const less_than_or_equal_exprt &value)
{
binary_relation_exprt::check(value);
}
/// \brief Cast an exprt to a \ref binary_relation_exprt
///
/// \a expr must be known to be \ref binary_relation_exprt.
///
/// \param expr: Source expression
/// \return Object of type \ref binary_relation_exprt
inline const binary_relation_exprt &to_binary_relation_expr(const exprt &expr)
{
binary_relation_exprt::check(expr);
return static_cast<const binary_relation_exprt &>(expr);
}
/// \copydoc to_binary_relation_expr(const exprt &)
inline binary_relation_exprt &to_binary_relation_expr(exprt &expr)
{
binary_relation_exprt::check(expr);
return static_cast<binary_relation_exprt &>(expr);
}
/// \brief A base class for multi-ary expressions
/// Associativity is not specified.
class multi_ary_exprt : public expr_protectedt
{
public:
multi_ary_exprt(const irep_idt &_id, operandst _operands, typet _type)
: expr_protectedt(_id, std::move(_type))
{
operands() = std::move(_operands);
}
multi_ary_exprt(const exprt &_lhs, const irep_idt &_id, exprt _rhs)
: expr_protectedt(_id, _lhs.type(), {_lhs, std::move(_rhs)})
{
}
multi_ary_exprt(exprt _lhs, const irep_idt &_id, exprt _rhs, typet _type)
: expr_protectedt(_id, std::move(_type), {std::move(_lhs), std::move(_rhs)})
{
}
// In contrast to exprt::opX, the methods
// below check the size.
exprt &op0()
{
PRECONDITION(operands().size() >= 1);
return operands().front();
}
exprt &op1()
{
PRECONDITION(operands().size() >= 2);
return operands()[1];
}
exprt &op2()
{
PRECONDITION(operands().size() >= 3);
return operands()[2];
}
exprt &op3()
{
PRECONDITION(operands().size() >= 4);
return operands()[3];
}
const exprt &op0() const
{
PRECONDITION(operands().size() >= 1);
return operands().front();
}
const exprt &op1() const
{
PRECONDITION(operands().size() >= 2);
return operands()[1];
}
const exprt &op2() const
{
PRECONDITION(operands().size() >= 3);
return operands()[2];
}
const exprt &op3() const
{
PRECONDITION(operands().size() >= 4);
return operands()[3];
}
};
/// \brief Cast an exprt to a \ref multi_ary_exprt
///
/// \a expr must be known to be \ref multi_ary_exprt.
///
/// \param expr: Source expression
/// \return Object of type \ref multi_ary_exprt
inline const multi_ary_exprt &to_multi_ary_expr(const exprt &expr)
{
return static_cast<const multi_ary_exprt &>(expr);
}
/// \copydoc to_multi_ary_expr(const exprt &)
inline multi_ary_exprt &to_multi_ary_expr(exprt &expr)
{
return static_cast<multi_ary_exprt &>(expr);
}
/// \brief The plus expression
/// Associativity is not specified.