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