cprover
cpp_typecheck_type.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 #include <util/source_location.h>
15 #include <util/simplify_expr.h>
16 #include <util/c_types.h>
17 
18 #include <ansi-c/c_qualifiers.h>
19 
20 #include "cpp_convert_type.h"
21 #include "expr2cpp.h"
22 
24 {
25  assert(!type.id().empty());
26  assert(type.is_not_nil());
27 
28  try
29  {
31  }
32 
33  catch(const char *err)
34  {
36  error() << err << eom;
37  throw 0;
38  }
39 
40  catch(const std::string &err)
41  {
43  error() << err << eom;
44  throw 0;
45  }
46 
47  if(type.id()==ID_cpp_name)
48  {
49  c_qualifierst qualifiers(type);
50 
51  cpp_namet cpp_name;
52  cpp_name.swap(type);
53 
54  exprt symbol_expr=resolve(
55  cpp_name,
58 
59  if(symbol_expr.id()!=ID_type)
60  {
62  error() << "error: expected type" << eom;
63  throw 0;
64  }
65 
66  type=symbol_expr.type();
67  assert(type.is_not_nil());
68 
69  if(type.get_bool(ID_C_constant))
70  qualifiers.is_constant = true;
71 
72  qualifiers.write(type);
73  }
74  else if(type.id()==ID_struct ||
75  type.id()==ID_union)
76  {
78  }
79  else if(type.id()==ID_pointer)
80  {
81  c_qualifierst qualifiers(type);
82 
83  // the pointer/reference might have a qualifier,
84  // but do subtype first
85  typecheck_type(type.subtype());
86 
87  // Check if it is a pointer-to-member
88  if(type.find("to-member").is_not_nil())
89  {
90  // these can point either to data members or member functions
91  // of a class
92 
93  typet &class_object=static_cast<typet &>(type.add("to-member"));
94 
95  if(class_object.id()==ID_cpp_name)
96  {
97  assert(class_object.get_sub().back().id()=="::");
98  class_object.get_sub().pop_back();
99  }
100 
101  typecheck_type(class_object);
102 
103  // there may be parameters if this is a pointer to member function
104  if(type.subtype().id()==ID_code)
105  {
106  irept::subt &parameters=type.subtype().add(ID_parameters).get_sub();
107 
108  if(parameters.empty() ||
109  parameters.front().get(ID_C_base_name)!=ID_this)
110  {
111  // Add 'this' to the parameters
112  exprt a0(ID_parameter);
113  a0.set(ID_C_base_name, ID_this);
114  a0.type()=pointer_type(class_object);
115  parameters.insert(parameters.begin(), a0);
116  }
117  }
118  }
119 
120  if(type.get_bool(ID_C_constant))
121  qualifiers.is_constant = true;
122 
123  qualifiers.write(type);
124  }
125  else if(type.id()==ID_array)
126  {
127  exprt &size_expr=to_array_type(type).size();
128 
129  if(size_expr.is_not_nil())
130  {
131  typecheck_expr(size_expr);
132  simplify(size_expr, *this);
133  }
134 
135  typecheck_type(type.subtype());
136 
137  if(type.subtype().get_bool(ID_C_constant))
138  type.set(ID_C_constant, true);
139 
140  if(type.subtype().get_bool(ID_C_volatile))
141  type.set(ID_C_volatile, true);
142  }
143  else if(type.id()==ID_vector)
144  {
146  }
147  else if(type.id()==ID_code)
148  {
149  code_typet &code_type=to_code_type(type);
150  typecheck_type(code_type.return_type());
151 
152  code_typet::parameterst &parameters=code_type.parameters();
153 
154  for(auto &param : parameters)
155  {
156  typecheck_type(param.type());
157 
158  // see if there is a default value
159  if(param.has_default_value())
160  {
161  typecheck_expr(param.default_value());
162  implicit_typecast(param.default_value(), param.type());
163  }
164  }
165  }
166  else if(type.id()==ID_template)
167  {
168  typecheck_type(type.subtype());
169  }
170  else if(type.id()==ID_c_enum)
171  {
172  typecheck_enum_type(type);
173  }
174  else if(type.id()==ID_c_enum_tag)
175  {
176  }
177  else if(type.id()==ID_c_bit_field)
178  {
180  }
181  else if(type.id()==ID_unsignedbv ||
182  type.id()==ID_signedbv ||
183  type.id()==ID_bool ||
184  type.id()==ID_floatbv ||
185  type.id()==ID_fixedbv ||
186  type.id()==ID_empty)
187  {
188  }
189  else if(type.id()==ID_symbol)
190  {
191  }
192  else if(type.id()==ID_constructor ||
193  type.id()==ID_destructor)
194  {
195  }
196  else if(type.id()=="cpp-cast-operator")
197  {
198  }
199  else if(type.id()=="cpp-template-type")
200  {
201  }
202  else if(type.id()==ID_typeof)
203  {
204  exprt e=static_cast<const exprt &>(type.find(ID_expr_arg));
205 
206  if(e.is_nil())
207  {
208  typet tmp_type=
209  static_cast<const typet &>(type.find(ID_type_arg));
210 
211  if(tmp_type.id()==ID_cpp_name)
212  {
213  // this may be ambiguous -- it can be either a type or
214  // an expression
215 
216  cpp_typecheck_fargst fargs;
217 
218  exprt symbol_expr=resolve(
219  to_cpp_name(static_cast<const irept &>(tmp_type)),
221  fargs);
222 
223  type=symbol_expr.type();
224  }
225  else
226  {
227  typecheck_type(tmp_type);
228  type=tmp_type;
229  }
230  }
231  else
232  {
233  typecheck_expr(e);
234  type=e.type();
235  }
236  }
237  else if(type.id()==ID_decltype)
238  {
239  exprt e=static_cast<const exprt &>(type.find(ID_expr_arg));
240  typecheck_expr(e);
241 
242  if(e.type().id() == ID_c_bit_field)
243  type = e.type().subtype();
244  else
245  type = e.type();
246  }
247  else if(type.id()==ID_unassigned)
248  {
249  // ignore, for template parameter guessing
250  }
251  else if(type.id()==ID_template_class_instance)
252  {
253  // ok (internally generated)
254  }
255  else if(type.id()==ID_block_pointer)
256  {
257  // This is an Apple extension for lambda-like constructs.
258  // http://thirdcog.eu/pwcblocks/
259  // we just treat them as references to functions
260  type.id(ID_frontend_pointer);
261  typecheck_type(type);
262  }
263  else if(type.id()==ID_nullptr)
264  {
265  }
266  else if(type.id()==ID_already_typechecked)
267  {
269  }
270  else
271  {
273  error() << "unexpected cpp type: " << type.pretty() << eom;
274  throw 0;
275  }
276 
277  assert(type.is_not_nil());
278 }
The type of an expression.
Definition: type.h:22
void typecheck_type(typet &type)
Base type of functions.
Definition: std_types.h:764
bool is_nil() const
Definition: irep.h:172
bool is_not_nil() const
Definition: irep.h:173
pointer_typet pointer_type(const typet &subtype)
Definition: c_types.cpp:243
std::string pretty(unsigned indent=0, unsigned max_indent=0) const
Definition: irep.cpp:641
std::vector< irept > subt
Definition: irep.h:160
const code_typet & to_code_type(const typet &type)
Cast a generic typet to a code_typet.
Definition: std_types.h:993
cpp_namet & to_cpp_name(irept &cpp_name)
Definition: cpp_name.h:144
std::vector< parametert > parameterst
Definition: std_types.h:767
typet & type()
Definition: expr.h:56
virtual void implicit_typecast(exprt &expr, const typet &type)
static mstreamt & eom(mstreamt &m)
Definition: message.h:272
bool get_bool(const irep_namet &name) const
Definition: irep.cpp:240
virtual void typecheck_expr(exprt &expr)
subt & get_sub()
Definition: irep.h:317
void typecheck_enum_type(typet &type)
const irep_idt & id() const
Definition: irep.h:259
source_locationt source_location
Definition: message.h:214
C++ Language Conversion.
mstreamt & error() const
Definition: message.h:302
const vector_typet & to_vector_type(const typet &type)
Cast a generic typet to a vector_typet.
Definition: std_types.h:1669
const exprt & size() const
Definition: std_types.h:1023
C++ Language Type Checking.
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
const source_locationt & source_location() const
Definition: type.h:97
virtual void typecheck_vector_type(vector_typet &type)
const array_typet & to_array_type(const typet &type)
Cast a generic typet to an array_typet.
Definition: std_types.h:1054
Base class for all expressions.
Definition: expr.h:42
const parameterst & parameters() const
Definition: std_types.h:905
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
irept & add(const irep_namet &name)
Definition: irep.cpp:306
void cpp_convert_plain_type(typet &type)
void typecheck_compound_type(struct_union_typet &type)
void swap(irept &irep)
Definition: irep.h:303
virtual void write(typet &src) const override
const c_bit_field_typet & to_c_bit_field_type(const typet &type)
Cast a generic typet to a c_bit_field_typet.
Definition: std_types.h:1411
virtual void typecheck_type(typet &type)
const typet & subtype() const
Definition: type.h:33
virtual void typecheck_c_bit_field_type(c_bit_field_typet &type)
bool empty() const
Definition: dstring.h:73
const irept & find(const irep_namet &name) const
Definition: irep.cpp:285
const typet & return_type() const
Definition: std_types.h:895
void set(const irep_namet &name, const irep_idt &value)
Definition: irep.h:286
bool simplify(exprt &expr, const namespacet &ns)