CBMC
cpp_typecheck_fargs.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_fargs.h"
13 
14 #include <util/std_types.h>
15 
16 #include "cpp_typecheck.h"
17 
19 {
20  for(const auto &op : operands)
21  {
22  if(op.type().id() == ID_struct)
23  return true;
24  }
25 
26  return false;
27 }
28 
30  const side_effect_expr_function_callt &function_call)
31 {
32  in_use=true;
33  operands = function_call.arguments();
34 }
35 
37  const code_typet &code_type,
38  unsigned &distance,
40 {
41  distance=0;
42 
44  const code_typet::parameterst &parameters=code_type.parameters();
45 
46  if(parameters.size()>ops.size())
47  {
48  // Check for default values.
49  ops.reserve(parameters.size());
50 
51  for(std::size_t i=ops.size(); i<parameters.size(); i++)
52  {
53  const exprt &default_value=
54  parameters[i].default_value();
55 
56  if(default_value.is_nil())
57  return false;
58 
59  ops.push_back(default_value);
60  }
61  }
62  else if(parameters.size()<ops.size())
63  {
64  // check for ellipsis
65  if(!code_type.has_ellipsis())
66  return false;
67  }
68 
69  exprt::operandst::iterator it=ops.begin();
70  for(const auto &parameter : parameters)
71  {
72  // read
73  // http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/topic/
74  // com.ibm.xlcpp8a.doc/language/ref/implicit_conversion_sequences.htm
75  //
76  // The following are the three categories of conversion sequences
77  // in order from best to worst:
78  // * Standard conversion sequences
79  // * User-defined conversion sequences
80  // * Ellipsis conversion sequences
81 
82  DATA_INVARIANT(it != ops.end(), "arguments and parameters must match");
83  const exprt &operand=*it;
84  typet type=parameter.type();
85 
86  #if 0
87  // unclear, todo
88  if(is_reference(operand.type()))
89  std::cout << "O: " << operand.pretty() << '\n';
90 
91  assert(!is_reference(operand.type()));
92  #endif
93 
94  // "this" is a special case -- we turn the pointer type
95  // into a reference type to do the type matching
96  if(it == ops.begin() && parameter.get_this())
97  {
98  type.set(ID_C_reference, true);
99  type.set(ID_C_this, true);
100  }
101 
102  unsigned rank=0;
103  exprt new_expr;
104 
105  #if 0
106  std::cout << "C: " << cpp_typecheck.to_string(operand.type())
107  << " -> " << cpp_typecheck.to_string(parameter.type())
108  << '\n';
109  #endif
110 
111  // can we do the standard conversion sequence?
112  if(cpp_typecheck.implicit_conversion_sequence(
113  operand, type, new_expr, rank))
114  {
115  // ok
116  distance+=rank;
117  #if 0
118  std::cout << "OK " << rank << '\n';
119  #endif
120  }
121  else if(
122  operand.id() == ID_initializer_list && cpp_typecheck.cpp_is_pod(type) &&
123  operand.operands().size() == 1 &&
124  cpp_typecheck.implicit_conversion_sequence(
125  to_unary_expr(operand).op(), type, new_expr, rank))
126  {
127  distance += rank;
128  }
129  else
130  {
131  #if 0
132  std::cout << "NOT OK\n";
133  #endif
134  return false; // no conversion possible
135  }
136 
137  ++it;
138  }
139 
140  // we may not have used all operands
141  for( ; it!=ops.end(); ++it)
142  // Ellipsis is the 'worst' of the conversion sequences
143  distance+=1000;
144 
145  return true;
146 }
Base type of functions.
Definition: std_types.h:583
std::vector< parametert > parameterst
Definition: std_types.h:585
bool has_ellipsis() const
Definition: std_types.h:655
const parameterst & parameters() const
Definition: std_types.h:699
exprt::operandst operands
bool match(const code_typet &code_type, unsigned &distance, cpp_typecheckt &cpp_typecheck) const
void build(const side_effect_expr_function_callt &function_call)
Base class for all expressions.
Definition: expr.h:56
std::vector< exprt > operandst
Definition: expr.h:58
typet & type()
Return the type of the expression.
Definition: expr.h:84
operandst & operands()
Definition: expr.h:94
std::string pretty(unsigned indent=0, unsigned max_indent=0) const
Definition: irep.cpp:482
void set(const irep_idt &name, const irep_idt &value)
Definition: irep.h:412
const irep_idt & id() const
Definition: irep.h:388
bool is_nil() const
Definition: irep.h:368
A side_effect_exprt representation of a function call side effect.
Definition: std_code.h:1692
exprt::operandst & arguments()
Definition: std_code.h:1718
The type of an expression, extends irept.
Definition: type.h:29
bool cpp_typecheck(cpp_parse_treet &cpp_parse_tree, symbol_table_baset &symbol_table, const std::string &module, message_handlert &message_handler)
C++ Language Type Checking.
C++ Language Type Checking.
bool is_reference(const typet &type)
Returns true if the type is a reference.
Definition: std_types.cpp:145
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition: invariant.h:534
const unary_exprt & to_unary_expr(const exprt &expr)
Cast an exprt to a unary_exprt.
Definition: std_expr.h:426
Pre-defined types.