-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathstd_code.h
2579 lines (2186 loc) · 59.4 KB
/
std_code.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: Data structures representing statements in a program
Author: Daniel Kroening, [email protected]
\*******************************************************************/
#ifndef CPROVER_UTIL_STD_CODE_H
#define CPROVER_UTIL_STD_CODE_H
#include <list>
#include "expr.h"
#include "expr_cast.h"
#include "invariant.h"
#include "std_expr.h"
#include "std_types.h"
#include "validate.h"
#include "validate_code.h"
/// Data structure for representing an arbitrary statement in a program. Every
/// specific type of statement (e.g. block of statements, assignment,
/// if-then-else statement...) is represented by a subtype of `codet`.
/// `codet`s are represented to be subtypes of \ref exprt since statements can
/// occur in an expression context in C: for example, the assignment `x = y;`
/// is an expression with return value `y`. For other types of statements in an
/// expression context, see e.g.
/// https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html.
/// To distinguish a `codet` from other [exprts](\ref exprt), we set its
/// [id()](\ref irept::id) to `ID_code`. To distinguish different types of
/// `codet`, we use a named sub `ID_statement`.
class codet:public exprt
{
public:
/// \param statement: Specifies the type of the `codet` to be constructed,
/// e.g. `ID_block` for a \ref code_blockt or `ID_assign` for a
/// \ref code_assignt.
explicit codet(const irep_idt &statement) : exprt(ID_code, empty_typet())
{
set_statement(statement);
}
codet(const irep_idt &statement, source_locationt loc)
: exprt(ID_code, empty_typet(), std::move(loc))
{
set_statement(statement);
}
/// \param statement: Specifies the type of the `codet` to be constructed,
/// e.g. `ID_block` for a \ref code_blockt or `ID_assign` for a
/// \ref code_assignt.
/// \param _op: any operands to be added
explicit codet(const irep_idt &statement, operandst _op) : codet(statement)
{
operands() = std::move(_op);
}
codet(const irep_idt &statement, operandst op, source_locationt loc)
: codet(statement, std::move(loc))
{
operands() = std::move(op);
}
void set_statement(const irep_idt &statement)
{
set(ID_statement, statement);
}
const irep_idt &get_statement() const
{
return get(ID_statement);
}
codet &first_statement();
const codet &first_statement() const;
codet &last_statement();
const codet &last_statement() const;
DEPRECATED(SINCE(2019, 2, 6, "use code_blockt(...) instead"))
class code_blockt &make_block();
/// Check that the code statement is well-formed (shallow checks only, i.e.,
/// enclosed statements, subexpressions, etc. are not checked)
///
/// Subclasses may override this function to provide specific well-formedness
/// checks for the corresponding types.
///
/// The validation mode indicates whether well-formedness check failures are
/// reported via DATA_INVARIANT violations or exceptions.
static void check(const codet &, const validation_modet)
{
}
/// Check that the code statement is well-formed, assuming that all its
/// enclosed statements, subexpressions, etc. have all ready been checked for
/// well-formedness.
///
/// Subclasses may override this function to provide specific well-formedness
/// checks for the corresponding types.
///
/// The validation mode indicates whether well-formedness check failures are
/// reported via DATA_INVARIANT violations or exceptions.
static void validate(
const codet &code,
const namespacet &,
const validation_modet vm = validation_modet::INVARIANT)
{
check_code(code, vm);
}
/// Check that the code statement is well-formed (full check, including checks
/// of all subexpressions)
///
/// Subclasses may override this function to provide specific well-formedness
/// checks for the corresponding types.
///
/// The validation mode indicates whether well-formedness check failures are
/// reported via DATA_INVARIANT violations or exceptions.
static void validate_full(
const codet &code,
const namespacet &,
const validation_modet vm = validation_modet::INVARIANT)
{
check_code(code, vm);
}
};
namespace detail // NOLINT
{
template<typename Tag>
inline bool can_cast_code_impl(const exprt &expr, const Tag &tag)
{
if(const auto ptr = expr_try_dynamic_cast<codet>(expr))
{
return ptr->get_statement() == tag;
}
return false;
}
} // namespace detail
template<> inline bool can_cast_expr<codet>(const exprt &base)
{
return base.id()==ID_code;
}
// to_code has no validation other than checking the id(), so no validate_expr
// is provided for codet
inline const codet &to_code(const exprt &expr)
{
PRECONDITION(expr.id() == ID_code);
return static_cast<const codet &>(expr);
}
inline codet &to_code(exprt &expr)
{
PRECONDITION(expr.id() == ID_code);
return static_cast<codet &>(expr);
}
/// A \ref codet representing sequential composition of program statements.
/// Each operand represents a statement in the block.
class code_blockt:public codet
{
public:
code_blockt():codet(ID_block)
{
}
typedef std::vector<codet> code_operandst;
code_operandst &statements()
{
return (code_operandst &)get_sub();
}
const code_operandst &statements() const
{
return (const code_operandst &)get_sub();
}
static code_blockt from_list(const std::list<codet> &_list)
{
code_blockt result;
auto &s=result.statements();
s.reserve(_list.size());
for(const auto &c : _list)
s.push_back(c);
return result;
}
explicit code_blockt(const std::vector<codet> &_statements)
: codet(ID_block, (const std::vector<exprt> &)_statements)
{
}
explicit code_blockt(std::vector<codet> &&_statements)
: codet(ID_block, std::move((std::vector<exprt> &&) _statements))
{
}
void add(const codet &code)
{
add_to_operands(code);
}
void add(codet &&code)
{
add_to_operands(std::move(code));
}
void add(codet code, source_locationt loc)
{
code.add_source_location().swap(loc);
add(std::move(code));
}
void append(const code_blockt &extra_block);
// This is the closing '}' or 'END' at the end of a block
source_locationt end_location() const
{
return static_cast<const source_locationt &>(find(ID_C_end_location));
}
codet &find_last_statement();
static void validate_full(
const codet &code,
const namespacet &ns,
const validation_modet vm = validation_modet::INVARIANT)
{
for(const auto &statement : code.operands())
{
DATA_CHECK(
vm, code.id() == ID_code, "code block must be made up of codet");
validate_full_code(to_code(statement), ns, vm);
}
}
};
template<> inline bool can_cast_expr<code_blockt>(const exprt &base)
{
return detail::can_cast_code_impl(base, ID_block);
}
// to_code_block has no validation other than checking the statement(), so no
// validate_expr is provided for code_blockt
inline const code_blockt &to_code_block(const codet &code)
{
PRECONDITION(code.get_statement() == ID_block);
return static_cast<const code_blockt &>(code);
}
inline code_blockt &to_code_block(codet &code)
{
PRECONDITION(code.get_statement() == ID_block);
return static_cast<code_blockt &>(code);
}
/// A \ref codet representing a `skip` statement.
class code_skipt:public codet
{
public:
code_skipt():codet(ID_skip)
{
}
protected:
using codet::op0;
using codet::op1;
using codet::op2;
using codet::op3;
};
template<> inline bool can_cast_expr<code_skipt>(const exprt &base)
{
return detail::can_cast_code_impl(base, ID_skip);
}
// there is no to_code_skip, so no validate_expr is provided for code_skipt
/// A \ref codet representing an assignment in the program.
/// For example, if an expression `e1` is represented as an \ref exprt `expr1`
/// and an expression `e2` is represented as an \ref exprt `expr2`, the
/// assignment `e1 = e2;` can be represented as `code_assignt(expr1, expr2)`.
class code_assignt:public codet
{
public:
code_assignt():codet(ID_assign)
{
operands().resize(2);
}
code_assignt(exprt lhs, exprt rhs)
: codet(ID_assign, {std::move(lhs), std::move(rhs)})
{
}
code_assignt(exprt lhs, exprt rhs, source_locationt loc)
: codet(ID_assign, {std::move(lhs), std::move(rhs)}, std::move(loc))
{
}
exprt &lhs()
{
return op0();
}
exprt &rhs()
{
return op1();
}
const exprt &lhs() const
{
return op0();
}
const exprt &rhs() const
{
return op1();
}
static void check(
const codet &code,
const validation_modet vm = validation_modet::INVARIANT)
{
DATA_CHECK(
vm, code.operands().size() == 2, "assignment must have two operands");
}
static void validate(
const codet &code,
const namespacet &,
const validation_modet vm = validation_modet::INVARIANT)
{
check(code, vm);
DATA_CHECK(
vm,
code.op0().type() == code.op1().type(),
"lhs and rhs of assignment must have same type");
}
static void validate_full(
const codet &code,
const namespacet &ns,
const validation_modet vm = validation_modet::INVARIANT)
{
for(const exprt &op : code.operands())
{
validate_full_expr(op, ns, vm);
}
validate(code, ns, vm);
}
protected:
using codet::op0;
using codet::op1;
using codet::op2;
using codet::op3;
};
template<> inline bool can_cast_expr<code_assignt>(const exprt &base)
{
return detail::can_cast_code_impl(base, ID_assign);
}
inline void validate_expr(const code_assignt & x)
{
code_assignt::check(x);
}
inline const code_assignt &to_code_assign(const codet &code)
{
PRECONDITION(code.get_statement() == ID_assign);
code_assignt::check(code);
return static_cast<const code_assignt &>(code);
}
inline code_assignt &to_code_assign(codet &code)
{
PRECONDITION(code.get_statement() == ID_assign);
code_assignt::check(code);
return static_cast<code_assignt &>(code);
}
/// A `codet` representing the declaration of a local variable.
/// For example, if a variable (symbol) `x` is represented as a
/// \ref symbol_exprt `sym`, then the declaration of this variable can be
/// represented as `code_declt(sym)`.
class code_declt:public codet
{
public:
explicit code_declt(symbol_exprt symbol) : codet(ID_decl, {std::move(symbol)})
{
}
symbol_exprt &symbol()
{
return static_cast<symbol_exprt &>(op0());
}
const symbol_exprt &symbol() const
{
return static_cast<const symbol_exprt &>(op0());
}
const irep_idt &get_identifier() const
{
return symbol().get_identifier();
}
static void check(
const codet &code,
const validation_modet vm = validation_modet::INVARIANT)
{
// will be size()==1 in the future
DATA_CHECK(
vm,
code.operands().size() >= 1,
"declaration must have one or more operands");
DATA_CHECK(
vm,
code.op0().id() == ID_symbol,
"declaring a non-symbol: " +
id2string(to_symbol_expr(code.op0()).get_identifier()));
}
};
template<> inline bool can_cast_expr<code_declt>(const exprt &base)
{
return detail::can_cast_code_impl(base, ID_decl);
}
inline void validate_expr(const code_declt &x)
{
code_declt::check(x);
}
inline const code_declt &to_code_decl(const codet &code)
{
PRECONDITION(code.get_statement() == ID_decl);
code_declt::check(code);
return static_cast<const code_declt &>(code);
}
inline code_declt &to_code_decl(codet &code)
{
PRECONDITION(code.get_statement() == ID_decl);
code_declt::check(code);
return static_cast<code_declt &>(code);
}
/// A \ref codet representing the removal of a local variable going out of
/// scope.
class code_deadt:public codet
{
public:
explicit code_deadt(symbol_exprt symbol) : codet(ID_dead, {std::move(symbol)})
{
}
symbol_exprt &symbol()
{
return static_cast<symbol_exprt &>(op0());
}
const symbol_exprt &symbol() const
{
return static_cast<const symbol_exprt &>(op0());
}
const irep_idt &get_identifier() const
{
return symbol().get_identifier();
}
static void check(
const codet &code,
const validation_modet vm = validation_modet::INVARIANT)
{
DATA_CHECK(
vm,
code.operands().size() == 1,
"removal (code_deadt) must have one operand");
DATA_CHECK(
vm,
code.op0().id() == ID_symbol,
"removing a non-symbol: " + id2string(code.op0().id()) + "from scope");
}
protected:
using codet::op0;
using codet::op1;
using codet::op2;
using codet::op3;
};
template<> inline bool can_cast_expr<code_deadt>(const exprt &base)
{
return detail::can_cast_code_impl(base, ID_dead);
}
inline void validate_expr(const code_deadt &x)
{
code_deadt::check(x);
}
inline const code_deadt &to_code_dead(const codet &code)
{
PRECONDITION(code.get_statement() == ID_dead);
code_deadt::check(code);
return static_cast<const code_deadt &>(code);
}
inline code_deadt &to_code_dead(codet &code)
{
PRECONDITION(code.get_statement() == ID_dead);
code_deadt::check(code);
return static_cast<code_deadt &>(code);
}
/// An assumption, which must hold in subsequent code.
class code_assumet:public codet
{
public:
explicit code_assumet(exprt expr) : codet(ID_assume, {std::move(expr)})
{
}
const exprt &assumption() const
{
return op0();
}
exprt &assumption()
{
return op0();
}
protected:
using codet::op0;
using codet::op1;
using codet::op2;
using codet::op3;
};
template<> inline bool can_cast_expr<code_assumet>(const exprt &base)
{
return detail::can_cast_code_impl(base, ID_assume);
}
inline void validate_expr(const code_assumet &x)
{
validate_operands(x, 1, "assume must have one operand");
}
inline const code_assumet &to_code_assume(const codet &code)
{
PRECONDITION(code.get_statement() == ID_assume);
const code_assumet &ret = static_cast<const code_assumet &>(code);
validate_expr(ret);
return ret;
}
inline code_assumet &to_code_assume(codet &code)
{
PRECONDITION(code.get_statement() == ID_assume);
code_assumet &ret = static_cast<code_assumet &>(code);
validate_expr(ret);
return ret;
}
/// A non-fatal assertion, which checks a condition then permits execution to
/// continue.
class code_assertt:public codet
{
public:
explicit code_assertt(exprt expr) : codet(ID_assert, {std::move(expr)})
{
}
const exprt &assertion() const
{
return op0();
}
exprt &assertion()
{
return op0();
}
protected:
using codet::op0;
using codet::op1;
using codet::op2;
using codet::op3;
};
template<> inline bool can_cast_expr<code_assertt>(const exprt &base)
{
return detail::can_cast_code_impl(base, ID_assert);
}
inline void validate_expr(const code_assertt &x)
{
validate_operands(x, 1, "assert must have one operand");
}
inline const code_assertt &to_code_assert(const codet &code)
{
PRECONDITION(code.get_statement() == ID_assert);
const code_assertt &ret = static_cast<const code_assertt &>(code);
validate_expr(ret);
return ret;
}
inline code_assertt &to_code_assert(codet &code)
{
PRECONDITION(code.get_statement() == ID_assert);
code_assertt &ret = static_cast<code_assertt &>(code);
validate_expr(ret);
return ret;
}
/// A `codet` representing the declaration that an input of a particular
/// description has a value which corresponds to the value of a given expression
/// (or expressions).
/// When working with the C front end, calls to the `__CPROVER_input` intrinsic
/// can be added to the input code in order add instructions of this type to the
/// goto program.
/// The first argument is expected to be a C string denoting the input
/// identifier. The second argument is the expression for the input value.
class code_inputt : public codet
{
public:
/// This constructor is for support of calls to `__CPROVER_input` in user
/// code. Where the first first argument is a description which may be any
/// `const char *` and one or more corresponding expression arguments follow.
explicit code_inputt(
std::vector<exprt> arguments,
optionalt<source_locationt> location = {});
/// This constructor is intended for generating input instructions as part of
/// synthetic entry point code, rather than as part of user code.
/// \param description: This is used to construct an expression for a pointer
/// to a string constant containing the description text. This expression
/// is then used as the first argument.
/// \param expression: This expression corresponds to a value which should be
/// recorded as an input.
/// \param location: A location to associate with this instruction.
code_inputt(
const irep_idt &description,
exprt expression,
optionalt<source_locationt> location = {});
static void check(
const codet &code,
const validation_modet vm = validation_modet::INVARIANT);
};
template <>
inline bool can_cast_expr<code_inputt>(const exprt &base)
{
return detail::can_cast_code_impl(base, ID_input);
}
inline void validate_expr(const code_inputt &input)
{
code_inputt::check(input);
}
/// A `codet` representing the declaration that an output of a particular
/// description has a value which corresponds to the value of a given expression
/// (or expressions).
/// When working with the C front end, calls to the `__CPROVER_output` intrinsic
/// can be added to the input code in order add instructions of this type to the
/// goto program.
/// The first argument is expected to be a C string denoting the output
/// identifier. The second argument is the expression for the output value.
class code_outputt : public codet
{
public:
/// This constructor is for support of calls to `__CPROVER_output` in user
/// code. Where the first first argument is a description which may be any
/// `const char *` and one or more corresponding expression arguments follow.
explicit code_outputt(
std::vector<exprt> arguments,
optionalt<source_locationt> location = {});
/// This constructor is intended for generating output instructions as part of
/// synthetic entry point code, rather than as part of user code.
/// \param description: This is used to construct an expression for a pointer
/// to a string constant containing the description text.
/// \param expression: This expression corresponds to a value which should be
/// recorded as an output.
/// \param location: A location to associate with this instruction.
code_outputt(
const irep_idt &description,
exprt expression,
optionalt<source_locationt> location = {});
static void check(
const codet &code,
const validation_modet vm = validation_modet::INVARIANT);
};
template <>
inline bool can_cast_expr<code_outputt>(const exprt &base)
{
return detail::can_cast_code_impl(base, ID_output);
}
inline void validate_expr(const code_outputt &output)
{
code_outputt::check(output);
}
/// Create a fatal assertion, which checks a condition and then halts if it does
/// not hold. Equivalent to `ASSERT(condition); ASSUME(condition)`.
///
/// Source level assertions should probably use this, whilst checks that are
/// normally non-fatal at runtime, such as integer overflows, should use
/// code_assertt by itself.
/// \param condition: condition to assert
/// \param source_location: source location to attach to the generated code;
/// conventionally this should have `comment` and `property_class` fields set
/// to indicate the nature of the assertion.
/// \return A code block that asserts a condition then aborts if it does not
/// hold.
code_blockt create_fatal_assertion(
const exprt &condition, const source_locationt &source_location);
/// \ref codet representation of an if-then-else statement.
class code_ifthenelset:public codet
{
public:
DEPRECATED(SINCE(
2018,
12,
2,
"use code_ifthenelset(condition, then_code[, else_code]) instead"))
code_ifthenelset():codet(ID_ifthenelse)
{
operands().resize(3);
op1().make_nil();
op2().make_nil();
}
/// An if \p condition then \p then_code else \p else_code statement.
code_ifthenelset(exprt condition, codet then_code, codet else_code)
: codet(
ID_ifthenelse,
{std::move(condition), std::move(then_code), std::move(else_code)})
{
}
/// An if \p condition then \p then_code statement (no "else" case).
code_ifthenelset(exprt condition, codet then_code)
: codet(
ID_ifthenelse,
{std::move(condition), std::move(then_code), nil_exprt()})
{
}
const exprt &cond() const
{
return op0();
}
exprt &cond()
{
return op0();
}
const codet &then_case() const
{
return static_cast<const codet &>(op1());
}
bool has_else_case() const
{
return op2().is_not_nil();
}
const codet &else_case() const
{
return static_cast<const codet &>(op2());
}
codet &then_case()
{
return static_cast<codet &>(op1());
}
codet &else_case()
{
return static_cast<codet &>(op2());
}
protected:
using codet::op0;
using codet::op1;
using codet::op2;
using codet::op3;
};
template<> inline bool can_cast_expr<code_ifthenelset>(const exprt &base)
{
return detail::can_cast_code_impl(base, ID_ifthenelse);
}
inline void validate_expr(const code_ifthenelset &x)
{
validate_operands(x, 3, "if-then-else must have three operands");
}
inline const code_ifthenelset &to_code_ifthenelse(const codet &code)
{
PRECONDITION(code.get_statement() == ID_ifthenelse);
const code_ifthenelset &ret = static_cast<const code_ifthenelset &>(code);
validate_expr(ret);
return ret;
}
inline code_ifthenelset &to_code_ifthenelse(codet &code)
{
PRECONDITION(code.get_statement() == ID_ifthenelse);
code_ifthenelset &ret = static_cast<code_ifthenelset &>(code);
validate_expr(ret);
return ret;
}
/// \ref codet representing a `switch` statement.
class code_switcht:public codet
{
public:
code_switcht(exprt _value, codet _body)
: codet(ID_switch, {std::move(_value), std::move(_body)})
{
}
const exprt &value() const
{
return op0();
}
exprt &value()
{
return op0();
}
const codet &body() const
{
return to_code(op1());
}
codet &body()
{
return static_cast<codet &>(op1());
}
protected:
using codet::op0;
using codet::op1;
using codet::op2;
using codet::op3;
};
template<> inline bool can_cast_expr<code_switcht>(const exprt &base)
{
return detail::can_cast_code_impl(base, ID_switch);
}
inline void validate_expr(const code_switcht &x)
{
validate_operands(x, 2, "switch must have two operands");
}
inline const code_switcht &to_code_switch(const codet &code)
{
PRECONDITION(code.get_statement() == ID_switch);
const code_switcht &ret = static_cast<const code_switcht &>(code);
validate_expr(ret);
return ret;
}
inline code_switcht &to_code_switch(codet &code)
{
PRECONDITION(code.get_statement() == ID_switch);
code_switcht &ret = static_cast<code_switcht &>(code);
validate_expr(ret);
return ret;
}
/// \ref codet representing a `while` statement.
class code_whilet:public codet
{
public:
code_whilet(exprt _cond, codet _body)
: codet(ID_while, {std::move(_cond), std::move(_body)})
{
}
const exprt &cond() const
{
return op0();
}
exprt &cond()
{
return op0();
}
const codet &body() const
{
return to_code(op1());
}
codet &body()
{
return static_cast<codet &>(op1());
}
protected:
using codet::op0;
using codet::op1;
using codet::op2;
using codet::op3;
};
template<> inline bool can_cast_expr<code_whilet>(const exprt &base)
{
return detail::can_cast_code_impl(base, ID_while);
}
inline void validate_expr(const code_whilet &x)
{
validate_operands(x, 2, "while must have two operands");
}
inline const code_whilet &to_code_while(const codet &code)
{
PRECONDITION(code.get_statement() == ID_while);
const code_whilet &ret = static_cast<const code_whilet &>(code);
validate_expr(ret);
return ret;
}
inline code_whilet &to_code_while(codet &code)
{
PRECONDITION(code.get_statement() == ID_while);
code_whilet &ret = static_cast<code_whilet &>(code);
validate_expr(ret);
return ret;
}
/// \ref codet representation of a `do while` statement.
class code_dowhilet:public codet
{
public:
code_dowhilet(exprt _cond, codet _body)
: codet(ID_dowhile, {std::move(_cond), std::move(_body)})
{
}
const exprt &cond() const
{
return op0();
}
exprt &cond()
{
return op0();
}
const codet &body() const
{
return to_code(op1());
}
codet &body()
{
return static_cast<codet &>(op1());
}
protected:
using codet::op0;
using codet::op1;
using codet::op2;
using codet::op3;
};