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