cprover
cpp_typecheck_expr.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: C++ Language Type Checking
4 
5 Author: Daniel Kroening, kroening@cs.cmu.edu
6 
7 \*******************************************************************/
8 
11 
12 #include "cpp_typecheck.h"
13 
14 #ifdef DEBUG
15 #include <iostream>
16 #endif
17 
18 #include <util/arith_tools.h>
19 #include <util/base_type.h>
20 #include <util/c_types.h>
21 #include <util/config.h>
22 #include <util/expr_initializer.h>
24 
25 #include <ansi-c/c_qualifiers.h>
26 
27 #include "cpp_exception_id.h"
28 #include "cpp_type2name.h"
29 #include "expr2cpp.h"
30 
32  const symbolt &symb,
33  const irep_idt &base_name,
34  irep_idt &identifier)
35 {
36  forall_irep(bit, symb.type.find(ID_bases).get_sub())
37  {
38  if(lookup(bit->find(ID_type).get(ID_identifier)).base_name == base_name)
39  {
40  identifier=bit->find(ID_type).get(ID_identifier);
41  return true;
42  }
43  }
44 
45  return false;
46 }
47 
50 {
51  if(expr.id()==ID_cpp_name)
53  else if(expr.id()=="cpp-this")
54  typecheck_expr_this(expr);
55  else if(expr.id()=="pointer-to-member")
56  convert_pmop(expr);
57  else if(expr.id()=="new_object")
58  {
59  }
60  else if(operator_is_overloaded(expr))
61  {
62  }
63  else if(expr.id()=="explicit-typecast")
65  else if(expr.id()=="explicit-constructor-call")
67  else if(expr.is_nil())
68  {
69 #ifdef DEBUG
70  std::cerr << "E: " << expr.pretty() << '\n';
71  std::cerr << "cpp_typecheckt::typecheck_expr_main got nil\n";
72 #endif
74  }
75  else if(expr.id()==ID_code)
76  {
77 #ifdef DEBUG
78  std::cerr << "E: " << expr.pretty() << '\n';
79  std::cerr << "cpp_typecheckt::typecheck_expr_main got code\n";
80 #endif
82  }
83  else if(expr.id()==ID_symbol)
84  {
85  // ignore here
86 #ifdef DEBUG
87  std::cerr << "E: " << expr.pretty() << '\n';
88  std::cerr << "cpp_typecheckt::typecheck_expr_main got symbol\n";
89 #endif
90  }
91  else if(expr.id()=="__is_base_of")
92  {
93  // an MS extension
94  // http://msdn.microsoft.com/en-us/library/ms177194(v=vs.80).aspx
95 
96  typet base=static_cast<const typet &>(expr.find("type_arg1"));
97  typet deriv=static_cast<const typet &>(expr.find("type_arg2"));
98 
99  typecheck_type(base);
100  typecheck_type(deriv);
101 
102  base = follow(base);
103  deriv = follow(deriv);
104 
105  if(base.id()!=ID_struct || deriv.id()!=ID_struct)
106  expr=false_exprt();
107  else
108  {
109  irep_idt base_name=base.get(ID_name);
110  const class_typet &class_type=to_class_type(deriv);
111 
112  if(class_type.has_base(base_name))
113  expr=true_exprt();
114  else
115  expr=false_exprt();
116  }
117  }
118  else if(expr.id()==ID_msc_uuidof)
119  {
120  // these appear to have type "struct _GUID"
121  // and they are lvalues!
122  expr.type()=symbol_typet("tag-_GUID");
123  follow(expr.type());
124  expr.set(ID_C_lvalue, true);
125  }
126  else if(expr.id()==ID_noexcept)
127  {
128  // TODO
129  expr=false_exprt();
130  }
131  else if(expr.id()==ID_initializer_list)
132  {
133  expr.type().id(ID_initializer_list);
134  }
135  else
137 }
138 
140 {
141  assert(expr.operands().size()==3);
142 
143  implicit_typecast(expr.op0(), bool_typet());
144 
145  if(expr.op1().type().id()==ID_empty ||
146  expr.op1().type().id()==ID_empty)
147  {
148  if(expr.op1().get_bool(ID_C_lvalue))
149  {
150  exprt e1(expr.op1());
152  {
153  error().source_location=e1.find_source_location();
154  error() << "error: lvalue to rvalue conversion" << eom;
155  throw 0;
156  }
157  }
158 
159  if(expr.op1().type().id()==ID_array)
160  {
161  exprt e1(expr.op1());
163  {
164  error().source_location=e1.find_source_location();
165  error() << "error: array to pointer conversion" << eom;
166  throw 0;
167  }
168  }
169 
170  if(expr.op1().type().id()==ID_code)
171  {
172  exprt e1(expr.op1());
174  {
175  error().source_location=e1.find_source_location();
176  error() << "error: function to pointer conversion" << eom;
177  throw 0;
178  }
179  }
180 
181  if(expr.op2().get_bool(ID_C_lvalue))
182  {
183  exprt e2(expr.op2());
185  {
186  error().source_location=e2.find_source_location();
187  error() << "error: lvalue to rvalue conversion" << eom;
188  throw 0;
189  }
190  }
191 
192  if(expr.op2().type().id()==ID_array)
193  {
194  exprt e2(expr.op2());
196  {
197  error().source_location=e2.find_source_location();
198  error() << "error: array to pointer conversion" << eom;
199  throw 0;
200  }
201  }
202 
203  if(expr.op2().type().id()==ID_code)
204  {
205  exprt e2(expr.op2());
207  {
209  error() << "error: function to pointer conversion" << eom;
210  throw 0;
211  }
212  }
213 
214  if(expr.op1().get(ID_statement)==ID_throw &&
215  expr.op2().get(ID_statement)!=ID_throw)
216  expr.type()=expr.op2().type();
217  else if(expr.op2().get(ID_statement)==ID_throw &&
218  expr.op1().get(ID_statement)!=ID_throw)
219  expr.type()=expr.op1().type();
220  else if(expr.op1().type().id()==ID_empty &&
221  expr.op2().type().id()==ID_empty)
222  expr.type()=empty_typet();
223  else
224  {
226  error() << "error: bad types for operands" << eom;
227  throw 0;
228  }
229  return;
230  }
231 
232  if(expr.op1().type() == expr.op2().type())
233  {
234  c_qualifierst qual1, qual2;
235  qual1.read(expr.op1().type());
236  qual2.read(expr.op2().type());
237 
238  if(qual1.is_subset_of(qual2))
239  expr.type()=expr.op1().type();
240  else
241  expr.type()=expr.op2().type();
242  }
243  else
244  {
245  exprt e1=expr.op1();
246  exprt e2=expr.op2();
247 
248  if(implicit_conversion_sequence(expr.op1(), expr.op2().type(), e1))
249  {
250  expr.type()=e1.type();
251  expr.op1().swap(e1);
252  }
253  else if(implicit_conversion_sequence(expr.op2(), expr.op1().type(), e2))
254  {
255  expr.type()=e2.type();
256  expr.op2().swap(e2);
257  }
258  else if(expr.op1().type().id()==ID_array &&
259  expr.op2().type().id()==ID_array &&
260  expr.op1().type().subtype() == expr.op2().type().subtype())
261  {
262  // array-to-pointer conversion
263 
264  index_exprt index1;
265  index1.array()=expr.op1();
266  index1.index()=from_integer(0, index_type());
267  index1.type()=expr.op1().type().subtype();
268 
269  index_exprt index2;
270  index2.array()=expr.op2();
271  index2.index()=from_integer(0, index_type());
272  index2.type()=expr.op2().type().subtype();
273 
274  address_of_exprt addr1(index1);
275  address_of_exprt addr2(index2);
276 
277  expr.op1()=addr1;
278  expr.op2()=addr2;
279  expr.type()=addr1.type();
280  return;
281  }
282  else
283  {
285  error() << "error: types are incompatible.\n"
286  << "I got `" << type2cpp(expr.op1().type(), *this)
287  << "' and `" << type2cpp(expr.op2().type(), *this)
288  << "'." << eom;
289  throw 0;
290  }
291  }
292 
293  if(expr.op1().get_bool(ID_C_lvalue) &&
294  expr.op2().get_bool(ID_C_lvalue))
295  expr.set(ID_C_lvalue, true);
296 
297  return;
298 }
299 
301 {
303  expr,
305 }
306 
308 {
309  // We need to overload, "sizeof-expression" can be mis-parsed
310  // as a type.
311 
312  if(expr.operands().empty())
313  {
314  const typet &type=
315  static_cast<const typet &>(expr.find(ID_type_arg));
316 
317  if(type.id()==ID_cpp_name)
318  {
319  // sizeof(X) may be ambiguous -- X can be either a type or
320  // an expression.
321 
322  cpp_typecheck_fargst fargs;
323 
324  exprt symbol_expr=resolve(
325  to_cpp_name(static_cast<const irept &>(type)),
327  fargs);
328 
329  if(symbol_expr.id()!=ID_type)
330  {
331  expr.copy_to_operands(symbol_expr);
332  expr.remove(ID_type_arg);
333  }
334  }
335  else if(type.id()==ID_array)
336  {
337  // sizeof(expr[index]) can be parsed as an array type!
338 
339  if(type.subtype().id()==ID_cpp_name)
340  {
341  cpp_typecheck_fargst fargs;
342 
343  exprt symbol_expr=resolve(
344  to_cpp_name(static_cast<const irept &>(type.subtype())),
346  fargs);
347 
348  if(symbol_expr.id()!=ID_type)
349  {
350  // _NOT_ a type
351  index_exprt index_expr(symbol_expr, to_array_type(type).size());
352  expr.copy_to_operands(index_expr);
353  expr.remove(ID_type_arg);
354  }
355  }
356  }
357  }
358 
360 }
361 
363 {
365 }
366 
368  exprt &expr,
369  const cpp_typecheck_fargst &fargs)
370 {
371  if(expr.id()==ID_cpp_name)
372  typecheck_expr_cpp_name(expr, fargs);
373  else if(expr.id()==ID_member)
374  {
376  typecheck_expr_member(expr, fargs);
377  }
378  else if(expr.id()==ID_ptrmember)
379  {
382 
383  // is operator-> overloaded?
384  if(expr.op0().type().id() != ID_pointer)
385  {
386  std::string op_name="operator->";
387 
388  // turn this into a function call
389  side_effect_expr_function_callt function_call;
390  function_call.arguments().reserve(expr.operands().size());
391  function_call.add_source_location()=expr.source_location();
392 
393  // first do function/operator
394  cpp_namet cpp_name;
395  cpp_name.get_sub().push_back(irept(ID_name));
396  cpp_name.get_sub().back().set(ID_identifier, op_name);
397  cpp_name.get_sub().back().add(ID_C_source_location)=
398  expr.source_location();
399 
400  function_call.function()=
401  static_cast<const exprt &>(
402  static_cast<const irept &>(cpp_name));
403 
404  // now do the argument
405  function_call.arguments().push_back(expr.op0());
407 
408  exprt tmp("already_typechecked");
409  tmp.copy_to_operands(function_call);
410  function_call.swap(tmp);
411 
412  expr.op0().swap(function_call);
413  typecheck_function_expr(expr, fargs);
414  return;
415  }
416 
417  typecheck_expr_ptrmember(expr, fargs);
418  }
419  else
420  typecheck_expr(expr);
421 }
422 
424 {
425  // at least one argument must have class or enumerated type
426 
427  forall_operands(it, expr)
428  {
429  typet t=follow(it->type());
430 
431  if(is_reference(t))
432  t=t.subtype();
433 
434  if(t.id()==ID_struct ||
435  t.id()==ID_union ||
436  t.id()==ID_c_enum || t.id() == ID_c_enum_tag)
437  return true;
438  }
439 
440  return false;
441 }
442 
444 {
445  const irep_idt id;
446  const char *op_name;
447 } const operators[] =
448 {
449  { ID_plus, "+" },
450  { ID_minus, "-" },
451  { ID_mult, "*" },
452  { ID_div, "/" },
453  { ID_bitnot, "~" },
454  { ID_bitand, "&" },
455  { ID_bitor, "|" },
456  { ID_bitxor, "^" },
457  { ID_not, "!" },
458  { ID_unary_minus, "-" },
459  { ID_and, "&&" },
460  { ID_or, "||" },
461  { ID_not, "!" },
462  { ID_index, "[]" },
463  { ID_equal, "==" },
464  { ID_lt, "<"},
465  { ID_le, "<="},
466  { ID_gt, ">"},
467  { ID_ge, ">="},
468  { ID_shl, "<<"},
469  { ID_shr, ">>"},
470  { ID_notequal, "!=" },
471  { ID_dereference, "*" },
472  { ID_ptrmember, "->" },
473  { irep_idt(), nullptr }
474 };
475 
477 {
478  // Check argument types first.
479  // At least one struct/enum operand is required.
480 
481  if(!overloadable(expr))
482  return false;
483  else if(expr.id()==ID_dereference &&
484  expr.get_bool(ID_C_implicit))
485  return false;
486 
487  assert(expr.operands().size()>=1);
488 
489  if(expr.id()=="explicit-typecast")
490  {
491  // the cast operator can be overloaded
492 
493  typet t=expr.type();
494  typecheck_type(t);
495  std::string op_name=std::string("operator")+"("+cpp_type2name(t)+")";
496 
497  // turn this into a function call
498  side_effect_expr_function_callt function_call;
499  function_call.arguments().reserve(expr.operands().size());
500  function_call.add_source_location()=expr.source_location();
501 
502  cpp_namet cpp_name;
503  cpp_name.get_sub().push_back(irept(ID_name));
504  cpp_name.get_sub().back().set(ID_identifier, op_name);
505  cpp_name.get_sub().back().add(ID_C_source_location)=expr.source_location();
506 
507  // See if the struct declares the cast operator as a member
508  bool found_in_struct=false;
509  assert(!expr.operands().empty());
510  typet t0(follow(expr.op0().type()));
511 
512  if(t0.id()==ID_struct)
513  {
514  const struct_typet &struct_type=
515  to_struct_type(t0);
516 
517  const struct_typet::componentst &components=
518  struct_type.components();
519 
520  for(struct_typet::componentst::const_iterator
521  it=components.begin();
522  it!=components.end();
523  it++)
524  {
525  if(!it->get_bool(ID_from_base) &&
526  it->get(ID_base_name) == op_name)
527  {
528  found_in_struct=true;
529  break;
530  }
531  }
532  }
533 
534  if(!found_in_struct)
535  return false;
536 
537  {
538  exprt member(ID_member);
539  member.add(ID_component_cpp_name)= cpp_name;
540 
541  exprt tmp("already_typechecked");
542  tmp.copy_to_operands(expr.op0());
543  member.copy_to_operands(tmp);
544 
545  function_call.function()=member;
546  }
547 
548  if(expr.operands().size()>1)
549  {
550  for(exprt::operandst::const_iterator
551  it=(expr.operands().begin()+1);
552  it!=(expr).operands().end();
553  it++)
554  function_call.arguments().push_back(*it);
555  }
556 
558 
559  if(expr.id()==ID_ptrmember)
560  {
561  add_implicit_dereference(function_call);
562  exprt tmp("already_typechecked");
563  tmp.move_to_operands(function_call);
564  expr.op0().swap(tmp);
565  typecheck_expr(expr);
566  return true;
567  }
568 
569  expr.swap(function_call);
570  return true;
571  }
572 
573  for(const operator_entryt *e=operators;
574  !e->id.empty();
575  e++)
576  if(expr.id()==e->id)
577  {
578  if(expr.id()==ID_dereference)
579  assert(!expr.get_bool(ID_C_implicit));
580 
581  std::string op_name=std::string("operator")+e->op_name;
582 
583  // first do function/operator
584  cpp_namet cpp_name;
585  cpp_name.get_sub().push_back(irept(ID_name));
586  cpp_name.get_sub().back().set(ID_identifier, op_name);
587  cpp_name.get_sub().back().add(ID_C_source_location)=
588  expr.source_location();
589 
590  // turn this into a function call
591  side_effect_expr_function_callt function_call;
592  function_call.arguments().reserve(expr.operands().size());
593  function_call.add_source_location()=expr.source_location();
594 
595  // There are two options to overload an operator:
596  //
597  // 1. In the scope of a as a.operator(b, ...)
598  // 2. Anywhere in scope as operator(a, b, ...)
599  //
600  // Using both is not allowed.
601  //
602  // We try and fail silently, maybe conversions will work
603  // instead.
604 
605  // go into scope of first operand
606  if(expr.op0().type().id()==ID_symbol &&
607  follow(expr.op0().type()).id()==ID_struct)
608  {
609  const irep_idt &struct_identifier=
610  expr.op0().type().get(ID_identifier);
611 
612  // get that scope
613  cpp_save_scopet save_scope(cpp_scopes);
614  cpp_scopes.set_scope(struct_identifier);
615 
616  // build fargs for resolver
617  cpp_typecheck_fargst fargs;
618  fargs.operands=expr.operands();
619  fargs.has_object=true;
620  fargs.in_use=true;
621 
622  // should really be a qualified search
623  exprt resolve_result=resolve(
624  cpp_name, cpp_typecheck_resolvet::wantt::VAR, fargs, false);
625 
626  if(resolve_result.is_not_nil())
627  {
628  // Found! We turn op(a, b, ...) into a.op(b, ...)
629  {
630  exprt member(ID_member);
631  member.add(ID_component_cpp_name)=cpp_name;
632 
633  exprt tmp("already_typechecked");
634  tmp.copy_to_operands(expr.op0());
635  member.copy_to_operands(tmp);
636 
637  function_call.function()=member;
638  }
639 
640  if(expr.operands().size()>1)
641  {
642  // skip first
643  for(exprt::operandst::const_iterator
644  it=expr.operands().begin()+1;
645  it!=expr.operands().end();
646  it++)
647  function_call.arguments().push_back(*it);
648  }
649 
651 
652  expr=function_call;
653 
654  return true;
655  }
656  }
657 
658  // 2nd option!
659  {
660  cpp_typecheck_fargst fargs;
661  fargs.operands=expr.operands();
662  fargs.has_object=false;
663  fargs.in_use=true;
664 
665  exprt resolve_result=resolve(
666  cpp_name, cpp_typecheck_resolvet::wantt::VAR, fargs, false);
667 
668  if(resolve_result.is_not_nil())
669  {
670  // found!
671  function_call.function()=
672  static_cast<const exprt &>(
673  static_cast<const irept &>(cpp_name));
674 
675  // now do arguments
676  forall_operands(it, expr)
677  function_call.arguments().push_back(*it);
678 
680 
681  if(expr.id()==ID_ptrmember)
682  {
683  add_implicit_dereference(function_call);
684  exprt tmp("already_typechecked");
685  tmp.move_to_operands(function_call);
686  expr.op0()=tmp;
687  typecheck_expr(expr);
688  return true;
689  }
690 
691  expr=function_call;
692 
693  return true;
694  }
695  }
696  }
697 
698  return false;
699 }
700 
702 {
703  if(expr.operands().size()!=1)
704  {
706  error() << "address_of expects one operand" << eom;
707  throw 0;
708  }
709 
710  exprt &op=expr.op0();
711 
712  if(!op.get_bool(ID_C_lvalue) && expr.type().id()==ID_code)
713  {
715  error() << "expr not an lvalue" << eom;
716  throw 0;
717  }
718 
719  if(expr.op0().type().id()==ID_code)
720  {
721  // we take the address of the method.
722  assert(expr.op0().id()==ID_member);
723  exprt symb=cpp_symbol_expr(lookup(expr.op0().get(ID_component_name)));
724  address_of_exprt address(symb, pointer_type(symb.type()));
725  address.set(ID_C_implicit, true);
726  expr.op0().swap(address);
727  }
728 
729  if(expr.op0().id()==ID_address_of &&
730  expr.op0().get_bool(ID_C_implicit))
731  {
732  // must be the address of a function
733  code_typet &code_type=to_code_type(op.type().subtype());
734 
735  code_typet::parameterst &args=code_type.parameters();
736  if(args.size() > 0 && args[0].get(ID_C_base_name)==ID_this)
737  {
738  // it's a pointer to member function
739  const symbol_typet symbol(code_type.get(ID_C_member_name));
740  expr.op0().type().add("to-member")=symbol;
741 
742  if(code_type.get_bool(ID_C_is_virtual))
743  {
745  error() << "error: pointers to virtual methods"
746  << " are currently not implemented" << eom;
747  throw 0;
748  }
749  }
750  }
751  else if(
752  expr.op0().id() == ID_ptrmember && expr.op0().op0().id() == "cpp-this")
753  {
754  expr.type() = pointer_type(expr.op0().type());
755  expr.type().add("to-member") = expr.op0().op0().type().subtype();
756  return;
757  }
758 
759  // the C front end does not know about references
760  const bool is_ref=is_reference(expr.type());
762  if(is_ref)
763  expr.type()=reference_type(expr.type().subtype());
764 }
765 
767 {
768  // these are of type void
769  expr.type()=empty_typet();
770 
771  assert(expr.operands().size()==1 ||
772  expr.operands().empty());
773 
774  if(expr.operands().size()==1)
775  {
776  // nothing really to do; one can throw _almost_ anything
777  const typet &exception_type=expr.op0().type();
778 
779  if(follow(exception_type).id()==ID_empty)
780  {
782  error() << "cannot throw void" << eom;
783  throw 0;
784  }
785 
786  // annotate the relevant exception IDs
787  expr.set(ID_exception_list,
788  cpp_exception_list(exception_type, *this));
789  }
790 }
791 
793 {
794  // next, find out if we do an array
795 
796  if(expr.type().id()==ID_array)
797  {
798  // first typecheck subtype
799  typecheck_type(expr.type().subtype());
800 
801  // typecheck the size
802  exprt &size=to_array_type(expr.type()).size();
803  typecheck_expr(size);
804 
805  bool size_is_unsigned=(size.type().id()==ID_unsignedbv);
806  typet integer_type(size_is_unsigned?ID_unsignedbv:ID_signedbv);
807  integer_type.set(ID_width, config.ansi_c.int_width);
808  implicit_typecast(size, integer_type);
809 
810  expr.set(ID_statement, ID_cpp_new_array);
811 
812  // save the size expression
813  expr.set(ID_size, to_array_type(expr.type()).size());
814 
815  // new actually returns a pointer, not an array
816  pointer_typet ptr_type=
817  pointer_type(expr.type().subtype());
818  expr.type().swap(ptr_type);
819  }
820  else
821  {
822  // first typecheck type
823  typecheck_type(expr.type());
824 
825  expr.set(ID_statement, ID_cpp_new);
826 
827  pointer_typet ptr_type=pointer_type(expr.type());
828  expr.type().swap(ptr_type);
829  }
830 
831  exprt object_expr("new_object", expr.type().subtype());
832  object_expr.set(ID_C_lvalue, true);
833 
834  {
835  exprt tmp("already_typechecked");
836  tmp.move_to_operands(object_expr);
837  object_expr.swap(tmp);
838  }
839 
840  // not yet typechecked-stuff
841  exprt &initializer=static_cast<exprt &>(expr.add(ID_initializer));
842 
843  // arrays must not have an initializer
844  if(!initializer.operands().empty() &&
845  expr.get(ID_statement)==ID_cpp_new_array)
846  {
848  error() << "new with array type must not use initializer" << eom;
849  throw 0;
850  }
851 
852  exprt code=
854  expr.find_source_location(),
855  object_expr,
856  initializer.operands());
857 
858  expr.add(ID_initializer).swap(code);
859 
860  // we add the size of the object for convenience of the
861  // runtime library
862 
863  exprt &sizeof_expr=static_cast<exprt &>(expr.add(ID_sizeof));
864  sizeof_expr=size_of_expr(expr.type().subtype(), *this);
865  sizeof_expr.add(ID_C_c_sizeof_type)=expr.type().subtype();
866 }
867 
869 {
870  exprt result;
871 
872  if(src.id()==ID_comma)
873  {
874  assert(src.operands().size()==2);
875  result=collect_comma_expression(src.op0());
876  result.copy_to_operands(src.op1());
877  }
878  else
879  result.copy_to_operands(src);
880 
881  return result;
882 }
883 
885 {
886  // these can have 0 or 1 arguments
887 
888  if(expr.operands().empty())
889  {
890  // Default value, e.g., int()
891  typecheck_type(expr.type());
892  exprt new_expr=
894  expr.type(),
895  expr.find_source_location(),
896  *this,
898 
899  new_expr.add_source_location()=expr.source_location();
900  expr=new_expr;
901  }
902  else if(expr.operands().size()==1)
903  {
904  // Explicitly given value, e.g., int(1).
905  // There is an expr-vs-type ambiguity, as it is possible to write
906  // (f)(1), where 'f' is a function symbol and not a type.
907  // This also exists with a "comma expression", e.g.,
908  // (f)(1, 2, 3)
909 
910  if(expr.type().id()==ID_cpp_name)
911  {
912  // try to resolve as type
913  cpp_typecheck_fargst fargs;
914 
915  exprt symbol_expr=resolve(
916  to_cpp_name(static_cast<const irept &>(expr.type())),
918  fargs,
919  false); // fail silently
920 
921  if(symbol_expr.id()==ID_type)
922  expr.type()=symbol_expr.type();
923  else
924  {
925  // It's really a function call. Note that multiple arguments
926  // become a comma expression, and that these are already typechecked.
928 
929  f_call.add_source_location()=expr.source_location();
930  f_call.function().swap(expr.type());
931  f_call.arguments()=collect_comma_expression(expr.op0()).operands();
932 
934 
935  expr.swap(f_call);
936  return;
937  }
938  }
939  else
940  typecheck_type(expr.type());
941 
942  // We allow (TYPE){ initializer_list }
943  // This is called "compound literal", and is syntactic
944  // sugar for a (possibly local) declaration.
945  if(expr.op0().id()==ID_initializer_list)
946  {
947  // just do a normal initialization
948  do_initializer(expr.op0(), expr.type(), false);
949 
950  // This produces a struct-expression,
951  // union-expression, array-expression,
952  // or an expression for a pointer or scalar.
953  // We produce a compound_literal expression.
954  exprt tmp(ID_compound_literal, expr.type());
955  tmp.move_to_operands(expr.op0());
956  expr=tmp;
957  expr.set(ID_C_lvalue, true); // these are l-values
958  return;
959  }
960 
961  exprt new_expr;
962 
963  if(const_typecast(expr.op0(), expr.type(), new_expr) ||
964  static_typecast(expr.op0(), expr.type(), new_expr, false) ||
965  reinterpret_typecast(expr.op0(), expr.type(), new_expr, false))
966  {
967  expr=new_expr;
969  }
970  else
971  {
973  error() << "invalid explicit cast:\n"
974  << "operand type: `" << to_string(expr.op0().type())
975  << "'\n"
976  << "casting to: `" << to_string(expr.type()) << "'"
977  << eom;
978  throw 0;
979  }
980  }
981  else
982  {
984  error() << "explicit typecast expects 0 or 1 operands" << eom;
985  throw 0;
986  }
987 }
988 
990 {
991  typecheck_type(expr.type());
992 
993  if(cpp_is_pod(expr.type()))
994  {
995  expr.id("explicit-typecast");
996  typecheck_expr_main(expr);
997  }
998  else
999  {
1000  assert(expr.type().id()==ID_struct);
1001 
1002  symbol_typet symb(expr.type().get(ID_name));
1003  symb.add_source_location()=expr.source_location();
1004 
1005  exprt e=expr;
1006  new_temporary(e.source_location(), symb, e.operands(), expr);
1007  }
1008 }
1009 
1011 {
1013  {
1015  error() << "`this' is not allowed here" << eom;
1016  throw 0;
1017  }
1018 
1019  const exprt &this_expr=cpp_scopes.current_scope().this_expr;
1020  const source_locationt source_location=expr.find_source_location();
1021 
1022  assert(this_expr.is_not_nil());
1023  assert(this_expr.type().id()==ID_pointer);
1024 
1025  expr=this_expr;
1026  expr.add_source_location()=source_location;
1027 }
1028 
1030 {
1031  if(expr.operands().size()!=1)
1032  {
1034  error() << "delete expects one operand" << eom;
1035  throw 0;
1036  }
1037 
1038  const irep_idt statement=expr.get(ID_statement);
1039 
1040  if(statement==ID_cpp_delete)
1041  {
1042  }
1043  else if(statement==ID_cpp_delete_array)
1044  {
1045  }
1046  else
1047  UNREACHABLE;
1048 
1049  typet pointer_type=follow(expr.op0().type());
1050 
1051  if(pointer_type.id()!=ID_pointer)
1052  {
1054  error() << "delete takes a pointer type operand, but got `"
1055  << to_string(pointer_type) << "'" << eom;
1056  throw 0;
1057  }
1058 
1059  // remove any const-ness of the argument
1060  // (which would impair the call to the destructor)
1061  pointer_type.subtype().remove(ID_C_constant);
1062 
1063  // delete expressions are always void
1064  expr.type()=typet(ID_empty);
1065 
1066  // we provide the right destructor, for the convenience
1067  // of later stages
1068  exprt new_object(ID_new_object, pointer_type.subtype());
1069  new_object.add_source_location()=expr.source_location();
1070  new_object.set(ID_C_lvalue, true);
1071 
1072  already_typechecked(new_object);
1073 
1074  codet destructor_code=cpp_destructor(
1075  expr.source_location(),
1077  new_object);
1078 
1079  // this isn't typechecked yet
1080  if(destructor_code.is_not_nil())
1081  typecheck_code(destructor_code);
1082 
1083  expr.set(ID_destructor, destructor_code);
1084 }
1085 
1087 {
1088  // should not be called
1089  #if 0
1090  std::cout << "E: " << expr.pretty() << '\n';
1091  UNREACHABLE;
1092  #endif
1093 }
1094 
1096  exprt &expr,
1097  const cpp_typecheck_fargst &fargs)
1098 {
1099  if(expr.operands().size()!=1)
1100  {
1102  error() << "error: member operator expects one operand" << eom;
1103  throw 0;
1104  }
1105 
1106  exprt &op0=expr.op0();
1108 
1109  // The notation for explicit calls to destructors can be used regardless
1110  // of whether the type defines a destructor. This allows you to make such
1111  // explicit calls without knowing if a destructor is defined for the type.
1112  // An explicit call to a destructor where none is defined has no effect.
1113 
1114  if(expr.find(ID_component_cpp_name).is_not_nil() &&
1115  to_cpp_name(expr.find(ID_component_cpp_name)).is_destructor() &&
1116  follow(op0.type()).id()!=ID_struct)
1117  {
1118  exprt tmp("cpp_dummy_destructor");
1119  tmp.add_source_location()=expr.source_location();
1120  expr.swap(tmp);
1121  return;
1122  }
1123 
1124  // The member operator will trigger template elaboration
1126 
1127  const typet &followed_op0_type=follow(op0.type());
1128 
1129  if(followed_op0_type.id()==ID_incomplete_struct ||
1130  followed_op0_type.id()==ID_incomplete_union)
1131  {
1133  error() << "error: member operator got incomplete type "
1134  << "on left hand side" << eom;
1135  throw 0;
1136  }
1137 
1138  if(followed_op0_type.id()!=ID_struct &&
1139  followed_op0_type.id()!=ID_union)
1140  {
1142  error() << "error: member operator requires struct/union type "
1143  << "on left hand side but got `"
1144  << to_string(followed_op0_type) << "'" << eom;
1145  throw 0;
1146  }
1147 
1148  const struct_union_typet &type=
1149  to_struct_union_type(followed_op0_type);
1150 
1151  irep_idt struct_identifier=type.get(ID_name);
1152 
1153  if(expr.find(ID_component_cpp_name).is_not_nil())
1154  {
1155  cpp_namet component_cpp_name=
1156  to_cpp_name(expr.find(ID_component_cpp_name));
1157 
1158  // go to the scope of the struct/union
1159  cpp_save_scopet save_scope(cpp_scopes);
1160  cpp_scopes.set_scope(struct_identifier);
1161 
1162  // resolve the member name in this scope
1163  cpp_typecheck_fargst new_fargs(fargs);
1164  new_fargs.add_object(op0);
1165 
1166  exprt symbol_expr=resolve(
1167  component_cpp_name,
1169  new_fargs);
1170 
1171  if(symbol_expr.id()==ID_dereference)
1172  {
1173  assert(symbol_expr.get_bool(ID_C_implicit));
1174  exprt tmp=symbol_expr.op0();
1175  symbol_expr.swap(tmp);
1176  }
1177 
1178  assert(symbol_expr.id()==ID_symbol ||
1179  symbol_expr.id()==ID_member ||
1180  symbol_expr.id()==ID_constant);
1181 
1182  // If it is a symbol or a constant, just return it!
1183  // Note: the resolver returns a symbol if the member
1184  // is static or if it is a constructor.
1185 
1186  if(symbol_expr.id()==ID_symbol)
1187  {
1188  if(symbol_expr.type().id()==ID_code &&
1189  symbol_expr.type().get(ID_return_type)==ID_constructor)
1190  {
1192  error() << "error: member `"
1193  << lookup(symbol_expr.get(ID_identifier)).base_name
1194  << "' is a constructor" << eom;
1195  throw 0;
1196  }
1197  else
1198  {
1199  // it must be a static component
1200  const struct_typet::componentt pcomp=
1201  type.get_component(to_symbol_expr(symbol_expr).get_identifier());
1202 
1203  if(pcomp.is_nil())
1204  {
1206  error() << "error: `"
1207  << symbol_expr.get(ID_identifier)
1208  << "' is not static member "
1209  << "of class `" << to_string(type) << "'"
1210  << eom;
1211  throw 0;
1212  }
1213  }
1214 
1215  expr=symbol_expr;
1216  return;
1217  }
1218  else if(symbol_expr.id()==ID_constant)
1219  {
1220  expr=symbol_expr;
1221  return;
1222  }
1223 
1224  const irep_idt component_name=symbol_expr.get(ID_component_name);
1225 
1226  expr.remove(ID_component_cpp_name);
1227  expr.set(ID_component_name, component_name);
1228  }
1229 
1230  const irep_idt &component_name=expr.get(ID_component_name);
1231 
1232  assert(component_name!="");
1233 
1234  exprt component;
1235  component.make_nil();
1236 
1237  assert(follow(expr.op0().type()).id()==ID_struct ||
1238  follow(expr.op0().type()).id()==ID_union);
1239 
1240  exprt member;
1241 
1242  if(get_component(expr.source_location(),
1243  expr.op0(),
1244  component_name,
1245  member))
1246  {
1247  // because of possible anonymous members
1248  expr.swap(member);
1249  }
1250  else
1251  {
1253  error() << "error: member `" << component_name
1254  << "' of `" << to_string(type)
1255  << "' not found" << eom;
1256  throw 0;
1257  }
1258 
1260 
1261  if(expr.type().id()==ID_code)
1262  {
1263  // Check if the function body has to be typechecked
1264  symbol_tablet::symbolst::const_iterator it=
1265  symbol_table.symbols.find(component_name);
1266 
1267  assert(it!=symbol_table.symbols.end());
1268 
1269  if(it->second.value.id()=="cpp_not_typechecked")
1270  symbol_table.get_writeable_ref(component_name).value.set("is_used", true);
1271  }
1272 }
1273 
1275  exprt &expr,
1276  const cpp_typecheck_fargst &fargs)
1277 {
1278  assert(expr.id()==ID_ptrmember);
1279 
1280  if(expr.operands().size()!=1)
1281  {
1283  error() << "error: ptrmember operator expects one operand" << eom;
1284  throw 0;
1285  }
1286 
1287  add_implicit_dereference(expr.op0());
1288 
1289  if(expr.op0().type().id()!=ID_pointer)
1290  {
1292  error() << "error: ptrmember operator requires pointer type "
1293  << "on left hand side, but got `"
1294  << to_string(expr.op0().type()) << "'" << eom;
1295  throw 0;
1296  }
1297 
1298  exprt tmp;
1299  exprt &op=expr.op0();
1300 
1301  op.swap(tmp);
1302 
1303  op.id(ID_dereference);
1304  op.move_to_operands(tmp);
1307 
1308  expr.id(ID_member);
1309  typecheck_expr_member(expr, fargs);
1310 }
1311 
1313 {
1316 
1317  if(e.arguments().size() != 1)
1318  {
1320  error() << "cast expressions expect one operand" << eom;
1321  throw 0;
1322  }
1323 
1324  exprt &f_op=e.function();
1325  exprt &cast_op=e.arguments().front();
1326 
1327  add_implicit_dereference(cast_op);
1328 
1329  const irep_idt &id=
1330  f_op.get_sub().front().get(ID_identifier);
1331 
1332  if(f_op.get_sub().size()!=2 ||
1333  f_op.get_sub()[1].id()!=ID_template_args)
1334  {
1336  error() << id << " expects template argument" << eom;
1337  throw 0;
1338  }
1339 
1340  irept &template_arguments=f_op.get_sub()[1].add(ID_arguments);
1341 
1342  if(template_arguments.get_sub().size()!=1)
1343  {
1345  error() << id << " expects one template argument" << eom;
1346  throw 0;
1347  }
1348 
1349  irept &template_arg=template_arguments.get_sub().front();
1350 
1351  if(template_arg.id()!=ID_type &&
1352  template_arg.id()!="ambiguous")
1353  {
1355  error() << id << " expects a type as template argument" << eom;
1356  throw 0;
1357  }
1358 
1359  typet &type=static_cast<typet &>(
1360  template_arguments.get_sub().front().add(ID_type));
1361 
1362  typecheck_type(type);
1363 
1364  source_locationt source_location=expr.source_location();
1365 
1366  exprt new_expr;
1367  if(id==ID_const_cast)
1368  {
1369  if(!const_typecast(cast_op, type, new_expr))
1370  {
1372  error() << "type mismatch on const_cast:\n"
1373  << "operand type: `" << to_string(cast_op.type())
1374  << "'\n"
1375  << "cast type: `" << to_string(type) << "'" << eom;
1376  throw 0;
1377  }
1378  }
1379  else if(id==ID_dynamic_cast)
1380  {
1381  if(!dynamic_typecast(cast_op, type, new_expr))
1382  {
1384  error() << "type mismatch on dynamic_cast:\n"
1385  << "operand type: `" << to_string(cast_op.type())
1386  << "'\n"
1387  << "cast type: `" << to_string(type) << "'" << eom;
1388  throw 0;
1389  }
1390  }
1391  else if(id==ID_reinterpret_cast)
1392  {
1393  if(!reinterpret_typecast(cast_op, type, new_expr))
1394  {
1396  error() << "type mismatch on reinterpret_cast:\n"
1397  << "operand type: `" << to_string(cast_op.type())
1398  << "'\n"
1399  << "cast type: `" << to_string(type) << "'" << eom;
1400  throw 0;
1401  }
1402  }
1403  else if(id==ID_static_cast)
1404  {
1405  if(!static_typecast(cast_op, type, new_expr))
1406  {
1408  error() << "type mismatch on static_cast:\n"
1409  << "operand type: `" << to_string(cast_op.type())
1410  << "'\n"
1411  << "cast type: `" << to_string(type) << "'" << eom;
1412  throw 0;
1413  }
1414  }
1415  else
1416  UNREACHABLE;
1417 
1418  expr.swap(new_expr);
1419 }
1420 
1422  exprt &expr,
1423  const cpp_typecheck_fargst &fargs)
1424 {
1425  source_locationt source_location=
1426  to_cpp_name(expr).source_location();
1427 
1428  if(expr.get_sub().size()==1 &&
1429  expr.get_sub()[0].id()==ID_name)
1430  {
1431  const irep_idt identifier=expr.get_sub()[0].get(ID_identifier);
1432 
1433  if(identifier=="__sync_fetch_and_add" ||
1434  identifier=="__sync_fetch_and_sub" ||
1435  identifier=="__sync_fetch_and_or" ||
1436  identifier=="__sync_fetch_and_and" ||
1437  identifier=="__sync_fetch_and_xor" ||
1438  identifier=="__sync_fetch_and_nand" ||
1439  identifier=="__sync_add_and_fetch" ||
1440  identifier=="__sync_sub_and_fetch" ||
1441  identifier=="__sync_or_and_fetch" ||
1442  identifier=="__sync_and_and_fetch" ||
1443  identifier=="__sync_xor_and_fetch" ||
1444  identifier=="__sync_nand_and_fetch" ||
1445  identifier=="__sync_val_compare_and_swap" ||
1446  identifier=="__sync_lock_test_and_set" ||
1447  identifier=="__sync_lock_release")
1448  {
1449  // These are polymorphic, see
1450  // http://gcc.gnu.org/onlinedocs/gcc-4.1.1/gcc/Atomic-Builtins.html
1451 
1452  // adjust return type of function to match pointer subtype
1453  if(fargs.operands.empty())
1454  {
1455  error().source_location=source_location;
1456  error() << "__sync_* primitives take as least one argument"
1457  << eom;
1458  throw 0;
1459  }
1460 
1461  const exprt &ptr_arg=fargs.operands.front();
1462 
1463  if(ptr_arg.type().id()!=ID_pointer)
1464  {
1465  error().source_location=source_location;
1466  error() << "__sync_* primitives take a pointer as first argument"
1467  << eom;
1468  throw 0;
1469  }
1470 
1472  result.add_source_location()=source_location;
1473  result.set_identifier(identifier);
1474  code_typet t(
1475  {code_typet::parametert(ptr_arg.type())}, ptr_arg.type().subtype());
1476  t.make_ellipsis();
1477  result.type()=t;
1478  expr.swap(result);
1479  return;
1480  }
1481  else if(identifier=="__atomic_load_n")
1482  {
1483  // These are polymorphic
1484  // https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html
1485  // type __atomic_load_n(type *ptr, int memorder)
1486 
1487  if(fargs.operands.size()!=2)
1488  {
1489  error().source_location=source_location;
1490  error() << identifier << " expects two arguments" << eom;
1491  throw 0;
1492  }
1493 
1494  const exprt &ptr_arg=fargs.operands.front();
1495 
1496  if(ptr_arg.type().id()!=ID_pointer)
1497  {
1498  error().source_location=source_location;
1499  error() << identifier << " takes a pointer as first argument"
1500  << eom;
1501  throw 0;
1502  }
1503 
1505  result.add_source_location()=source_location;
1506  result.set_identifier(identifier);
1507  const code_typet t(
1508  {code_typet::parametert(ptr_arg.type()),
1510  ptr_arg.type().subtype());
1511  result.type()=t;
1512  expr.swap(result);
1513  return;
1514  }
1515  else if(identifier=="__atomic_store_n")
1516  {
1517  // These are polymorphic
1518  // https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html
1519  // void __atomic_store_n(type *ptr, type val, int memorder)
1520 
1521  if(fargs.operands.size()!=3)
1522  {
1523  error().source_location=source_location;
1524  error() << identifier << " expects three arguments" << eom;
1525  throw 0;
1526  }
1527 
1528  const exprt &ptr_arg=fargs.operands.front();
1529 
1530  if(ptr_arg.type().id()!=ID_pointer)
1531  {
1532  error().source_location=source_location;
1533  error() << identifier << " takes a pointer as first argument"
1534  << eom;
1535  throw 0;
1536  }
1537 
1539  result.add_source_location()=source_location;
1540  result.set_identifier(identifier);
1541  const code_typet t(
1542  {code_typet::parametert(ptr_arg.type()),
1543  code_typet::parametert(ptr_arg.type().subtype()),
1545  empty_typet());
1546  result.type()=t;
1547  expr.swap(result);
1548  return;
1549  }
1550  else if(identifier=="__atomic_exchange_n")
1551  {
1552  // These are polymorphic
1553  // https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html
1554  // type __atomic_exchange_n(type *ptr, type val, int memorder)
1555 
1556  if(fargs.operands.size()!=3)
1557  {
1558  error().source_location=source_location;
1559  error() << identifier << " expects three arguments" << eom;
1560  throw 0;
1561  }
1562 
1563  const exprt &ptr_arg=fargs.operands.front();
1564 
1565  if(ptr_arg.type().id()!=ID_pointer)
1566  {
1567  error().source_location=source_location;
1568  error() << identifier << " takes a pointer as first argument"
1569  << eom;
1570  throw 0;
1571  }
1572 
1574  result.add_source_location()=source_location;
1575  result.set_identifier(identifier);
1576  const code_typet t(
1577  {code_typet::parametert(ptr_arg.type()),
1578  code_typet::parametert(ptr_arg.type().subtype()),
1580  ptr_arg.type().subtype());
1581  result.type()=t;
1582  expr.swap(result);
1583  return;
1584  }
1585  else if(identifier=="__atomic_load" ||
1586  identifier=="__atomic_store")
1587  {
1588  // void __atomic_load(type *ptr, type *ret, int memorder)
1589  // void __atomic_store(type *ptr, type *val, int memorder)
1590 
1591  if(fargs.operands.size()!=3)
1592  {
1593  error().source_location=source_location;
1594  error() << identifier << " expects three arguments" << eom;
1595  throw 0;
1596  }
1597 
1598  if(fargs.operands[0].type().id()!=ID_pointer)
1599  {
1600  error().source_location=source_location;
1601  error() << identifier << " takes a pointer as first argument"
1602  << eom;
1603  throw 0;
1604  }
1605 
1606  if(fargs.operands[1].type().id()!=ID_pointer)
1607  {
1608  error().source_location=source_location;
1609  error() << identifier << " takes a pointer as second argument"
1610  << eom;
1611  throw 0;
1612  }
1613 
1614  const exprt &ptr_arg=fargs.operands.front();
1615 
1617  result.add_source_location()=source_location;
1618  result.set_identifier(identifier);
1619  const code_typet t(
1620  {code_typet::parametert(ptr_arg.type()),
1621  code_typet::parametert(ptr_arg.type()),
1623  empty_typet());
1624  result.type()=t;
1625  expr.swap(result);
1626  return;
1627  }
1628  else if(identifier=="__atomic_exchange")
1629  {
1630  // void __atomic_exchange(type *ptr, type *val, type *ret, int memorder)
1631 
1632  if(fargs.operands.size()!=4)
1633  {
1634  error().source_location=source_location;
1635  error() << identifier << " expects four arguments" << eom;
1636  throw 0;
1637  }
1638 
1639  if(fargs.operands[0].type().id()!=ID_pointer)
1640  {
1641  error().source_location=source_location;
1642  error() << identifier << " takes a pointer as first argument"
1643  << eom;
1644  throw 0;
1645  }
1646 
1647  if(fargs.operands[1].type().id()!=ID_pointer)
1648  {
1649  error().source_location=source_location;
1650  error() << identifier << " takes a pointer as second argument"
1651  << eom;
1652  throw 0;
1653  }
1654 
1655  if(fargs.operands[2].type().id()!=ID_pointer)
1656  {
1657  error().source_location=source_location;
1658  error() << identifier << " takes a pointer as third argument"
1659  << eom;
1660  throw 0;
1661  }
1662 
1663  const exprt &ptr_arg=fargs.operands.front();
1664 
1666  result.add_source_location()=source_location;
1667  result.set_identifier(identifier);
1668  const code_typet t(
1669  {code_typet::parametert(ptr_arg.type()),
1670  code_typet::parametert(ptr_arg.type()),
1671  code_typet::parametert(ptr_arg.type()),
1673  empty_typet());
1674  result.type()=t;
1675  expr.swap(result);
1676  return;
1677  }
1678  else if(identifier=="__atomic_compare_exchange_n" ||
1679  identifier=="__atomic_compare_exchange")
1680  {
1681  // bool __atomic_compare_exchange_n(type *ptr, type *expected, type
1682  // desired, bool weak, int success_memorder, int failure_memorder)
1683  // bool __atomic_compare_exchange(type *ptr, type *expected, type
1684  // *desired, bool weak, int success_memorder, int failure_memorder)
1685 
1686  if(fargs.operands.size()!=6)
1687  {
1688  error().source_location=source_location;
1689  error() << identifier << " expects six arguments" << eom;
1690  throw 0;
1691  }
1692 
1693  if(fargs.operands[0].type().id()!=ID_pointer)
1694  {
1695  error().source_location=source_location;
1696  error() << identifier << " takes a pointer as first argument"
1697  << eom;
1698  throw 0;
1699  }
1700 
1701  if(fargs.operands[1].type().id()!=ID_pointer)
1702  {
1703  error().source_location=source_location;
1704  error() << identifier << " takes a pointer as second argument"
1705  << eom;
1706  throw 0;
1707  }
1708 
1709  if(identifier=="__atomic_compare_exchange" &&
1710  fargs.operands[2].type().id()!=ID_pointer)
1711  {
1712  error().source_location=source_location;
1713  error() << identifier << " takes a pointer as third argument"
1714  << eom;
1715  throw 0;
1716  }
1717 
1718  const exprt &ptr_arg=fargs.operands.front();
1719 
1721  result.add_source_location()=source_location;
1722  result.set_identifier(identifier);
1723  code_typet::parameterst parameters;
1724  parameters.push_back(code_typet::parametert(ptr_arg.type()));
1725  parameters.push_back(code_typet::parametert(ptr_arg.type()));
1726 
1727  if(identifier=="__atomic_compare_exchange")
1728  parameters.push_back(code_typet::parametert(ptr_arg.type()));
1729  else
1730  parameters.push_back(code_typet::parametert(ptr_arg.type().subtype()));
1731 
1732  parameters.push_back(code_typet::parametert(c_bool_type()));
1733  parameters.push_back(code_typet::parametert(signed_int_type()));
1734  parameters.push_back(code_typet::parametert(signed_int_type()));
1735  code_typet t(std::move(parameters), c_bool_type());
1736  result.type()=t;
1737  expr.swap(result);
1738  return;
1739  }
1740  else if(identifier=="__atomic_add_fetch" ||
1741  identifier=="__atomic_sub_fetch" ||
1742  identifier=="__atomic_and_fetch" ||
1743  identifier=="__atomic_xor_fetch" ||
1744  identifier=="__atomic_or_fetch" ||
1745  identifier=="__atomic_nand_fetch")
1746  {
1747  if(fargs.operands.size()!=3)
1748  {
1749  error().source_location=source_location;
1750  error() << "__atomic_*_fetch primitives take three arguments"
1751  << eom;
1752  throw 0;
1753  }
1754 
1755  const exprt &ptr_arg=fargs.operands.front();
1756 
1757  if(ptr_arg.type().id()!=ID_pointer)
1758  {
1759  error().source_location=source_location;
1760  error() << "__atomic_*_fetch primitives take pointer as first argument"
1761  << eom;
1762  throw 0;
1763  }
1764 
1766  result.add_source_location()=source_location;
1767  result.set_identifier(identifier);
1768  code_typet t(
1769  {code_typet::parametert(ptr_arg.type())}, ptr_arg.type().subtype());
1770  t.make_ellipsis();
1771  result.type()=t;
1772  expr.swap(result);
1773  return;
1774  }
1775  else if(identifier=="__atomic_fetch_add" ||
1776  identifier=="__atomic_fetch_sub" ||
1777  identifier=="__atomic_fetch_and" ||
1778  identifier=="__atomic_fetch_xor" ||
1779  identifier=="__atomic_fetch_or" ||
1780  identifier=="__atomic_fetch_nand")
1781  {
1782  if(fargs.operands.size()!=3)
1783  {
1784  error().source_location=source_location;
1785  error() << "__atomic_fetch_* primitives take three arguments"
1786  << eom;
1787  throw 0;
1788  }
1789 
1790  const exprt &ptr_arg=fargs.operands.front();
1791 
1792  if(ptr_arg.type().id()!=ID_pointer)
1793  {
1794  error().source_location=source_location;
1795  error() << "__atomic_fetch_* primitives take pointer as first argument"
1796  << eom;
1797  throw 0;
1798  }
1799 
1801  result.add_source_location()=source_location;
1802  result.set_identifier(identifier);
1803  code_typet t(
1804  {code_typet::parametert(ptr_arg.type())}, ptr_arg.type().subtype());
1805  t.make_ellipsis();
1806  result.type()=t;
1807  expr.swap(result);
1808  return;
1809  }
1810  else if(identifier=="__atomic_test_and_set")
1811  {
1812  }
1813  else if(identifier=="__atomic_clear")
1814  {
1815  }
1816  else if(identifier=="__atomic_thread_fence")
1817  {
1818  }
1819  else if(identifier=="__atomic_signal_fence")
1820  {
1821  }
1822  else if(identifier=="__atomic_always_lock_free")
1823  {
1824  }
1825  else if(identifier=="__atomic_is_lock_free")
1826  {
1827  }
1828  }
1829 
1830  for(std::size_t i=0; i<expr.get_sub().size(); i++)
1831  {
1832  if(expr.get_sub()[i].id()==ID_cpp_name)
1833  {
1834  typet &type=static_cast<typet &>(expr.get_sub()[i]);
1835  typecheck_type(type);
1836 
1837  std::string tmp="("+cpp_type2name(type)+")";
1838 
1839  typet name(ID_name);
1840  name.set(ID_identifier, tmp);
1841  name.add_source_location()=source_location;
1842 
1843  type=name;
1844  }
1845  }
1846 
1847  if(expr.get_sub().size()>=1 &&
1848  expr.get_sub().front().id()==ID_name)
1849  {
1850  const irep_idt &id=expr.get_sub().front().get(ID_identifier);
1851 
1852  if(id==ID_const_cast ||
1853  id==ID_dynamic_cast ||
1854  id==ID_reinterpret_cast ||
1855  id==ID_static_cast)
1856  {
1857  expr.id("cast_expression");
1858  return;
1859  }
1860  }
1861 
1862  exprt symbol_expr=
1863  resolve(
1864  to_cpp_name(expr),
1866  fargs);
1867 
1868  // we want VAR
1869  assert(symbol_expr.id()!=ID_type);
1870 
1871  if(symbol_expr.id()==ID_member)
1872  {
1873  if(symbol_expr.operands().empty() ||
1874  symbol_expr.op0().is_nil())
1875  {
1876  if(symbol_expr.type().get(ID_return_type)!=ID_constructor)
1877  {
1879  {
1880  if(symbol_expr.type().id()!=ID_code)
1881  {
1882  error().source_location=source_location;
1883  error() << "object missing" << eom;
1884  throw 0;
1885  }
1886 
1887  // may still be good for address of
1888  }
1889  else
1890  {
1891  // Try again
1892  exprt ptrmem(ID_ptrmember);
1893  ptrmem.operands().push_back(
1895 
1896  ptrmem.add(ID_component_cpp_name)=expr;
1897 
1898  ptrmem.add_source_location()=source_location;
1899  typecheck_expr_ptrmember(ptrmem, fargs);
1900  symbol_expr.swap(ptrmem);
1901  }
1902  }
1903  }
1904  }
1905 
1906  symbol_expr.add_source_location()=source_location;
1907  expr=symbol_expr;
1908 
1909  if(expr.id()==ID_symbol)
1911 
1913 }
1914 
1916 {
1917  if(is_reference(expr.type()))
1918  {
1919  // add implicit dereference
1920  dereference_exprt tmp(expr);
1921  tmp.set(ID_C_implicit, true);
1922  tmp.add_source_location()=expr.source_location();
1923  tmp.set(ID_C_lvalue, true);
1924  expr.swap(tmp);
1925  }
1926 }
1927 
1930 {
1931  // For virtual functions, it is important to check whether
1932  // the function name is qualified. If it is qualified, then
1933  // the call is not virtual.
1934  bool is_qualified=false;
1935 
1936  if(expr.function().id()==ID_member ||
1937  expr.function().id()==ID_ptrmember)
1938  {
1939  if(expr.function().get(ID_component_cpp_name)==ID_cpp_name)
1940  {
1941  const cpp_namet &cpp_name=
1942  to_cpp_name(expr.function().find(ID_component_cpp_name));
1943  is_qualified=cpp_name.is_qualified();
1944  }
1945  }
1946  else if(expr.function().id()==ID_cpp_name)
1947  {
1948  const cpp_namet &cpp_name=to_cpp_name(expr.function());
1949  is_qualified=cpp_name.is_qualified();
1950  }
1951 
1952  // Backup of the original operand
1953  exprt op0=expr.function();
1954 
1955  // now do the function -- this has been postponed
1957 
1958  if(expr.function().id()=="pod_constructor")
1959  {
1960  assert(expr.function().type().id()==ID_code);
1961 
1962  // This must be a POD.
1963  const typet &pod=to_code_type(expr.function().type()).return_type();
1964  assert(cpp_is_pod(pod));
1965 
1966  // These aren't really function calls, but either conversions or
1967  // initializations.
1968  if(expr.arguments().empty())
1969  {
1970  // create temporary object
1971  side_effect_exprt tmp_object_expr(ID_temporary_object, pod);
1972  tmp_object_expr.set(ID_C_lvalue, true);
1973  tmp_object_expr.set(ID_mode, ID_cpp);
1974  tmp_object_expr.add_source_location()=expr.source_location();
1975  expr.swap(tmp_object_expr);
1976  }
1977  else if(expr.arguments().size()==1)
1978  {
1979  exprt typecast("explicit-typecast");
1980  typecast.type()=pod;
1981  typecast.add_source_location()=expr.source_location();
1982  typecast.copy_to_operands(expr.arguments().front());
1984  expr.swap(typecast);
1985  }
1986  else
1987  {
1989  error() << "zero or one argument expected" << eom;
1990  throw 0;
1991  }
1992 
1993  return;
1994  }
1995  else if(expr.function().id()=="cast_expression")
1996  {
1997  // These are not really function calls,
1998  // but usually just type adjustments.
1999  typecheck_cast_expr(expr);
2001  return;
2002  }
2003  else if(expr.function().id()=="cpp_dummy_destructor")
2004  {
2005  // these don't do anything, e.g., (char*)->~char()
2006  expr.set(ID_statement, ID_skip);
2007  expr.type()=empty_typet();
2008  return;
2009  }
2010 
2011  // look at type of function
2012 
2013  expr.function().type() = follow(expr.function().type());
2014 
2015  if(expr.function().type().id()==ID_pointer)
2016  {
2017  if(expr.function().type().find("to-member").is_not_nil())
2018  {
2019  const exprt &bound=
2020  static_cast<const exprt &>(expr.function().type().find("#bound"));
2021 
2022  if(bound.is_nil())
2023  {
2025  error() << "pointer-to-member not bound" << eom;
2026  throw 0;
2027  }
2028 
2029  // add `this'
2030  assert(bound.type().id()==ID_pointer);
2031  expr.arguments().insert(expr.arguments().begin(), bound);
2032 
2033  // we don't need the object any more
2034  expr.function().type().remove("#bound");
2035  }
2036 
2037  // do implicit dereference
2038  if(expr.function().id()==ID_address_of &&
2039  expr.function().operands().size()==1)
2040  {
2041  exprt tmp;
2042  tmp.swap(expr.function().op0());
2043  expr.function().swap(tmp);
2044  }
2045  else
2046  {
2047  assert(expr.function().type().id()==ID_pointer);
2048  dereference_exprt tmp(expr.function());
2049  tmp.add_source_location()=expr.op0().source_location();
2050  expr.function().swap(tmp);
2051  }
2052 
2053  if(expr.function().type().id()!=ID_code)
2054  {
2056  error() << "expecting code as argument" << eom;
2057  throw 0;
2058  }
2059  }
2060  else if(expr.function().type().id()==ID_code)
2061  {
2062  if(expr.function().type().get_bool("#is_virtual") &&
2063  !is_qualified)
2064  {
2065  exprt vtptr_member;
2066  if(op0.id()==ID_member || op0.id()==ID_ptrmember)
2067  {
2068  vtptr_member.id(op0.id());
2069  vtptr_member.move_to_operands(op0.op0());
2070  }
2071  else
2072  {
2073  vtptr_member.id(ID_ptrmember);
2074  exprt this_expr("cpp-this");
2075  vtptr_member.move_to_operands(this_expr);
2076  }
2077 
2078  // get the virtual table
2079  typet this_type=
2080  to_code_type(expr.function().type()).parameters().front().type();
2081  irep_idt vtable_name=
2082  this_type.subtype().get_string(ID_identifier) +"::@vtable_pointer";
2083 
2084  const struct_typet &vt_struct=
2085  to_struct_type(follow(this_type.subtype()));
2086 
2087  const struct_typet::componentt &vt_compo=
2088  vt_struct.get_component(vtable_name);
2089 
2090  assert(vt_compo.is_not_nil());
2091 
2092  vtptr_member.set(ID_component_name, vtable_name);
2093 
2094  // look for the right entry
2095  irep_idt vtentry_component_name=
2096  vt_compo.type().subtype().get_string(ID_identifier)+"::"+
2097  expr.function().type().get_string("#virtual_name");
2098 
2099  exprt vtentry_member(ID_ptrmember);
2100  vtentry_member.copy_to_operands(vtptr_member);
2101  vtentry_member.set(ID_component_name, vtentry_component_name);
2102  typecheck_expr(vtentry_member);
2103 
2104  assert(vtentry_member.type().id()==ID_pointer);
2105 
2106  {
2107  dereference_exprt tmp(vtentry_member);
2108  tmp.add_source_location()=expr.op0().source_location();
2109  vtentry_member.swap(tmp);
2110  }
2111 
2112  // Typecheck the expression as if it was not virtual
2113  // (add the this pointer)
2114 
2115  expr.type()=
2116  to_code_type(expr.function().type()).return_type();
2117 
2119 
2120  // Let's make the call virtual
2121  expr.function().swap(vtentry_member);
2122 
2125  return;
2126  }
2127  }
2128  else if(expr.function().type().id()==ID_struct)
2129  {
2130  irept name(ID_name);
2131  name.set(ID_identifier, "operator()");
2132  name.set(ID_C_source_location, expr.source_location());
2133 
2134  cpp_namet cppname;
2135  cppname.get_sub().push_back(name);
2136 
2137  exprt member(ID_member);
2138  member.add(ID_component_cpp_name)=cppname;
2139 
2140  member.move_to_operands(op0);
2141 
2142  expr.function().swap(member);
2144 
2145  return;
2146  }
2147  else
2148  {
2150  error() << "function call expects function or function "
2151  << "pointer as argument, but got `"
2152  << to_string(expr.op0().type()) << "'" << eom;
2153  throw 0;
2154  }
2155 
2156  expr.type()=
2157  to_code_type(expr.function().type()).return_type();
2158 
2159  if(expr.type().id()==ID_constructor)
2160  {
2161  assert(expr.function().id() == ID_symbol);
2162 
2163  const code_typet::parameterst &parameters=
2164  to_code_type(expr.function().type()).parameters();
2165 
2166  assert(parameters.size()>=1);
2167 
2168  const typet &this_type=parameters[0].type();
2169 
2170  // change type from 'constructor' to object type
2171  expr.type()=this_type.subtype();
2172 
2173  // create temporary object
2174  side_effect_exprt tmp_object_expr(ID_temporary_object, this_type.subtype());
2175  tmp_object_expr.set(ID_C_lvalue, true);
2176  tmp_object_expr.set(ID_mode, ID_cpp);
2177  tmp_object_expr.add_source_location()=expr.source_location();
2178 
2179  exprt member;
2180 
2181  exprt new_object("new_object", tmp_object_expr.type());
2182  new_object.set(ID_C_lvalue, true);
2183 
2184  assert(follow(tmp_object_expr.type()).id()==ID_struct);
2185 
2187  new_object,
2188  expr.function().get(ID_identifier),
2189  member);
2190 
2191  // special case for the initialization of parents
2192  if(member.get_bool("#not_accessible"))
2193  {
2194  assert(member.get(ID_C_access)!="");
2195  tmp_object_expr.set("#not_accessible", true);
2196  tmp_object_expr.set(ID_C_access, member.get(ID_C_access));
2197  }
2198 
2199  // the constructor is being used, so make sure the destructor
2200  // will be available
2201  {
2202  // find name of destructor
2203  const struct_typet::componentst &components=
2204  to_struct_type(follow(tmp_object_expr.type())).components();
2205 
2206  for(struct_typet::componentst::const_iterator
2207  it=components.begin();
2208  it!=components.end();
2209  it++)
2210  {
2211  const typet &type=it->type();
2212 
2213  if(!it->get_bool(ID_from_base) &&
2214  type.id()==ID_code &&
2215  type.find(ID_return_type).id()==ID_destructor)
2216  {
2217  add_method_body(&symbol_table.get_writeable_ref(it->get(ID_name)));
2218  break;
2219  }
2220  }
2221  }
2222 
2223  expr.function().swap(member);
2224 
2227 
2228  const code_expressiont new_code(expr);
2229  tmp_object_expr.add(ID_initializer)=new_code;
2230  expr.swap(tmp_object_expr);
2231  return;
2232  }
2233 
2234  assert(expr.operands().size()==2);
2235 
2236  if(expr.function().id()==ID_member)
2238  else
2239  {
2240  // for the object of a method call,
2241  // we are willing to add an "address_of"
2242  // for the sake of operator overloading
2243 
2244  const irept::subt &arguments=
2245  expr.function().type().find(ID_arguments).get_sub();
2246 
2247  if(arguments.size()>=1 &&
2248  arguments.front().get(ID_C_base_name)==ID_this &&
2249  expr.arguments().size()>=1)
2250  {
2251  const exprt &argument=
2252  static_cast<const exprt &>(arguments.front());
2253 
2254  exprt &operand=expr.op1();
2255  assert(argument.type().id()==ID_pointer);
2256 
2257  if(operand.type().id()!=ID_pointer &&
2258  operand.type()==argument.type().subtype())
2259  {
2260  address_of_exprt tmp(operand, pointer_type(operand.type()));
2261  tmp.add_source_location()=operand.source_location();
2262  operand=tmp;
2263  }
2264  }
2265  }
2266 
2267  assert(expr.operands().size()==2);
2268 
2270 
2271  assert(expr.operands().size()==2);
2272 
2274 
2275  // we will deal with some 'special' functions here
2276  exprt tmp=do_special_functions(expr);
2277  if(tmp.is_not_nil())
2278  expr.swap(tmp);
2279 }
2280 
2285 {
2286  exprt &f_op=expr.function();
2287  const code_typet &code_type=to_code_type(f_op.type());
2288  const code_typet::parameterst &parameters=code_type.parameters();
2289 
2290  // do default arguments
2291 
2292  if(parameters.size()>expr.arguments().size())
2293  {
2294  std::size_t i=expr.arguments().size();
2295 
2296  for(; i<parameters.size(); i++)
2297  {
2298  if(!parameters[i].has_default_value())
2299  break;
2300 
2301  const exprt &value=parameters[i].default_value();
2302  expr.arguments().push_back(value);
2303  }
2304  }
2305 
2306  exprt::operandst::iterator arg_it=expr.arguments().begin();
2307  for(const auto &parameter : parameters)
2308  {
2309  if(parameter.get_bool("#call_by_value"))
2310  {
2311  assert(is_reference(parameter.type()));
2312 
2313  if(arg_it->id()!=ID_temporary_object)
2314  {
2315  // create a temporary for the parameter
2316 
2317  exprt arg("already_typechecked");
2318  arg.copy_to_operands(*arg_it);
2319 
2320  exprt temporary;
2321  new_temporary(
2322  arg_it->source_location(),
2323  parameter.type().subtype(),
2324  arg,
2325  temporary);
2326  arg_it->swap(temporary);
2327  }
2328  }
2329 
2330  ++arg_it;
2331  }
2332 
2334 }
2335 
2337  side_effect_exprt &expr)
2338 {
2339  const irep_idt &statement=expr.get(ID_statement);
2340 
2341  if(statement==ID_cpp_new ||
2342  statement==ID_cpp_new_array)
2343  {
2344  typecheck_expr_new(expr);
2345  }
2346  else if(statement==ID_cpp_delete ||
2347  statement==ID_cpp_delete_array)
2348  {
2349  typecheck_expr_delete(expr);
2350  }
2351  else if(statement==ID_preincrement ||
2352  statement==ID_predecrement ||
2353  statement==ID_postincrement ||
2354  statement==ID_postdecrement)
2355  {
2357  }
2358  else if(statement==ID_throw)
2359  {
2360  typecheck_expr_throw(expr);
2361  }
2362  else if(statement==ID_temporary_object)
2363  {
2364  // TODO
2365  }
2366  else
2368 }
2369 
2372 {
2373  assert(expr.operands().size()==2);
2374 
2375  assert(expr.function().id()==ID_member);
2376  assert(expr.function().operands().size()==1);
2377 
2378  // turn e.f(...) into xx::f(e, ...)
2379 
2380  exprt member_expr;
2381  member_expr.swap(expr.function());
2382 
2383  const symbolt &symbol=lookup(member_expr.get(ID_component_name));
2385 
2386  // build new function expression
2387  exprt new_function(cpp_symbol_expr(symbol));
2388  new_function.add_source_location()=member_expr.source_location();
2389  expr.function().swap(new_function);
2390 
2391  if(!expr.function().type().get_bool("#is_static"))
2392  {
2393  const code_typet &func_type=to_code_type(symbol.type);
2394  typet this_type=func_type.parameters().front().type();
2395 
2396  // Special case. Make it a reference.
2397  assert(this_type.id()==ID_pointer);
2398  this_type.set(ID_C_reference, true);
2399  this_type.set("#this", true);
2400 
2401  if(expr.arguments().size()==func_type.parameters().size())
2402  {
2403  // this might be set up for base-class initialisation
2404  if(!base_type_eq(expr.arguments().front().type(),
2405  func_type.parameters().front().type(),
2406  *this))
2407  {
2408  implicit_typecast(expr.arguments().front(), this_type);
2409  assert(is_reference(expr.arguments().front().type()));
2410  expr.arguments().front().type().remove(ID_C_reference);
2411  }
2412  }
2413  else
2414  {
2415  exprt this_arg=member_expr.op0();
2416  implicit_typecast(this_arg, this_type);
2417  assert(is_reference(this_arg.type()));
2418  this_arg.type().remove(ID_C_reference);
2419  expr.arguments().insert(expr.arguments().begin(), this_arg);
2420  }
2421  }
2422 
2423  if(symbol.value.id()=="cpp_not_typechecked" &&
2424  !symbol.value.get_bool("is_used"))
2425  {
2426  symbol_table.get_writeable_ref(symbol.name).value.set("is_used", true);
2427  }
2428 }
2429 
2431 {
2432  if(expr.operands().size()!=2)
2433  {
2435  error() << "assignment side effect expected to have two operands"
2436  << eom;
2437  throw 0;
2438  }
2439 
2440  typet type0=expr.op0().type();
2441 
2442  if(is_reference(type0))
2443  type0=type0.subtype();
2444 
2445  if(cpp_is_pod(type0))
2446  {
2447  // for structs we use the 'implicit assignment operator',
2448  // and therefore, it is allowed to assign to a rvalue struct.
2449  if(follow(type0).id()==ID_struct)
2450  expr.op0().set(ID_C_lvalue, true);
2451 
2453 
2454  // Note that in C++ (as opposed to C), the assignment yields
2455  // an lvalue!
2456  expr.set(ID_C_lvalue, true);
2457  return;
2458  }
2459 
2460  // It's a non-POD.
2461  // Turn into an operator call
2462 
2463  std::string strop="operator";
2464 
2465  const irep_idt statement=expr.get(ID_statement);
2466 
2467  if(statement==ID_assign)
2468  strop += "=";
2469  else if(statement==ID_assign_shl)
2470  strop += "<<=";
2471  else if(statement==ID_assign_shr)
2472  strop += ">>=";
2473  else if(statement==ID_assign_plus)
2474  strop += "+=";
2475  else if(statement==ID_assign_minus)
2476  strop += "-=";
2477  else if(statement==ID_assign_mult)
2478  strop += "*=";
2479  else if(statement==ID_assign_div)
2480  strop += "/=";
2481  else if(statement==ID_assign_bitand)
2482  strop += "&=";
2483  else if(statement==ID_assign_bitor)
2484  strop += "|=";
2485  else if(statement==ID_assign_bitxor)
2486  strop += "^=";
2487  else
2488  {
2490  error() << "bad assignment operator `" << statement << "'" << eom;
2491  throw 0;
2492  }
2493 
2494  cpp_namet cpp_name;
2495  cpp_name.get_sub().push_back(irept(ID_name));
2496  cpp_name.get_sub().front().set(ID_identifier, strop);
2497  cpp_name.get_sub().front().set(ID_C_source_location, expr.source_location());
2498 
2499  // expr.op0() is already typechecked
2500  exprt already_typechecked(ID_already_typechecked);
2501  already_typechecked.move_to_operands(expr.op0());
2502 
2503  exprt member(ID_member);
2504  member.set(ID_component_cpp_name, cpp_name);
2506 
2508  new_expr.function().swap(member);
2509  new_expr.arguments().push_back(expr.op1());
2510  new_expr.add_source_location()=expr.source_location();
2511 
2513 
2514  expr=new_expr;
2515 }
2516 
2518  side_effect_exprt &expr)
2519 {
2520  if(expr.operands().size()!=1)
2521  {
2523  error() << "statement " << expr.get_statement()
2524  << " expected to have one operand" << eom;
2525  throw 0;
2526  }
2527 
2528  add_implicit_dereference(expr.op0());
2529 
2530  typet tmp_type=follow(expr.op0().type());
2531 
2532  if(is_number(tmp_type) ||
2533  tmp_type.id()==ID_pointer)
2534  {
2535  // standard stuff
2537  return;
2538  }
2539 
2540  // Turn into an operator call
2541 
2542  std::string str_op="operator";
2543  bool post=false;
2544 
2545  if(expr.get(ID_statement)==ID_preincrement)
2546  str_op += "++";
2547  else if(expr.get(ID_statement)==ID_predecrement)
2548  str_op += "--";
2549  else if(expr.get(ID_statement)==ID_postincrement)
2550  {
2551  str_op += "++";
2552  post=true;
2553  }
2554  else if(expr.get(ID_statement)==ID_postdecrement)
2555  {
2556  str_op += "--";
2557  post=true;
2558  }
2559  else
2560  {
2562  error() << "bad assignment operator `"
2563  << expr.get_statement()
2564  << "'" << eom;
2565  throw 0;
2566  }
2567 
2568  cpp_namet cpp_name;
2569  cpp_name.get_sub().push_back(irept(ID_name));
2570  cpp_name.get_sub().front().set(ID_identifier, str_op);
2571  cpp_name.get_sub().front().set(ID_C_source_location, expr.source_location());
2572 
2573  exprt already_typechecked("already_typechecked");
2574  already_typechecked.move_to_operands(expr.op0());
2575 
2576  exprt member(ID_member);
2577  member.set(ID_component_cpp_name, cpp_name);
2579 
2581  new_expr.function().swap(member);
2582  new_expr.add_source_location()=expr.source_location();
2583 
2584  // the odd C++ way to denote the post-inc/dec operator
2585  if(post)
2586  new_expr.arguments().push_back(
2588 
2590  expr.swap(new_expr);
2591 }
2592 
2594 {
2595  if(expr.operands().size()!=1)
2596  {
2598  error() << "unary operator * expects one operand" << eom;
2599  throw 0;
2600  }
2601 
2602  exprt &op=expr.op0();
2603  const typet op_type=follow(op.type());
2604 
2605  if(op_type.id()==ID_pointer &&
2606  op_type.find("to-member").is_not_nil())
2607  {
2609  error() << "pointer-to-member must use "
2610  << "the .* or ->* operators" << eom;
2611  throw 0;
2612  }
2613 
2615 }
2616 
2618 {
2619  assert(expr.id()=="pointer-to-member");
2620  assert(expr.operands().size() == 2);
2621 
2622  if(expr.op1().type().id()!=ID_pointer
2623  || expr.op1().type().find("to-member").is_nil())
2624  {
2626  error() << "pointer-to-member expected" << eom;
2627  throw 0;
2628  }
2629 
2630  typet t0=expr.op0().type().id()==ID_pointer ?
2631  expr.op0().type().subtype(): expr.op0().type();
2632 
2633  typet t1((const typet&)expr.op1().type().find("to-member"));
2634 
2635  t0=follow(t0);
2636  t1=follow(t1);
2637 
2638  if(t0.id()!=ID_struct)
2639  {
2641  error() << "pointer-to-member type error" << eom;
2642  throw 0;
2643  }
2644 
2645  const struct_typet &from_struct=to_struct_type(t0);
2646  const struct_typet &to_struct=to_struct_type(t1);
2647 
2648  if(!subtype_typecast(from_struct, to_struct))
2649  {
2651  error() << "pointer-to-member type error" << eom;
2652  throw 0;
2653  }
2654 
2655  typecheck_expr_main(expr.op1());
2656 
2657  if(expr.op0().type().id()!=ID_pointer)
2658  {
2659  if(expr.op0().id()==ID_dereference)
2660  {
2661  exprt tmp=expr.op0().op0();
2662  expr.op0().swap(tmp);
2663  }
2664  else
2665  {
2666  assert(expr.op0().get_bool(ID_C_lvalue));
2667  expr.op0()=address_of_exprt(expr.op0());
2668  }
2669  }
2670 
2671  exprt tmp(expr.op1());
2672  tmp.type().set("#bound", expr.op0());
2673  expr.swap(tmp);
2674  return;
2675 }
2676 
2678 {
2679  if(expr.id()==ID_symbol)
2680  {
2681  // Check if the function body has to be typechecked
2682  symbol_tablet::symbolst::const_iterator it=
2683  symbol_table.symbols.find(expr.get(ID_identifier));
2684 
2685  assert(it != symbol_table.symbols.end());
2686 
2687  if(it->second.value.id()=="cpp_not_typechecked")
2688  symbol_table.get_writeable_ref(it->first).value.set("is_used", true);
2689  }
2690 
2692 }
2693 
2695 {
2696  bool override_constantness=
2697  expr.get_bool("#override_constantness");
2698 
2699  // We take care of an ambiguity in the C++ grammar.
2700  // Needs to be done before the operands!
2702 
2703  // cpp_name uses get_sub, which can get confused with expressions.
2704  if(expr.id()==ID_cpp_name)
2706  else
2707  {
2708  // This does the operands, and then calls typecheck_expr_main.
2710  }
2711 
2712  if(override_constantness)
2713  expr.type().set(ID_C_constant, false);
2714 }
2715 
2717 {
2718  // There is an ambiguity in the C++ grammar as follows:
2719  // (TYPENAME) + expr (typecast of unary plus) vs.
2720  // (expr) + expr (sum of two expressions)
2721  // Same issue with the operators & and - and *
2722 
2723  // We figure this out by resolving the type argument
2724  // and re-writing if needed
2725 
2726  if(expr.id()!="explicit-typecast")
2727  return;
2728 
2729  assert(expr.operands().size()==1);
2730 
2731  irep_idt op0_id=expr.op0().id();
2732 
2733  if(expr.type().id()==ID_cpp_name &&
2734  expr.op0().operands().size()==1 &&
2735  (op0_id==ID_unary_plus ||
2736  op0_id==ID_unary_minus ||
2737  op0_id==ID_address_of ||
2738  op0_id==ID_dereference))
2739  {
2740  exprt resolve_result=
2741  resolve(
2742  to_cpp_name(expr.type()),
2745 
2746  if(resolve_result.id()!=ID_type)
2747  {
2748  // need to re-write the expression
2749  // e.g., (ID) +expr -> ID+expr
2750  exprt new_binary_expr;
2751 
2752  new_binary_expr.operands().resize(2);
2753  new_binary_expr.op0().swap(expr.type());
2754  new_binary_expr.op1().swap(expr.op0().op0());
2755 
2756  if(op0_id==ID_unary_plus)
2757  new_binary_expr.id(ID_plus);
2758  else if(op0_id==ID_unary_minus)
2759  new_binary_expr.id(ID_minus);
2760  else if(op0_id==ID_address_of)
2761  new_binary_expr.id(ID_bitand);
2762  else if(op0_id==ID_dereference)
2763  new_binary_expr.id(ID_mult);
2764 
2765  new_binary_expr.add_source_location()=expr.op0().source_location();
2766  expr.swap(new_binary_expr);
2767  }
2768  }
2769 }
2770 
2772 {
2773  if(expr.operands().size()!=2)
2774  {
2776  error() << "operator `" << expr.id() << "' expects two operands"
2777  << eom;
2778  throw 0;
2779  }
2780 
2781  add_implicit_dereference(expr.op0());
2782  add_implicit_dereference(expr.op1());
2783 
2785 }
2786 
2788 {
2790 }
2791 
2793 {
2794  if(expr.operands().size()!=2)
2795  {
2797  error() << "comma operator expects two operands" << eom;
2798  throw 0;
2799  }
2800 
2801  if(follow(expr.op0().type()).id()==ID_struct)
2802  {
2803  // TODO: check if the comma operator has been overloaded!
2804  }
2805 
2807 }
2808 
2810 {
2812 }
side_effect_expr_function_callt & to_side_effect_expr_function_call(exprt &expr)
Definition: std_code.h:1392
void typecheck_function_expr(exprt &expr, const cpp_typecheck_fargst &fargs)
The type of an expression.
Definition: type.h:22
irep_idt name
The unique identifier.
Definition: symbol.h:43
exprt size_of_expr(const typet &type, const namespacet &ns)
void typecheck_expr_dereference(exprt &expr)
struct configt::ansi_ct ansi_c
virtual void typecheck_side_effect_assignment(side_effect_exprt &expr)
void typecheck_side_effect_function_call(side_effect_expr_function_callt &expr)
BigInt mp_integer
Definition: mp_arith.h:22
void typecheck_type(typet &type)
Base type of functions.
Definition: std_types.h:764
bool is_nil() const
Definition: irep.h:102
A generic base class for relations, i.e., binary predicates.
Definition: std_expr.h:752
bool is_not_nil() const
Definition: irep.h:103
pointer_typet pointer_type(const typet &subtype)
Definition: c_types.cpp:243
void new_temporary(const source_locationt &source_location, const typet &, const exprt::operandst &ops, exprt &temporary)
void typecheck_expr_member(exprt &expr)
virtual void typecheck_expr_index(exprt &expr)
void typecheck_expr_rel(binary_relation_exprt &expr)
exprt & op0()
Definition: expr.h:72
std::string pretty(unsigned indent=0, unsigned max_indent=0) const
Definition: irep.cpp:641
std::vector< irept > subt
Definition: irep.h:90
void add_implicit_dereference(exprt &expr)
static exprt collect_comma_expression(const exprt &src)
bool base_type_eq(const typet &type1, const typet &type2, const namespacet &ns)
Definition: base_type.cpp:326
bool static_typecast(const exprt &expr, const typet &type, exprt &new_expr, bool check_constantness=true)
bool has_base(const irep_idt &id) const
Definition: std_types.h:414
bool find_parent(const symbolt &symb, const irep_idt &base_name, irep_idt &identifier)
const code_typet & to_code_type(const typet &type)
Cast a generic typet to a code_typet.
Definition: std_types.h:987
exprt::operandst operands
void copy_to_operands(const exprt &expr)
Definition: expr.cpp:55
irept cpp_exception_list(const typet &src, const namespacet &ns)
turns a type into a list of relevant exception IDs
symbolt & get_writeable_ref(const irep_idt &name)
Find a symbol in the symbol table for read-write access.
cpp_namet & to_cpp_name(irept &cpp_name)
Definition: cpp_name.h:144
std::vector< componentt > componentst
Definition: std_types.h:243
void move_to_operands(exprt &expr)
Definition: expr.cpp:22
std::vector< parametert > parameterst
Definition: std_types.h:767
void typecheck_expr_function_identifier(exprt &expr)
void already_typechecked(irept &irep)
Definition: cpp_util.h:18
bool overloadable(const exprt &expr)
virtual void typecheck_code(codet &code)
exprt value
Initial value of symbol.
Definition: symbol.h:37
const componentst & components() const
Definition: std_types.h:245
The trinary if-then-else operator.
Definition: std_expr.h:3361
void explicit_typecast_ambiguity(exprt &expr)
typet & type()
Definition: expr.h:56
const class_typet & to_class_type(const typet &type)
Cast a generic typet to a class_typet.
Definition: std_types.h:435
void typecheck_method_application(side_effect_expr_function_callt &expr)
void typecheck_expr_delete(exprt &expr)
void typecheck_expr_this(exprt &expr)
virtual void implicit_typecast(exprt &expr, const typet &type)
The proper Booleans.
Definition: std_types.h:34
Symbol table entry.This is a symbol in the symbol table, stored in an object of type symbol_tablet...
Definition: symbol.h:30
static mstreamt & eom(mstreamt &m)
Definition: message.h:272
Structure type.
Definition: std_types.h:297
bool standard_conversion_function_to_pointer(const exprt &expr, exprt &new_expr) const
Function-to-pointer conversion.
bool get_bool(const irep_namet &name) const
Definition: irep.cpp:240
configt config
Definition: config.cpp:23
An expression statement.
Definition: std_code.h:1188
void typecheck_expr_typecast(exprt &expr)
bool is_qualified() const
Definition: cpp_name.h:109
void typecheck_expr_binary_arithmetic(exprt &expr)
virtual void typecheck_expr(exprt &expr)
subt & get_sub()
Definition: irep.h:245
symbol_tablet & symbol_table
void typecheck_expr_explicit_typecast(exprt &expr)
bool dynamic_typecast(const exprt &expr, const typet &type, exprt &new_expr)
Expression Initialization.
void add_method_body(symbolt *_method_symbol)
const irep_idt & id() const
Definition: irep.h:189
const source_locationt & source_location() const
Definition: cpp_name.h:73
void typecheck_expr_ptrmember(exprt &expr)
void typecheck_function_call_arguments(side_effect_expr_function_callt &expr)
void elaborate_class_template(const typet &type)
elaborate class template instances
virtual void typecheck_expr_side_effect(side_effect_exprt &expr)
void typecheck_expr_explicit_constructor_call(exprt &expr)
The boolean constant true.
Definition: std_expr.h:4488
virtual void typecheck_expr_operands(exprt &expr)
A reference into the symbol table.
Definition: std_types.h:110
void typecheck_cast_expr(exprt &expr)
reference_typet reference_type(const typet &subtype)
Definition: c_types.cpp:248
struct operator_entryt operators[]
The pointer type.
Definition: std_types.h:1426
const source_locationt & find_source_location() const
Definition: expr.cpp:246
C++ Language Module.
source_locationt source_location
Definition: message.h:214
The empty type.
Definition: std_types.h:54
bool cpp_is_pod(const typet &type) const
Definition: cpp_is_pod.cpp:14
Operator to dereference a pointer.
Definition: std_expr.h:3284
bool reinterpret_typecast(const exprt &expr, const typet &type, exprt &new_expr, bool check_constantness=true)
virtual void typecheck_expr_binary_arithmetic(exprt &expr)
C++ Language Type Checking.
bool is_reference(const typet &type)
TO_BE_DOCUMENTED.
Definition: std_types.cpp:105
exprt & op1()
Definition: expr.h:75
const irep_idt & get(const irep_namet &name) const
Definition: irep.cpp:213
codet cpp_destructor(const source_locationt &source_location, const typet &type, const exprt &object)
mstreamt & error() const
Definition: message.h:302
virtual void read(const typet &src) override
std::string cpp_type2name(const typet &type)
std::size_t int_width
Definition: config.h:30
virtual void typecheck_expr(exprt &expr)
Base class for tree-like data structures with sharing.
Definition: irep.h:86
#define forall_operands(it, expr)
Definition: expr.h:17
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast a generic exprt to a symbol_exprt.
Definition: std_expr.h:210
irep_idt class_identifier
Definition: cpp_id.h:76
void add_object(const exprt &expr)
C++ Language Type Checking.
const typet & follow(const typet &) const
Definition: namespace.cpp:55
void typecheck_expr_throw(exprt &expr)
bitvector_typet index_type()
Definition: c_types.cpp:16
const struct_typet & to_struct_type(const typet &type)
Cast a generic typet to a struct_typet.
Definition: std_types.h:318
void typecheck_side_effect_assignment(side_effect_exprt &expr)
void convert_pmop(exprt &expr)
Operator to return the address of an object.
Definition: std_expr.h:3170
void zero_initializer(const exprt &object, const typet &type, const source_locationt &source_location, exprt::operandst &ops)
const symbolst & symbols
exprt resolve(const cpp_namet &cpp_name, const cpp_typecheck_resolvet::wantt want, const cpp_typecheck_fargst &fargs, bool fail_with_exception=true)
Definition: cpp_typecheck.h:87
The boolean constant false.
Definition: std_expr.h:4499
virtual void typecheck_expr_function_identifier(exprt &expr)
void typecheck_expr_trinary(if_exprt &expr)
void make_ellipsis()
Definition: std_types.h:885
Pointer Logic.
exprt this_expr
Definition: cpp_id.h:77
virtual void typecheck_function_call_arguments(side_effect_expr_function_callt &expr)
cpp_scopet & set_scope(const irep_idt &identifier)
Definition: cpp_scopes.h:88
A function call side effect.
Definition: std_code.h:1352
bool const_typecast(const exprt &expr, const typet &type, exprt &new_expr)
bool get_component(const source_locationt &source_location, const exprt &object, const irep_idt &component_name, exprt &member)
void typecheck_expr_address_of(exprt &expr)
virtual exprt do_special_functions(side_effect_expr_function_callt &expr)
typet type
Type of symbol.
Definition: symbol.h:34
virtual void typecheck_expr_sizeof(exprt &expr)
bool subtype_typecast(const struct_typet &from, const struct_typet &to) const
message_handlert & get_message_handler()
Definition: message.h:153
bool operator_is_overloaded(exprt &expr)
bool standard_conversion_lvalue_to_rvalue(const exprt &expr, exprt &new_expr) const
Lvalue-to-rvalue conversion.
Base type of C structs and unions, and C++ classes.
Definition: std_types.h:162
mstreamt & result() const
Definition: message.h:312
bool is_number(const typet &type)
Definition: type.cpp:25
exprt & index()
Definition: std_expr.h:1496
virtual bool is_subset_of(const qualifierst &other) const override
Definition: c_qualifiers.h:107
void typecheck_expr_comma(exprt &expr)
std::string type2cpp(const typet &type, const namespacet &ns)
Definition: expr2cpp.cpp:511
const array_typet & to_array_type(const typet &type)
Cast a generic typet to an array_typet.
Definition: std_types.h:1045
Base class for all expressions.
Definition: expr.h:42
const parameterst & parameters() const
Definition: std_types.h:905
cpp_scopet & current_scope()
Definition: cpp_scopes.h:33
const struct_union_typet & to_struct_union_type(const typet &type)
Cast a generic typet to a struct_union_typet.
Definition: std_types.h:280
source_locationt & add_source_location()
Definition: type.h:102
const source_locationt & source_location() const
Definition: expr.h:125
#define UNREACHABLE
Definition: invariant.h:250
virtual void typecheck_expr_dereference(exprt &expr)
irept & add(const irep_namet &name)
Definition: irep.cpp:306
virtual std::string to_string(const typet &type)
const std::string & get_string(const irep_namet &name) const
Definition: irep.h:202
exprt::operandst & arguments()
Definition: std_code.h:1371
void make_nil()
Definition: irep.h:243
void swap(irept &irep)
Definition: irep.h:231
void typecheck_expr_side_effect(side_effect_exprt &expr)
source_locationt & add_source_location()
Definition: expr.h:130
void typecheck_expr_main(exprt &expr)
Called after the operands are done.
bool standard_conversion_array_to_pointer(const exprt &expr, exprt &new_expr) const
Array-to-pointer conversion.
Expression to hold a symbol (variable)
Definition: std_expr.h:90
exprt & op2()
Definition: expr.h:78
virtual void typecheck_expr_rel(binary_relation_exprt &expr)
virtual void do_initializer(exprt &initializer, const typet &type, bool force_constant)
dstringt irep_idt
Definition: irep.h:31
A statement in a programming language.
Definition: std_code.h:21
signedbv_typet signed_int_type()
Definition: c_types.cpp:30
void typecheck_expr_cpp_name(exprt &expr, const cpp_typecheck_fargst &fargs)
exprt cpp_symbol_expr(const symbolt &symbol)
Definition: cpp_util.cpp:14
void remove(const irep_namet &name)
Definition: irep.cpp:270
void typecheck_side_effect_inc_dec(side_effect_exprt &expr)
Base Type Computation.
const typet & subtype() const
Definition: type.h:33
virtual void typecheck_expr_address_of(exprt &expr)
virtual void typecheck_expr_comma(exprt &expr)
C++ class type.
Definition: std_types.h:341
An expression containing a side effect.
Definition: std_code.h:1238
void typecheck_expr_sizeof(exprt &expr)
bool implicit_conversion_sequence(const exprt &expr, const typet &type, exprt &new_expr, unsigned &rank)
implicit conversion sequence
virtual void typecheck_expr_main(exprt &expr)
operandst & operands()
Definition: expr.h:66
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
void typecheck_expr_index(exprt &expr)
typet c_bool_type()
Definition: c_types.cpp:108
bool empty() const
Definition: dstring.h:61
exprt & array()
Definition: std_expr.h:1486
const irept & find(const irep_namet &name) const
Definition: irep.cpp:285
codet cpp_constructor(const source_locationt &source_location, const exprt &object, const exprt::operandst &operands)
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See namespace_baset::lookup().
Definition: namespace.cpp:130
void typecheck_expr_new(exprt &expr)
void set(const irep_namet &name, const irep_idt &value)
Definition: irep.h:214
const componentt & get_component(const irep_idt &component_name) const
Definition: std_types.cpp:51
const irep_idt & get_statement() const
Definition: std_code.h:1253
#define forall_irep(it, irep)
Definition: irep.h:61
array index operator
Definition: std_expr.h:1462
cpp_scopest cpp_scopes