-
Notifications
You must be signed in to change notification settings - Fork 300
Expand file tree
/
Copy pathcpp_typecheck_expr.cpp
More file actions
2375 lines (1984 loc) · 64 KB
/
Copy pathcpp_typecheck_expr.cpp
File metadata and controls
2375 lines (1984 loc) · 64 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: Daniel Kroening, kroening@cs.cmu.edu
\*******************************************************************/
/// \file
/// C++ Language Type Checking
#include "cpp_typecheck.h"
#ifdef DEBUG
#include <iostream>
#endif
#include <util/arith_tools.h>
#include <util/c_types.h>
#include <util/config.h>
#include <util/expr_initializer.h>
#include <util/mathematical_types.h>
#include <util/pointer_expr.h>
#include <util/pointer_offset_size.h>
#include <util/symbol_table_base.h>
#include <ansi-c/c_qualifiers.h>
#include "cpp_exception_id.h"
#include "cpp_type2name.h"
#include "cpp_typecheck_fargs.h"
#include "cpp_util.h"
#include "expr2cpp.h"
bool cpp_typecheckt::find_parent(
const symbolt &symb,
const irep_idt &base_name,
irep_idt &identifier)
{
for(const auto &b : to_struct_type(symb.type).bases())
{
const irep_idt &id = b.type().get_identifier();
if(lookup(id).base_name == base_name)
{
identifier = id;
return true;
}
}
return false;
}
/// Called after the operands are done
void cpp_typecheckt::typecheck_expr_main(exprt &expr)
{
if(expr.id()==ID_cpp_name)
typecheck_expr_cpp_name(expr, cpp_typecheck_fargst());
else if(expr.id()=="cpp-this")
typecheck_expr_this(expr);
else if(expr.id() == ID_pointer_to_member)
convert_pmop(expr);
else if(expr.id() == ID_new_object)
{
}
else if(operator_is_overloaded(expr))
{
}
else if(expr.id()=="explicit-typecast")
typecheck_expr_explicit_typecast(expr);
else if(expr.id()=="explicit-constructor-call")
typecheck_expr_explicit_constructor_call(expr);
else if(expr.id()==ID_code)
{
#ifdef DEBUG
std::cerr << "E: " << expr.pretty() << '\n';
std::cerr << "cpp_typecheckt::typecheck_expr_main got code\n";
#endif
UNREACHABLE;
}
else if(expr.id()==ID_symbol)
{
// ignore here
#ifdef DEBUG
std::cerr << "E: " << expr.pretty() << '\n';
std::cerr << "cpp_typecheckt::typecheck_expr_main got symbol\n";
#endif
}
else if(expr.id()=="__is_base_of")
{
// an MS extension
// http://msdn.microsoft.com/en-us/library/ms177194(v=vs.80).aspx
typet base=static_cast<const typet &>(expr.find("type_arg1"));
typet deriv=static_cast<const typet &>(expr.find("type_arg2"));
typecheck_type(base);
typecheck_type(deriv);
if(base.id() != ID_struct_tag || deriv.id() != ID_struct_tag)
expr=false_exprt();
else
{
irep_idt base_name = follow_tag(to_struct_tag_type(base)).get(ID_name);
const class_typet &class_type =
to_class_type(follow_tag(to_struct_tag_type(deriv)));
if(class_type.has_base(base_name))
expr=true_exprt();
else
expr=false_exprt();
}
}
else if(expr.id()==ID_msc_uuidof)
{
// these appear to have type "struct _GUID"
// and they are lvalues!
expr.type() = struct_tag_typet("tag-_GUID");
expr.set(ID_C_lvalue, true);
}
else if(expr.id()==ID_noexcept)
{
// TODO
expr=false_exprt();
}
else if(expr.id()==ID_initializer_list)
{
expr.type().id(ID_initializer_list);
}
else
c_typecheck_baset::typecheck_expr_main(expr);
}
void cpp_typecheckt::typecheck_expr_trinary(if_exprt &expr)
{
PRECONDITION(expr.operands().size() == 3);
implicit_typecast(expr.op0(), bool_typet());
if(expr.op1().type().id()==ID_empty ||
expr.op1().type().id()==ID_empty)
{
if(expr.op1().get_bool(ID_C_lvalue))
{
exprt e1(expr.op1());
if(!standard_conversion_lvalue_to_rvalue(e1, expr.op1()))
{
error().source_location=e1.find_source_location();
error() << "lvalue to rvalue conversion" << eom;
throw 0;
}
}
if(expr.op1().type().id()==ID_array)
{
exprt e1(expr.op1());
if(!standard_conversion_array_to_pointer(e1, expr.op1()))
{
error().source_location=e1.find_source_location();
error() << "array to pointer conversion" << eom;
throw 0;
}
}
if(expr.op1().type().id()==ID_code)
{
exprt e1(expr.op1());
if(!standard_conversion_function_to_pointer(e1, expr.op1()))
{
error().source_location=e1.find_source_location();
error() << "function to pointer conversion" << eom;
throw 0;
}
}
if(expr.op2().get_bool(ID_C_lvalue))
{
exprt e2(expr.op2());
if(!standard_conversion_lvalue_to_rvalue(e2, expr.op2()))
{
error().source_location=e2.find_source_location();
error() << "lvalue to rvalue conversion" << eom;
throw 0;
}
}
if(expr.op2().type().id()==ID_array)
{
exprt e2(expr.op2());
if(!standard_conversion_array_to_pointer(e2, expr.op2()))
{
error().source_location=e2.find_source_location();
error() << "array to pointer conversion" << eom;
throw 0;
}
}
if(expr.op2().type().id()==ID_code)
{
exprt e2(expr.op2());
if(!standard_conversion_function_to_pointer(e2, expr.op2()))
{
error().source_location=expr.find_source_location();
error() << "function to pointer conversion" << eom;
throw 0;
}
}
if(expr.op1().get(ID_statement)==ID_throw &&
expr.op2().get(ID_statement)!=ID_throw)
expr.type()=expr.op2().type();
else if(expr.op2().get(ID_statement)==ID_throw &&
expr.op1().get(ID_statement)!=ID_throw)
expr.type()=expr.op1().type();
else if(expr.op1().type().id()==ID_empty &&
expr.op2().type().id()==ID_empty)
expr.type() = void_type();
else
{
error().source_location=expr.find_source_location();
error() << "bad types for operands" << eom;
throw 0;
}
return;
}
if(expr.op1().type() == expr.op2().type())
{
c_qualifierst qual1, qual2;
qual1.read(expr.op1().type());
qual2.read(expr.op2().type());
if(qual1.is_subset_of(qual2))
expr.type()=expr.op1().type();
else
expr.type()=expr.op2().type();
}
else
{
exprt e1=expr.op1();
exprt e2=expr.op2();
if(implicit_conversion_sequence(expr.op1(), expr.op2().type(), e1))
{
expr.type()=e1.type();
expr.op1().swap(e1);
}
else if(implicit_conversion_sequence(expr.op2(), expr.op1().type(), e2))
{
expr.type()=e2.type();
expr.op2().swap(e2);
}
else if(
expr.op1().type().id() == ID_array &&
expr.op2().type().id() == ID_array &&
to_array_type(expr.op1().type()).element_type() ==
to_array_type(expr.op2().type()).element_type())
{
// array-to-pointer conversion
index_exprt index1(expr.op1(), from_integer(0, c_index_type()));
index_exprt index2(expr.op2(), from_integer(0, c_index_type()));
address_of_exprt addr1(index1);
address_of_exprt addr2(index2);
expr.op1()=addr1;
expr.op2()=addr2;
expr.type()=addr1.type();
return;
}
else
{
error().source_location=expr.find_source_location();
error() << "types are incompatible.\n"
<< "I got '" << type2cpp(expr.op1().type(), *this) << "' and '"
<< type2cpp(expr.op2().type(), *this) << "'." << eom;
throw 0;
}
}
if(expr.op1().get_bool(ID_C_lvalue) &&
expr.op2().get_bool(ID_C_lvalue))
expr.set(ID_C_lvalue, true);
return;
}
void cpp_typecheckt::typecheck_expr_member(exprt &expr)
{
typecheck_expr_member(
expr,
cpp_typecheck_fargst());
}
void cpp_typecheckt::typecheck_expr_sizeof(exprt &expr)
{
// We need to overload, "sizeof-expression" can be mis-parsed
// as a type.
if(expr.operands().empty())
{
const typet &type=
static_cast<const typet &>(expr.find(ID_type_arg));
if(type.id()==ID_cpp_name)
{
// sizeof(X) may be ambiguous -- X can be either a type or
// an expression.
cpp_typecheck_fargst fargs;
exprt symbol_expr=resolve(
to_cpp_name(static_cast<const irept &>(type)),
cpp_typecheck_resolvet::wantt::BOTH,
fargs);
if(symbol_expr.id()!=ID_type)
{
expr.copy_to_operands(symbol_expr);
expr.remove(ID_type_arg);
}
}
else if(type.id()==ID_array)
{
// sizeof(expr[index]) can be parsed as an array type!
if(to_array_type(type).element_type().id() == ID_cpp_name)
{
cpp_typecheck_fargst fargs;
exprt symbol_expr = resolve(
to_cpp_name(
static_cast<const irept &>(to_array_type(type).element_type())),
cpp_typecheck_resolvet::wantt::BOTH,
fargs);
if(symbol_expr.id()!=ID_type)
{
// _NOT_ a type
index_exprt index_expr(symbol_expr, to_array_type(type).size());
expr.copy_to_operands(index_expr);
expr.remove(ID_type_arg);
}
}
}
}
c_typecheck_baset::typecheck_expr_sizeof(expr);
}
void cpp_typecheckt::typecheck_expr_ptrmember(exprt &expr)
{
typecheck_expr_ptrmember(expr, cpp_typecheck_fargst());
}
void cpp_typecheckt::typecheck_function_expr(
exprt &expr,
const cpp_typecheck_fargst &fargs)
{
if(expr.id()==ID_cpp_name)
typecheck_expr_cpp_name(expr, fargs);
else if(expr.id()==ID_member)
{
typecheck_expr_operands(expr);
typecheck_expr_member(expr, fargs);
}
else if(expr.id()==ID_ptrmember)
{
typecheck_expr_operands(expr);
add_implicit_dereference(to_unary_expr(expr).op());
// is operator-> overloaded?
if(to_unary_expr(expr).op().type().id() != ID_pointer)
{
std::string op_name="operator->";
// turn this into a function call
// first do function/operator
const cpp_namet cpp_name(op_name, expr.source_location());
side_effect_expr_function_callt function_call(
cpp_name.as_expr(),
{to_unary_expr(expr).op()},
uninitialized_typet{},
expr.source_location());
function_call.arguments().reserve(expr.operands().size());
typecheck_side_effect_function_call(function_call);
already_typechecked_exprt::make_already_typechecked(function_call);
to_unary_expr(expr).op().swap(function_call);
typecheck_function_expr(expr, fargs);
return;
}
typecheck_expr_ptrmember(expr, fargs);
}
else
typecheck_expr(expr);
}
bool cpp_typecheckt::overloadable(const exprt &expr)
{
// at least one argument must have class or enumerated type
for(const auto &op : expr.operands())
{
typet t = op.type();
if(is_reference(t))
t = to_reference_type(t).base_type();
if(
t.id() == ID_struct || t.id() == ID_union || t.id() == ID_c_enum ||
t.id() == ID_c_enum_tag || t.id() == ID_struct_tag ||
t.id() == ID_union_tag)
{
return true;
}
}
return false;
}
struct operator_entryt
{
const irep_idt id;
const char *op_name;
} const operators[] =
{
{ ID_plus, "+" },
{ ID_minus, "-" },
{ ID_mult, "*" },
{ ID_div, "/" },
{ ID_bitnot, "~" },
{ ID_bitand, "&" },
{ ID_bitor, "|" },
{ ID_bitxor, "^" },
{ ID_not, "!" },
{ ID_unary_minus, "-" },
{ ID_and, "&&" },
{ ID_or, "||" },
{ ID_not, "!" },
{ ID_index, "[]" },
{ ID_equal, "==" },
{ ID_lt, "<"},
{ ID_le, "<="},
{ ID_gt, ">"},
{ ID_ge, ">="},
{ ID_shl, "<<"},
{ ID_shr, ">>"},
{ ID_notequal, "!=" },
{ ID_dereference, "*" },
{ ID_ptrmember, "->" },
{ irep_idt(), nullptr }
};
bool cpp_typecheckt::operator_is_overloaded(exprt &expr)
{
// Check argument types first.
// At least one struct/enum operand is required.
if(!overloadable(expr))
return false;
else if(expr.id()==ID_dereference &&
expr.get_bool(ID_C_implicit))
return false;
PRECONDITION(expr.operands().size() >= 1);
if(expr.id()=="explicit-typecast")
{
// the cast operator can be overloaded
typet t=expr.type();
typecheck_type(t);
std::string op_name=std::string("operator")+"("+cpp_type2name(t)+")";
// turn this into a function call
const cpp_namet cpp_name(op_name, expr.source_location());
// See if the struct declares the cast operator as a member
bool found_in_struct=false;
PRECONDITION(!expr.operands().empty());
const typet &t0 = to_unary_expr(expr).op().type();
if(t0.id() == ID_struct_tag)
{
for(const auto &c : follow_tag(to_struct_tag_type(t0)).components())
{
if(!c.get_bool(ID_from_base) && c.get_base_name() == op_name)
{
found_in_struct=true;
break;
}
}
}
if(!found_in_struct)
return false;
exprt member(ID_member);
member.add(ID_component_cpp_name) = cpp_name;
member.copy_to_operands(
already_typechecked_exprt{to_unary_expr(expr).op()});
side_effect_expr_function_callt function_call(
std::move(member), {}, uninitialized_typet{}, expr.source_location());
function_call.arguments().reserve(expr.operands().size());
if(expr.operands().size()>1)
{
for(exprt::operandst::const_iterator
it=(expr.operands().begin()+1);
it!=(expr).operands().end();
it++)
function_call.arguments().push_back(*it);
}
typecheck_side_effect_function_call(function_call);
if(expr.id()==ID_ptrmember)
{
add_implicit_dereference(function_call);
already_typechecked_exprt::make_already_typechecked(function_call);
to_unary_expr(expr).op().swap(function_call);
typecheck_expr(expr);
return true;
}
expr.swap(function_call);
return true;
}
for(const operator_entryt *e=operators;
!e->id.empty();
e++)
{
if(expr.id()==e->id)
{
DATA_INVARIANT(
expr.id() != ID_dereference || !expr.get_bool(ID_C_implicit),
"no implicit dereference");
std::string op_name=std::string("operator")+e->op_name;
// first do function/operator
const cpp_namet cpp_name(op_name, expr.source_location());
// turn this into a function call
// There are two options to overload an operator:
//
// 1. In the scope of a as a.operator(b, ...)
// 2. Anywhere in scope as operator(a, b, ...)
//
// Using both is not allowed.
//
// We try and fail silently, maybe conversions will work
// instead.
// TODO: need to resolve an incomplete struct (template) here
// go into scope of first operand
if(to_multi_ary_expr(expr).op0().type().id() == ID_struct_tag)
{
const irep_idt &struct_identifier =
to_multi_ary_expr(expr).op0().type().get(ID_identifier);
// get that scope
cpp_save_scopet save_scope(cpp_scopes);
cpp_scopes.set_scope(struct_identifier);
// build fargs for resolver
cpp_typecheck_fargst fargs;
fargs.operands=expr.operands();
fargs.has_object=true;
fargs.in_use=true;
// should really be a qualified search
exprt resolve_result=resolve(
cpp_name, cpp_typecheck_resolvet::wantt::VAR, fargs, false);
if(resolve_result.is_not_nil())
{
// Found! We turn op(a, b, ...) into a.op(b, ...)
exprt member(ID_member);
member.add(ID_component_cpp_name) = cpp_name;
member.copy_to_operands(
already_typechecked_exprt{to_multi_ary_expr(expr).op0()});
side_effect_expr_function_callt function_call(
std::move(member),
{},
uninitialized_typet{},
expr.source_location());
function_call.arguments().reserve(expr.operands().size());
if(expr.operands().size()>1)
{
// skip first
for(exprt::operandst::const_iterator
it=expr.operands().begin()+1;
it!=expr.operands().end();
it++)
function_call.arguments().push_back(*it);
}
typecheck_side_effect_function_call(function_call);
expr=function_call;
return true;
}
}
// 2nd option!
{
cpp_typecheck_fargst fargs;
fargs.operands=expr.operands();
fargs.has_object=false;
fargs.in_use=true;
exprt resolve_result=resolve(
cpp_name, cpp_typecheck_resolvet::wantt::VAR, fargs, false);
if(resolve_result.is_not_nil())
{
// found!
side_effect_expr_function_callt function_call(
cpp_name.as_expr(),
{},
uninitialized_typet{},
expr.source_location());
function_call.arguments().reserve(expr.operands().size());
// now do arguments
for(const auto &op : as_const(expr).operands())
function_call.arguments().push_back(op);
typecheck_side_effect_function_call(function_call);
if(expr.id()==ID_ptrmember)
{
add_implicit_dereference(function_call);
already_typechecked_exprt::make_already_typechecked(function_call);
to_multi_ary_expr(expr).op0() = function_call;
typecheck_expr(expr);
return true;
}
expr=function_call;
return true;
}
}
}
}
return false;
}
void cpp_typecheckt::typecheck_expr_address_of(exprt &expr)
{
if(expr.operands().size()!=1)
{
error().source_location=expr.find_source_location();
error() << "address_of expects one operand" << eom;
throw 0;
}
exprt &op = to_address_of_expr(expr).op();
if(!op.get_bool(ID_C_lvalue) && expr.type().id()==ID_code)
{
error().source_location=expr.source_location();
error() << "expr not an lvalue" << eom;
throw 0;
}
if(op.type().id() == ID_code)
{
// we take the address of the method.
DATA_INVARIANT(op.id() == ID_member, "address-of code must be a member");
exprt symb = cpp_symbol_expr(lookup(op.get(ID_component_name)));
address_of_exprt address(symb, pointer_type(symb.type()));
address.set(ID_C_implicit, true);
op.swap(address);
}
if(op.id() == ID_address_of && op.get_bool(ID_C_implicit))
{
// must be the address of a function
code_typet &code_type =
to_code_type(to_pointer_type(op.type()).base_type());
code_typet::parameterst &args=code_type.parameters();
if(!args.empty() && args.front().get_this())
{
// it's a pointer to member function
const struct_tag_typet symbol(code_type.get(ID_C_member_name));
op.type().add(ID_to_member) = symbol;
if(code_type.get_bool(ID_C_is_virtual))
{
error().source_location=expr.source_location();
error() << "pointers to virtual methods"
<< " are currently not implemented" << eom;
throw 0;
}
}
}
else if(op.id() == ID_ptrmember && to_unary_expr(op).op().id() == "cpp-this")
{
expr.type() = pointer_type(op.type());
expr.type().add(ID_to_member) = to_struct_tag_type(
to_pointer_type(to_unary_expr(op).op().type()).base_type());
return;
}
// the C front end does not know about references
const bool is_ref=is_reference(expr.type());
c_typecheck_baset::typecheck_expr_address_of(expr);
if(is_ref)
expr.type() = reference_type(to_pointer_type(expr.type()).base_type());
}
void cpp_typecheckt::typecheck_expr_throw(exprt &expr)
{
expr.type() = void_type();
PRECONDITION(expr.operands().size() == 1 || expr.operands().empty());
if(expr.operands().size()==1)
{
// nothing really to do; one can throw _almost_ anything
const typet &exception_type = to_unary_expr(expr).op().type();
if(exception_type.id() == ID_empty)
{
error().source_location = to_unary_expr(expr).op().find_source_location();
error() << "cannot throw void" << eom;
throw 0;
}
// annotate the relevant exception IDs
expr.set(ID_exception_list,
cpp_exception_list(exception_type, *this));
}
}
void cpp_typecheckt::typecheck_expr_new(exprt &expr)
{
// next, find out if we do an array
if(expr.type().id()==ID_array)
{
// first typecheck the element type
typecheck_type(to_array_type(expr.type()).element_type());
// typecheck the size
exprt &size=to_array_type(expr.type()).size();
typecheck_expr(size);
bool size_is_unsigned=(size.type().id()==ID_unsignedbv);
bitvector_typet integer_type(
size_is_unsigned ? ID_unsignedbv : ID_signedbv, config.ansi_c.int_width);
implicit_typecast(size, integer_type);
expr.set(ID_statement, ID_cpp_new_array);
// save the size expression
expr.set(ID_size, to_array_type(expr.type()).size());
// new actually returns a pointer, not an array
pointer_typet ptr_type =
pointer_type(to_array_type(expr.type()).element_type());
expr.type().swap(ptr_type);
}
else
{
// first typecheck type
typecheck_type(expr.type());
expr.set(ID_statement, ID_cpp_new);
pointer_typet ptr_type=pointer_type(expr.type());
expr.type().swap(ptr_type);
}
exprt object_expr(ID_new_object, to_pointer_type(expr.type()).base_type());
object_expr.set(ID_C_lvalue, true);
already_typechecked_exprt::make_already_typechecked(object_expr);
// not yet typechecked-stuff
exprt &initializer=static_cast<exprt &>(expr.add(ID_initializer));
// arrays must not have an initializer
if(!initializer.operands().empty() &&
expr.get(ID_statement)==ID_cpp_new_array)
{
error().source_location =
to_multi_ary_expr(expr).op0().find_source_location();
error() << "new with array type must not use initializer" << eom;
throw 0;
}
auto code = cpp_constructor(
expr.find_source_location(), object_expr, initializer.operands());
if(code.has_value())
expr.add(ID_initializer).swap(code.value());
else
expr.add(ID_initializer) = nil_exprt();
// we add the size of the object for convenience of the
// runtime library
auto size_of_opt =
size_of_expr(to_pointer_type(expr.type()).base_type(), *this);
if(size_of_opt.has_value())
{
auto &sizeof_expr = static_cast<exprt &>(expr.add(ID_sizeof));
sizeof_expr = size_of_opt.value();
sizeof_expr.add(ID_C_c_sizeof_type) =
to_pointer_type(expr.type()).base_type();
}
}
static exprt collect_comma_expression(const exprt &src)
{
exprt result;
if(src.id()==ID_comma)
{
PRECONDITION(src.operands().size() == 2);
result = collect_comma_expression(to_binary_expr(src).op0());
result.copy_to_operands(to_binary_expr(src).op1());
}
else
result.copy_to_operands(src);
return result;
}
void cpp_typecheckt::typecheck_expr_explicit_typecast(exprt &expr)
{
// these can have 0 or 1 arguments
if(expr.operands().empty())
{
// Default value, e.g., int()
typecheck_type(expr.type());
auto new_expr =
::zero_initializer(expr.type(), expr.find_source_location(), *this);
if(!new_expr.has_value())
{
error().source_location = expr.find_source_location();
error() << "cannot zero-initialize '" << to_string(expr.type()) << "'"
<< eom;
throw 0;
}
new_expr->add_source_location() = expr.source_location();
expr = *new_expr;
}
else if(expr.operands().size()==1)
{
auto &op = to_unary_expr(expr).op();
// Explicitly given value, e.g., int(1).
// There is an expr-vs-type ambiguity, as it is possible to write
// (f)(1), where 'f' is a function symbol and not a type.
// This also exists with a "comma expression", e.g.,
// (f)(1, 2, 3)
if(expr.type().id()==ID_cpp_name)
{
// try to resolve as type
cpp_typecheck_fargst fargs;
exprt symbol_expr=resolve(
to_cpp_name(static_cast<const irept &>(expr.type())),
cpp_typecheck_resolvet::wantt::TYPE,
fargs,
false); // fail silently
if(symbol_expr.id()==ID_type)
expr.type()=symbol_expr.type();
else
{
// It's really a function call. Note that multiple arguments
// become a comma expression, and that these are already typechecked.
side_effect_expr_function_callt f_call(
static_cast<const exprt &>(static_cast<const irept &>(expr.type())),
collect_comma_expression(op).operands(),
uninitialized_typet{},
expr.source_location());
typecheck_side_effect_function_call(f_call);
expr.swap(f_call);
return;
}
}
else
typecheck_type(expr.type());
// We allow (TYPE){ initializer_list }
// This is called "compound literal", and is syntactic
// sugar for a (possibly local) declaration.
if(op.id() == ID_initializer_list)
{
// just do a normal initialization
do_initializer(op, expr.type(), false);
// This produces a struct-expression,
// union-expression, array-expression,
// or an expression for a pointer or scalar.
// We produce a compound_literal expression.
exprt tmp(ID_compound_literal, expr.type());
tmp.add_to_operands(std::move(op));
expr=tmp;
expr.set(ID_C_lvalue, true); // these are l-values
return;
}
exprt new_expr;
if(
const_typecast(op, expr.type(), new_expr) ||
static_typecast(op, expr.type(), new_expr, false) ||
reinterpret_typecast(op, expr.type(), new_expr, false))
{
expr=new_expr;
add_implicit_dereference(expr);
}
else
{
error().source_location=expr.find_source_location();
error() << "invalid explicit cast:\n"
<< "operand type: '" << to_string(op.type()) << "'\n"
<< "casting to: '" << to_string(expr.type()) << "'" << eom;
throw 0;
}
}
else
{
error().source_location=expr.find_source_location();
error() << "explicit typecast expects 0 or 1 operands" << eom;
throw 0;
}
}
void cpp_typecheckt::typecheck_expr_explicit_constructor_call(exprt &expr)
{
typecheck_type(expr.type());
if(cpp_is_pod(expr.type()))
{
expr.id("explicit-typecast");
typecheck_expr_main(expr);
}
else
{
CHECK_RETURN(expr.type().id() == ID_struct);
struct_tag_typet tag(expr.type().get(ID_name));
tag.add_source_location() = expr.source_location();
exprt e=expr;
new_temporary(e.source_location(), tag, e.operands(), expr);
}
}
void cpp_typecheckt::typecheck_expr_this(exprt &expr)
{
if(cpp_scopes.current_scope().class_identifier.empty())
{
error().source_location=expr.find_source_location();
error() << quote_begin << "this" << quote_end << " is not allowed here"
<< eom;
throw 0;
}
const exprt &this_expr=cpp_scopes.current_scope().this_expr;
const source_locationt source_location=expr.find_source_location();
PRECONDITION(this_expr.is_not_nil());
PRECONDITION(this_expr.type().id() == ID_pointer);
expr=this_expr;
expr.add_source_location()=source_location;
}
void cpp_typecheckt::typecheck_expr_delete(exprt &expr)
{