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