-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathcpp_typecheck_conversions.cpp
More file actions
2073 lines (1769 loc) · 59.4 KB
/
cpp_typecheck_conversions.cpp
File metadata and controls
2073 lines (1769 loc) · 59.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
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: C++ Language Type Checking
Author:
\*******************************************************************/
/// \file
/// C++ Language Type Checking
#include "cpp_typecheck.h"
#include <util/arith_tools.h>
#include <util/c_types.h>
#include <util/config.h>
#include <util/expr_util.h>
#include <util/pointer_expr.h>
#include <util/simplify_expr.h>
#include <util/std_expr.h>
#include <ansi-c/c_qualifiers.h>
#include "cpp_util.h"
/// Lvalue-to-rvalue conversion
///
/// An lvalue (3.10) of a non-function, non-array type T can be
/// converted to an rvalue. If T is an incomplete type, a program
/// that necessitates this conversion is ill-formed. If the object
/// to which the lvalue refers is not an object of type T and is
/// not an object of a type derived from T, or if the object is
/// uninitialized, a program that necessitates this conversion has
/// undefined behavior. If T is a non-class type, the type of the
/// rvalue is the cv-unqualified version of T. Otherwise, the type of
/// the rvalue is T.
///
/// The value contained in the object indicated by the lvalue
/// is the rvalue result. When an lvalue-to-rvalue conversion
/// occurs within the operand of sizeof (5.3.3) the value contained
/// in the referenced object is not accessed, since that operator
/// does not evaluate its operand.
/// \par parameters: A typechecked lvalue expression
/// \return True iff the lvalue-to-rvalue conversion is possible. 'new_type'
/// contains the result of the conversion.
bool cpp_typecheckt::standard_conversion_lvalue_to_rvalue(
const exprt &expr,
exprt &new_expr) const
{
PRECONDITION(expr.get_bool(ID_C_lvalue));
if(expr.type().id() == ID_code)
return false;
if(
expr.type().id() == ID_struct &&
to_struct_type(expr.type()).is_incomplete())
return false;
if(expr.type().id() == ID_union && to_union_type(expr.type()).is_incomplete())
return false;
new_expr=expr;
new_expr.remove(ID_C_lvalue);
return true;
}
/// Array-to-pointer conversion
///
/// An lvalue or rvalue of type "array of N T" or "array of unknown
/// bound of T" can be converted to an rvalue of type "pointer to T."
/// The result is a pointer to the first element of the array.
/// \par parameters: An array expression
/// \return True iff the array-to-pointer conversion is possible. The result of
/// the conversion is stored in 'new_expr'.
bool cpp_typecheckt::standard_conversion_array_to_pointer(
const exprt &expr,
exprt &new_expr) const
{
PRECONDITION(expr.type().id() == ID_array);
index_exprt index(expr, from_integer(0, c_index_type()));
index.set(ID_C_lvalue, true);
new_expr=address_of_exprt(index);
return true;
}
/// Function-to-pointer conversion
///
/// An lvalue of function type T can be converted to an rvalue of type
/// "pointer to T." The result is a pointer to the function.50)
/// \par parameters: A function expression
/// \return True iff the array-to-pointer conversion is possible. The result of
/// the conversion is stored in 'new_expr'.
bool cpp_typecheckt::standard_conversion_function_to_pointer(
const exprt &expr, exprt &new_expr) const
{
if(!expr.get_bool(ID_C_lvalue))
return false;
new_expr=address_of_exprt(expr);
return true;
}
/// Qualification conversion
/// \par parameters: A typechecked expression 'expr', a destination
/// type 'type'
/// \return True iff the qualification conversion is possible. The result of the
/// conversion is stored in 'new_expr'.
bool cpp_typecheckt::standard_conversion_qualification(
const exprt &expr,
const typet &type,
exprt &new_expr) const
{
if(expr.type().id()!=ID_pointer ||
is_reference(expr.type()))
return false;
if(expr.get_bool(ID_C_lvalue))
return false;
if(expr.type()!=type)
return false;
typet sub_from = to_pointer_type(expr.type()).base_type();
typet sub_to = to_pointer_type(type).base_type();
bool const_to=true;
while(sub_from.id()==ID_pointer)
{
c_qualifierst qual_from(sub_from);
c_qualifierst qual_to(sub_to);
if(!qual_to.is_constant)
const_to=false;
if(qual_from.is_constant && !qual_to.is_constant)
return false;
if(qual_from!=qual_to && !const_to)
return false;
typet tmp1 = to_pointer_type(sub_from).base_type();
sub_from.swap(tmp1);
typet tmp2 = sub_to.add_subtype();
sub_to.swap(tmp2);
}
c_qualifierst qual_from(sub_from);
c_qualifierst qual_to(sub_to);
if(qual_from.is_subset_of(qual_to))
{
new_expr=expr;
new_expr.type()=type;
return true;
}
return false;
}
/// Integral-promotion conversion
///
/// An rvalue of type char, signed char, unsigned char, short int,
/// or unsigned short int can be converted to an rvalue of type int
/// if int can represent all the values of the source type; otherwise,
/// the source rvalue can be converted to an rvalue of type unsigned int.
///
/// An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) can
/// be converted to an rvalue of the first of the following types that
/// can represent all the values of its underlying type: int, unsigned int,
/// long, or unsigned long.
///
/// An rvalue for an integral bit-field (9.6) can be converted
/// to an rvalue of type int if int can represent all the values of the
/// bit-field; otherwise, it can be converted to unsigned int if
/// unsigned int can represent all the values of the bit-field.
/// If the bit-field is larger yet, no integral promotion applies to
/// it. If the bit-field has an enumerated type, it is treated as
/// any other value of that type for promotion purposes.
///
/// An rvalue of type bool can be converted to an rvalue of type int,
/// with false becoming zero and true becoming one.
/// \par parameters: A typechecked expression 'expr'
/// \return True iff the integral promotion is possible. The result of the
/// conversion is stored in 'new_expr'.
bool cpp_typecheckt::standard_conversion_integral_promotion(
const exprt &expr,
exprt &new_expr) const
{
if(expr.get_bool(ID_C_lvalue))
return false;
c_qualifierst qual_from;
qual_from.read(expr.type());
typet int_type=signed_int_type();
qual_from.write(int_type);
if(expr.type().id()==ID_signedbv)
{
std::size_t width=to_signedbv_type(expr.type()).get_width();
if(width >= config.ansi_c.int_width)
return false;
new_expr = typecast_exprt(expr, int_type);
return true;
}
if(expr.type().id()==ID_unsignedbv)
{
std::size_t width=to_unsignedbv_type(expr.type()).get_width();
if(width >= config.ansi_c.int_width)
return false;
new_expr = typecast_exprt(expr, int_type);
return true;
}
if(expr.is_boolean() || expr.type().id() == ID_c_bool)
{
new_expr = typecast_exprt(expr, int_type);
return true;
}
if(expr.type().id()==ID_c_enum_tag)
{
new_expr = typecast_exprt(expr, int_type);
return true;
}
return false;
}
/// Floating-point-promotion conversion
///
/// An rvalue of type float can be converted to an rvalue of type
/// double. The value is unchanged.
/// \par parameters: A typechecked expression 'expr'
/// \return True iff the integral promotion is possible. The result of the
/// conversion is stored in 'new_expr'.
bool cpp_typecheckt::standard_conversion_floating_point_promotion(
const exprt &expr,
exprt &new_expr) const
{
if(expr.get_bool(ID_C_lvalue))
return false;
// we only do that with 'float',
// not with 'double' or 'long double'
if(expr.type()!=float_type())
return false;
std::size_t width=to_floatbv_type(expr.type()).get_width();
if(width!=config.ansi_c.single_width)
return false;
c_qualifierst qual_from;
qual_from.read(expr.type());
new_expr = typecast_exprt(expr, double_type());
qual_from.write(new_expr.type());
return true;
}
/// Integral conversion
///
/// An rvalue of type char, signed char, unsigned char, short int,
/// An rvalue of an integer type can be converted to an rvalue of
/// another integer type. An rvalue of an enumeration type can be
/// converted to an rvalue of an integer type.
///
/// If the destination type is unsigned, the resulting value is the
/// least unsigned integer congruent to the source integer (modulo
/// 2n where n is the number of bits used to represent the unsigned
/// type). [Note: In a two's complement representation, this
/// conversion is conceptual and there is no change in the bit
/// pattern (if there is no truncation). ]
///
/// If the destination type is signed, the value is unchanged if it
/// can be represented in the destination type (and bit-field width);
/// otherwise, the value is implementation-defined.
///
/// If the destination type is bool, see 4.12. If the source type is
/// bool, the value false is converted to zero and the value true is
/// converted to one.
///
/// The conversions allowed as integral promotions are excluded from
/// the set of integral conversions.
/// \par parameters: A typechecked expression 'expr', a destination
/// type 'type'
/// \return True iff the integral promotion is possible. The result of the
/// conversion is stored in 'new_expr'.
bool cpp_typecheckt::standard_conversion_integral_conversion(
const exprt &expr,
const typet &type,
exprt &new_expr) const
{
if(type.id()!=ID_signedbv &&
type.id()!=ID_unsignedbv)
return false;
if(
expr.type().id() != ID_signedbv && expr.type().id() != ID_unsignedbv &&
expr.type().id() != ID_c_bool && !expr.is_boolean() &&
expr.type().id() != ID_c_enum_tag)
{
return false;
}
if(expr.get_bool(ID_C_lvalue))
return false;
c_qualifierst qual_from;
qual_from.read(expr.type());
new_expr = typecast_exprt::conditional_cast(expr, type);
qual_from.write(new_expr.type());
return true;
}
/// Floating-integral conversion
///
/// An rvalue of a floating point type can be converted to an rvalue
/// of an integer type. The conversion truncates; that is, the
/// fractional part is discarded. The behavior is undefined if the
/// truncated value cannot be represented in the destination type.
/// [Note: If the destination type is bool, see 4.12. ]
///
/// An rvalue of an integer type or of an enumeration type can be
/// converted to an rvalue of a floating point type. The result is
/// exact if possible. Otherwise, it is an implementation-defined
/// choice of either the next lower or higher representable value.
/// [Note: loss of precision occurs if the integral value cannot be
/// represented exactly as a value of the floating type. ] If the
/// source type is bool, the value false is converted to zero and the
/// value true is converted to one.
/// \par parameters: A typechecked expression 'expr'
/// \return True iff the conversion is possible. The result of the conversion is
/// stored in 'new_expr'.
bool cpp_typecheckt::standard_conversion_floating_integral_conversion(
const exprt &expr,
const typet &type,
exprt &new_expr) const
{
if(expr.get_bool(ID_C_lvalue))
return false;
if(expr.type().id()==ID_floatbv ||
expr.type().id()==ID_fixedbv)
{
if(type.id()!=ID_signedbv &&
type.id()!=ID_unsignedbv)
return false;
}
else if(expr.type().id()==ID_signedbv ||
expr.type().id()==ID_unsignedbv ||
expr.type().id()==ID_c_enum_tag)
{
if(type.id()!=ID_fixedbv &&
type.id()!=ID_floatbv)
return false;
}
else
return false;
c_qualifierst qual_from;
qual_from.read(expr.type());
new_expr = typecast_exprt::conditional_cast(expr, type);
qual_from.write(new_expr.type());
return true;
}
/// Floating-point conversion
///
/// An rvalue of floating point type can be converted to an rvalue
/// of another floating point type. If the source value can be exactly
/// represented in the destination type, the result of the conversion
/// is that exact representation. If the source value is between two
/// adjacent destination values, the result of the conversion is an
/// implementation-defined choice of either of those values. Otherwise,
/// the behavior is undefined.
///
/// The conversions allowed as floating point promotions are excluded
/// from the set of floating point conversions.
/// \par parameters: A typechecked expression 'expr', a destination
/// type 'type'
/// \return True iff the floating-point conversion is possible. The result of
/// the conversion is stored in 'new_expr'.
bool cpp_typecheckt::standard_conversion_floating_point_conversion(
const exprt &expr,
const typet &type,
exprt &new_expr) const
{
if(expr.type().id()!=ID_floatbv &&
expr.type().id()!=ID_fixedbv)
return false;
if(type.id()!=ID_floatbv &&
type.id()!=ID_fixedbv)
return false;
if(expr.get_bool(ID_C_lvalue))
return false;
c_qualifierst qual_from;
qual_from.read(expr.type());
new_expr = typecast_exprt::conditional_cast(expr, type);
qual_from.write(new_expr.type());
return true;
}
/// Pointer conversion
///
/// A null pointer constant is an integral constant expression
/// (5.19) rvalue of integer type that evaluates to zero. A null
/// pointer constant can be converted to a pointer type; the result
/// is the null pointer value of that type and is distinguishable
/// from every other value of pointer to object or pointer to
/// function type. Two null pointer values of the same type shall
/// compare equal. The conversion of a null pointer constant to a
/// pointer to cv-qualified type is a single conversion, and not the
/// sequence of a pointer conversion followed by a qualification
/// conversion (4.4).
///
/// An rvalue of type "pointer to cv T," where T is an object type,
/// can be converted to an rvalue of type "pointer to cv void." The
/// result of converting a "pointer to cv T" to a "pointer to cv
/// void" points to the start of the storage location where the
/// object of type T resides, as if the object is a most derived
/// object (1.8) of type T (that is, not a base class subobject).
///
/// An rvalue of type "pointer to cv D," where D is a class type,
/// can be converted to an rvalue of type "pointer to cv B," where
/// B is a base class (clause 10) of D. If B is an inaccessible
/// (clause 11) or ambiguous (10.2) base class of D, a program that
/// necessitates this conversion is ill-formed. The result of the
/// conversion is a pointer to the base class sub-object of the
/// derived class object. The null pointer value is converted to
/// the null pointer value of the destination type.
/// \par parameters: A typechecked expression 'expr', a destination
/// type 'type'
/// \return True iff the pointer conversion is possible. The result of the
/// conversion is stored in 'new_expr'.
bool cpp_typecheckt::standard_conversion_pointer(
const exprt &expr,
const typet &type,
exprt &new_expr)
{
if(type.id()!=ID_pointer ||
is_reference(type))
return false;
if(expr.get_bool(ID_C_lvalue))
return false;
// integer 0 to NULL pointer conversion?
if(simplify_expr(expr, *this) == 0 && expr.type().id() != ID_pointer)
{
new_expr=expr;
new_expr.set(ID_value, ID_NULL);
new_expr.type()=type;
return true;
}
if(type.find(ID_to_member).is_not_nil())
return false;
if(
expr.type().id() != ID_pointer ||
expr.type().find(ID_to_member).is_not_nil())
{
return false;
}
const pointer_typet &pointer_type = to_pointer_type(type);
const typet &sub_from = to_pointer_type(expr.type()).base_type();
const typet &sub_to = pointer_type.base_type();
// std::nullptr_t to _any_ pointer type
if(sub_from.id()==ID_nullptr)
return true;
// anything but function pointer to void *
if(sub_from.id()!=ID_code && sub_to.id()==ID_empty)
{
c_qualifierst qual_from;
qual_from.read(to_pointer_type(expr.type()).base_type());
new_expr = typecast_exprt::conditional_cast(expr, type);
qual_from.write(to_pointer_type(new_expr.type()).base_type());
return true;
}
// struct * to struct *
if(sub_from.id() == ID_struct_tag && sub_to.id() == ID_struct_tag)
{
const struct_typet &from_struct = follow_tag(to_struct_tag_type(sub_from));
const struct_typet &to_struct = follow_tag(to_struct_tag_type(sub_to));
if(subtype_typecast(from_struct, to_struct))
{
c_qualifierst qual_from;
qual_from.read(to_pointer_type(expr.type()).base_type());
new_expr=expr;
make_ptr_typecast(new_expr, pointer_type);
qual_from.write(to_pointer_type(new_expr.type()).base_type());
return true;
}
}
return false;
}
/// Pointer-to-member conversion
///
/// A null pointer constant (4.10) can be converted to a pointer to
/// member type; the result is the null member pointer value of that
/// type and is distinguishable from any pointer to member not created
/// from a null pointer constant. Two null member pointer values of
/// the same type shall compare equal. The conversion of a null pointer
/// constant to a pointer to member of cv-qualified type is a single
/// conversion, and not the sequence of a pointer to member conversion
/// followed by a qualification conversion (4.4).
///
/// An rvalue of type "pointer to member of B of type cv T," where B
/// is a class type, can be converted to an rvalue of type "pointer
/// to member of D of type cv T," where D is a derived class
/// (clause 10) of B. If B is an inaccessible (clause 11), ambiguous
/// (10.2) or virtual (10.1) base class of D, a program that
/// necessitates this conversion is ill-formed. The result of the
/// conversion refers to the same member as the pointer to member
/// before the conversion took place, but it refers to the base class
/// member as if it were a member of the derived class. The result
/// refers to the member in D"s instance of B. Since the result has
/// type "pointer to member of D of type cv T," it can be dereferenced
/// with a D object. The result is the same as if the pointer to
/// member of B were dereferenced with the B sub-object of D. The null
/// member pointer value is converted to the null member pointer value
/// of the destination type.52)
/// \par parameters: A typechecked expression 'expr', a destination
/// type 'type'
/// \return True iff the pointer-to-member conversion is possible. The result of
/// the conversion is stored in 'new_expr'.
bool cpp_typecheckt::standard_conversion_pointer_to_member(
const exprt &expr,
const typet &type,
exprt &new_expr)
{
if(
type.id() != ID_pointer || is_reference(type) ||
type.find(ID_to_member).is_nil())
{
return false;
}
if(expr.type().id() != ID_pointer || expr.type().find(ID_to_member).is_nil())
return false;
if(
to_pointer_type(type).base_type() !=
to_pointer_type(expr.type()).base_type())
{
// base types are different
if(
to_pointer_type(type).base_type().id() == ID_code &&
to_pointer_type(expr.type()).base_type().id() == ID_code)
{
code_typet code1 = to_code_type(to_pointer_type(expr.type()).base_type());
DATA_INVARIANT(!code1.parameters().empty(), "must have parameters");
code_typet::parametert this1=code1.parameters()[0];
INVARIANT(this1.get_this(), "first parameter should be `this'");
code1.parameters().erase(code1.parameters().begin());
code_typet code2 = to_code_type(to_pointer_type(type).base_type());
DATA_INVARIANT(!code2.parameters().empty(), "must have parameters");
code_typet::parametert this2=code2.parameters()[0];
INVARIANT(this2.get_this(), "first parameter should be `this'");
code2.parameters().erase(code2.parameters().begin());
if(
to_pointer_type(this2.type()).base_type().get_bool(ID_C_constant) &&
!to_pointer_type(this1.type()).base_type().get_bool(ID_C_constant))
return false;
// give a second chance ignoring `this'
if(code1!=code2)
return false;
}
else
return false;
}
if(expr.get_bool(ID_C_lvalue))
return false;
if(expr.is_constant() && to_constant_expr(expr).is_null_pointer())
{
new_expr = typecast_exprt::conditional_cast(expr, type);
return true;
}
const struct_typet &from_struct = follow_tag(to_struct_tag_type(
static_cast<const typet &>(expr.type().find(ID_to_member))));
const struct_typet &to_struct = follow_tag(
to_struct_tag_type(static_cast<const typet &>(type.find(ID_to_member))));
if(subtype_typecast(to_struct, from_struct))
{
new_expr = typecast_exprt::conditional_cast(expr, type);
return true;
}
return false;
}
/// Boolean conversion
///
/// An rvalue of arithmetic, enumeration, pointer, or pointer to
/// member type can be converted to an rvalue of type bool.
/// A zero value, null pointer value, or null member pointer value is
/// converted to false; any other value is converted to true.
/// \par parameters: A typechecked expression 'expr'
/// \return True iff the boolean conversion is possible. The result of the
/// conversion is stored in 'new_expr'.
bool cpp_typecheckt::standard_conversion_boolean(
const exprt &expr, exprt &new_expr) const
{
if(expr.get_bool(ID_C_lvalue))
return false;
if(
expr.type().id() != ID_signedbv && expr.type().id() != ID_unsignedbv &&
expr.type().id() != ID_pointer && !expr.is_boolean() &&
expr.type().id() != ID_c_enum_tag)
{
return false;
}
c_qualifierst qual_from;
qual_from.read(expr.type());
typet Bool = c_bool_type();
qual_from.write(Bool);
new_expr = typecast_exprt::conditional_cast(expr, Bool);
return true;
}
/// Standard Conversion Sequence
///
/// A standard conversion sequence is a sequence of standard conversions
/// in the following order:
///
/// * Zero or one conversion from the following set: lvalue-to-rvalue
/// conversion, array-to-pointer conversion, and function-to-pointer
/// conversion.
///
/// * Zero or one conversion from the following set: integral
/// promotions, floating point promotion, integral conversions,
/// floating point conversions, floating-integral conversions,
/// pointer conversions, pointer to member conversions, and boolean
/// conversions.
///
/// * Zero or one qualification conversion.
/// \par parameters: A typechecked expression 'expr', a destination
/// type 'type'.
/// \return True iff a standard conversion sequence exists. The result of the
/// conversion is stored in 'new_expr'. The reference 'rank' is incremented.
bool cpp_typecheckt::standard_conversion_sequence(
const exprt &expr,
const typet &type,
exprt &new_expr,
unsigned &rank)
{
PRECONDITION(!is_reference(expr.type()) && !is_reference(type));
exprt curr_expr=expr;
// bit fields are converted like their underlying type
if(type.id()==ID_c_bit_field)
return standard_conversion_sequence(
expr, to_c_bit_field_type(type).underlying_type(), new_expr, rank);
// we turn bit fields into their underlying type
if(curr_expr.type().id()==ID_c_bit_field)
curr_expr = typecast_exprt(
curr_expr, to_c_bit_field_type(curr_expr.type()).underlying_type());
if(curr_expr.type().id()==ID_array)
{
if(type.id()==ID_pointer)
{
if(!standard_conversion_array_to_pointer(curr_expr, new_expr))
return false;
}
}
else if(curr_expr.type().id()==ID_code &&
type.id()==ID_pointer)
{
if(!standard_conversion_function_to_pointer(curr_expr, new_expr))
return false;
}
else if(curr_expr.get_bool(ID_C_lvalue))
{
if(!standard_conversion_lvalue_to_rvalue(curr_expr, new_expr))
return false;
}
else
new_expr=curr_expr;
curr_expr.swap(new_expr);
// two enums are the same if the tag is the same,
// even if the width differs (enum bit-fields!)
if(type.id() == ID_c_enum_tag && curr_expr.type().id() == ID_c_enum_tag)
{
if(
to_tag_type(type).get_identifier() ==
to_tag_type(curr_expr.type()).get_identifier())
{
return true;
}
else
{
// In contrast to C, we simply don't allow implicit conversions
// between enums.
return false;
}
}
// need to consider #c_type
if(
curr_expr.type() != type ||
curr_expr.type().get(ID_C_c_type) != type.get(ID_C_c_type))
{
if(
type.id() == ID_signedbv || type.id() == ID_unsignedbv ||
type.id() == ID_c_enum_tag)
{
if(!standard_conversion_integral_promotion(curr_expr, new_expr) ||
new_expr.type() != type)
{
if(!standard_conversion_integral_conversion(curr_expr, type, new_expr))
{
if(!standard_conversion_floating_integral_conversion(
curr_expr, type, new_expr))
return false;
}
rank+=3;
}
else
rank+=2;
}
else if(type.id()==ID_floatbv || type.id()==ID_fixedbv)
{
if(!standard_conversion_floating_point_promotion(curr_expr, new_expr) ||
new_expr.type() != type)
{
if(!standard_conversion_floating_point_conversion(
curr_expr, type, new_expr) &&
!standard_conversion_floating_integral_conversion(
curr_expr, type, new_expr))
return false;
rank += 3;
}
else
rank += 2;
}
else if(type.id()==ID_pointer)
{
if(
expr.type().id() == ID_pointer &&
to_pointer_type(expr.type()).base_type().id() == ID_nullptr)
{
// std::nullptr_t to _any_ pointer type is ok
new_expr = typecast_exprt::conditional_cast(new_expr, type);
}
else if(!standard_conversion_pointer(curr_expr, type, new_expr))
{
if(!standard_conversion_pointer_to_member(curr_expr, type, new_expr))
return false;
}
rank += 3;
}
else if(type.id() == ID_c_bool)
{
if(!standard_conversion_boolean(curr_expr, new_expr))
return false;
rank += 3;
}
else if(type.id() == ID_bool)
{
new_expr = is_not_zero(curr_expr, *this);
rank += 3;
}
else
return false;
}
else
new_expr=curr_expr;
curr_expr.swap(new_expr);
if(curr_expr.type().id()==ID_pointer)
{
typet sub_from=curr_expr.type();
typet sub_to=type;
do
{
typet tmp_from = to_pointer_type(sub_from).base_type();
sub_from.swap(tmp_from);
typet tmp_to = sub_to.add_subtype();
sub_to.swap(tmp_to);
c_qualifierst qual_from;
qual_from.read(sub_from);
c_qualifierst qual_to;
qual_to.read(sub_to);
if(qual_from!=qual_to)
{
rank+=1;
break;
}
}
while(sub_from.id()==ID_pointer);
if(!standard_conversion_qualification(curr_expr, type, new_expr))
return false;
}
else
{
new_expr=curr_expr;
new_expr.type()=type;
}
return true;
}
/// User-defined conversion sequence
/// \par parameters: A typechecked expression 'expr', a destination
/// type 'type'.
/// \return True iff a user-defined conversion sequence exists. The result of
/// the conversion is stored in 'new_expr'.
bool cpp_typecheckt::user_defined_conversion_sequence(
const exprt &expr,
const typet &to,
exprt &new_expr,
unsigned &rank)
{
PRECONDITION(!is_reference(expr.type()));
PRECONDITION(!is_reference(to));
const typet &from = expr.type();
new_expr.make_nil();
// special case:
// A conversion from a type to the same type is given an exact
// match rank even though a user-defined conversion is used
if(from==to)
rank+=0;
else
rank+=4; // higher than all the standard conversions
if(to.id() == ID_struct_tag)
{
std::string err_msg;
if(cpp_is_pod(to))
{
if(from.id() == ID_struct_tag)
{
const struct_typet &from_struct = follow_tag(to_struct_tag_type(from));
const struct_typet &to_struct = follow_tag(to_struct_tag_type(to));
// potentially requires
// expr.get_bool(ID_C_lvalue) ??
if(subtype_typecast(from_struct, to_struct))
{
exprt address=address_of_exprt(expr);
// simplify address
if(expr.id()==ID_dereference)
address = to_dereference_expr(expr).pointer();
pointer_typet ptr_sub = pointer_type(to);
c_qualifierst qual_from;
qual_from.read(expr.type());
qual_from.write(ptr_sub.base_type());
make_ptr_typecast(address, ptr_sub);
const dereference_exprt deref(address);
// create temporary object
side_effect_exprt tmp_object_expr(
ID_temporary_object, to, expr.source_location());
tmp_object_expr.copy_to_operands(deref);
tmp_object_expr.set(ID_C_lvalue, true);
tmp_object_expr.set(ID_mode, ID_cpp);
new_expr.swap(tmp_object_expr);
return true;
}
}
}
else
{
bool found=false;
const auto &struct_type_to = follow_tag(to_struct_tag_type(to));
for(const auto &component : struct_type_to.components())
{
if(component.get_bool(ID_from_base))
continue;
if(component.get_bool(ID_is_explicit))
continue;
const typet &comp_type = component.type();
if(comp_type.id() !=ID_code)
continue;
if(to_code_type(comp_type).return_type().id() != ID_constructor)
continue;
// TODO: ellipsis
const auto ¶meters = to_code_type(comp_type).parameters();
if(parameters.size() != 2)
continue;
exprt curr_arg1 = parameters[1];
typet arg1_type=curr_arg1.type();
if(is_reference(arg1_type))
{
typet tmp = to_reference_type(arg1_type).base_type();
arg1_type.swap(tmp);
}
unsigned tmp_rank=0;
if(arg1_type.id() != ID_struct_tag)
{
exprt tmp_expr;
if(standard_conversion_sequence(
expr, arg1_type, tmp_expr, tmp_rank))
{
// check if it's ambiguous
if(found)
return false;
found=true;
if(expr.get_bool(ID_C_lvalue))
tmp_expr.set(ID_C_lvalue, true);
tmp_expr.add_source_location()=expr.source_location();
exprt func_symb = cpp_symbol_expr(lookup(component.get_name()));
func_symb.type()=comp_type;
already_typechecked_exprt::make_already_typechecked(func_symb);
// create temporary object
side_effect_expr_function_callt ctor_expr(
std::move(func_symb),
{tmp_expr},
uninitialized_typet{},
expr.source_location());
typecheck_side_effect_function_call(ctor_expr);
CHECK_RETURN(ctor_expr.get(ID_statement) == ID_temporary_object);
new_expr.swap(ctor_expr);
if(struct_type_to.get_bool(ID_C_constant))
new_expr.type().set(ID_C_constant, true);
rank += tmp_rank;
}
}