cprover
cpp_typecheck.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 <algorithm>
15 
16 #include <util/source_location.h>
17 #include <util/symbol.h>
18 
19 #include <ansi-c/builtin_factory.h>
20 
21 #include "cpp_declarator.h"
22 #include "cpp_util.h"
23 #include "expr2cpp.h"
24 
26 {
27  if(item.is_declaration())
29  else if(item.is_linkage_spec())
30  convert(item.get_linkage_spec());
31  else if(item.is_namespace_spec())
33  else if(item.is_using())
34  convert(item.get_using());
35  else if(item.is_static_assert())
36  convert(item.get_static_assert());
37  else
38  {
40  error() << "unknown parse-tree element: " << item.id() << eom;
41  throw 0;
42  }
43 }
44 
47 {
48  // default linkage is "automatic"
49  current_linkage_spec=ID_auto;
50 
51  for(auto &item : cpp_parse_tree.items)
52  convert(item);
53 
55 
57 
59 
60  clean_up();
61 }
62 
64 {
65  const exprt &this_expr=
67 
68  assert(this_expr.is_not_nil());
69  assert(this_expr.type().id()==ID_pointer);
70 
71  const typet &t=follow(this_expr.type().subtype());
72  assert(t.id()==ID_struct);
73  return to_struct_type(t);
74 }
75 
76 std::string cpp_typecheckt::to_string(const exprt &expr)
77 {
78  return expr2cpp(expr, *this);
79 }
80 
81 std::string cpp_typecheckt::to_string(const typet &type)
82 {
83  return type2cpp(type, *this);
84 }
85 
87  cpp_parse_treet &cpp_parse_tree,
88  symbol_tablet &symbol_table,
89  const std::string &module,
90  message_handlert &message_handler)
91 {
93  cpp_parse_tree, symbol_table, module, message_handler);
94  return cpp_typecheck.typecheck_main();
95 }
96 
98  exprt &expr,
99  message_handlert &message_handler,
100  const namespacet &ns)
101 {
102  const unsigned errors_before=
103  message_handler.get_message_count(messaget::M_ERROR);
104 
105  symbol_tablet symbol_table;
106  cpp_parse_treet cpp_parse_tree;
107 
108  cpp_typecheckt cpp_typecheck(cpp_parse_tree, symbol_table,
109  ns.get_symbol_table(), "", message_handler);
110 
111  try
112  {
113  cpp_typecheck.typecheck_expr(expr);
114  }
115 
116  catch(int)
117  {
118  cpp_typecheck.error();
119  }
120 
121  catch(const char *e)
122  {
123  cpp_typecheck.error() << e << messaget::eom;
124  }
125 
126  catch(const std::string &e)
127  {
128  cpp_typecheck.error() << e << messaget::eom;
129  }
130 
131  return message_handler.get_message_count(messaget::M_ERROR)!=errors_before;
132 }
133 
149 {
150  code_blockt init_block; // Dynamic Initialization Block
151 
152  disable_access_control = true;
153 
154  for(const irep_idt &d_it : dynamic_initializations)
155  {
156  const symbolt &symbol = symbol_table.lookup_ref(d_it);
157 
158  if(symbol.is_extern)
159  continue;
160 
161  // PODs are always statically initialized
162  if(cpp_is_pod(symbol.type))
163  continue;
164 
165  assert(symbol.is_static_lifetime);
166  assert(!symbol.is_type);
167  assert(symbol.type.id()!=ID_code);
168 
169  exprt symbol_expr=cpp_symbol_expr(symbol);
170 
171  // initializer given?
172  if(symbol.value.is_not_nil())
173  {
174  // This will be a constructor call,
175  // which we execute.
176  init_block.add(to_code(symbol.value));
177 
178  // Make it nil to get zero initialization by
179  // __CPROVER_initialize
181  }
182  else
183  {
184  // use default constructor
185  exprt::operandst ops;
186 
187  auto call = cpp_constructor(symbol.location, symbol_expr, ops);
188 
189  if(call.has_value())
190  init_block.add(call.value());
191  }
192  }
193 
194  dynamic_initializations.clear();
195 
196  // Create the dynamic initialization procedure
197  symbolt init_symbol;
198 
199  init_symbol.name="#cpp_dynamic_initialization#"+id2string(module);
200  init_symbol.base_name="#cpp_dynamic_initialization#"+id2string(module);
201  init_symbol.value.swap(init_block);
202  init_symbol.mode=ID_cpp;
203  init_symbol.module=module;
204  init_symbol.type = code_typet({}, typet(ID_constructor));
205  init_symbol.is_type=false;
206  init_symbol.is_macro=false;
207 
208  symbol_table.insert(std::move(init_symbol));
209 
211 }
212 
214 {
215  bool cont;
216 
217  do
218  {
219  cont = false;
220 
221  for(const auto &named_symbol : symbol_table.symbols)
222  {
223  const symbolt &symbol=named_symbol.second;
224 
225  if(
226  symbol.value.id() == ID_cpp_not_typechecked &&
227  symbol.value.get_bool(ID_is_used))
228  {
229  assert(symbol.type.id()==ID_code);
230  exprt value = symbol.value;
231 
232  if(symbol.base_name=="operator=")
233  {
234  cpp_declaratort declarator;
235  declarator.add_source_location() = symbol.location;
237  lookup(symbol.type.get(ID_C_member_name)), declarator);
238  value.swap(declarator.value());
239  cont=true;
240  }
241  else if(symbol.value.operands().size()==1)
242  {
243  value = to_unary_expr(symbol.value).op();
244  cont=true;
245  }
246  else
247  UNREACHABLE; // Don't know what to do!
248 
249  symbolt &writable_symbol =
250  symbol_table.get_writeable_ref(named_symbol.first);
251  writable_symbol.value.swap(value);
252  convert_function(writable_symbol);
253  }
254  }
255  }
256  while(cont);
257 
258  for(const auto &named_symbol : symbol_table.symbols)
259  {
260  if(named_symbol.second.value.id() == ID_cpp_not_typechecked)
261  symbol_table.get_writeable_ref(named_symbol.first).value.make_nil();
262  }
263 }
264 
266 {
267  symbol_tablet::symbolst::const_iterator it=symbol_table.symbols.begin();
268 
269  while(it!=symbol_table.symbols.end())
270  {
271  symbol_tablet::symbolst::const_iterator cur_it = it;
272  it++;
273 
274  const symbolt &symbol=cur_it->second;
275 
276  // erase templates and all member functions that have not been converted
277  if(
278  symbol.type.get_bool(ID_is_template) ||
279  deferred_typechecking.find(symbol.name) != deferred_typechecking.end())
280  {
281  symbol_table.erase(cur_it);
282  continue;
283  }
284  else if(symbol.type.id()==ID_struct ||
285  symbol.type.id()==ID_union)
286  {
287  // remove methods from 'components'
288  struct_union_typet &struct_union_type=to_struct_union_type(
289  symbol_table.get_writeable_ref(cur_it->first).type);
290 
291  const struct_union_typet::componentst &components=
292  struct_union_type.components();
293 
294  struct_union_typet::componentst data_members;
295  data_members.reserve(components.size());
296 
297  struct_union_typet::componentst &function_members=
299  (struct_union_type.add(ID_methods).get_sub());
300 
301  function_members.reserve(components.size());
302 
303  for(const auto &compo_it : components)
304  {
305  if(compo_it.get_bool(ID_is_static) ||
306  compo_it.get_bool(ID_is_type))
307  {
308  // skip it
309  }
310  else if(compo_it.type().id()==ID_code)
311  {
312  function_members.push_back(compo_it);
313  }
314  else
315  {
316  data_members.push_back(compo_it);
317  }
318  }
319 
320  struct_union_type.components().swap(data_members);
321  }
322  }
323 }
324 
326 {
328 }
329 
331 {
332  if(expr.id() == ID_cpp_name || expr.id() == ID_cpp_declaration)
333  return true;
334 
335  for(const exprt &op : expr.operands())
336  {
337  if(contains_cpp_name(op))
338  return true;
339  }
340  return false;
341 }
bool builtin_factory(const irep_idt &identifier, symbol_tablet &symbol_table, message_handlert &mh)
Check whether given identifier is a compiler built-in.
symbol_tablet & symbol_table
const irep_idt module
A codet representing sequential composition of program statements.
Definition: std_code.h:168
void add(const codet &code)
Definition: std_code.h:206
Base type of functions.
Definition: std_types.h:539
exprt this_expr
Definition: cpp_id.h:76
bool is_namespace_spec() const
Definition: cpp_item.h:94
const source_locationt & source_location() const
Definition: cpp_item.h:143
cpp_usingt & get_using()
Definition: cpp_item.h:107
cpp_static_assertt & get_static_assert()
Definition: cpp_item.h:132
cpp_linkage_spect & get_linkage_spec()
Definition: cpp_item.h:57
bool is_static_assert() const
Definition: cpp_item.h:138
bool is_using() const
Definition: cpp_item.h:119
bool is_declaration() const
Definition: cpp_item.h:44
bool is_linkage_spec() const
Definition: cpp_item.h:69
cpp_namespace_spect & get_namespace_spec()
Definition: cpp_item.h:82
cpp_scopet & current_scope()
Definition: cpp_scopes.h:32
void static_and_dynamic_initialization()
Initialization of static objects:
bool contains_cpp_name(const exprt &)
void do_not_typechecked()
dynamic_initializationst dynamic_initializations
void convert_function(symbolt &symbol)
irep_idt current_linkage_spec
bool cpp_is_pod(const typet &type) const
Definition: cpp_is_pod.cpp:16
void convert(cpp_linkage_spect &)
std::unordered_set< irep_idt > deferred_typechecking
void default_assignop_value(const symbolt &symbol, cpp_declaratort &declarator)
Generate code for the implicit default assignment operator.
optionalt< codet > cpp_constructor(const source_locationt &source_location, const exprt &object, const exprt::operandst &operands)
void typecheck() override
typechecking main method
bool builtin_factory(const irep_idt &)
std::string to_string(const typet &) override
bool disable_access_control
const struct_typet & this_struct_type()
cpp_parse_treet & cpp_parse_tree
cpp_scopest cpp_scopes
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
Base class for all expressions.
Definition: expr.h:54
std::vector< exprt > operandst
Definition: expr.h:56
source_locationt & add_source_location()
Definition: expr.h:235
typet & type()
Return the type of the expression.
Definition: expr.h:82
operandst & operands()
Definition: expr.h:92
subt & get_sub()
Definition: irep.h:467
irept & add(const irep_namet &name)
Definition: irep.cpp:116
bool get_bool(const irep_namet &name) const
Definition: irep.cpp:58
bool is_not_nil() const
Definition: irep.h:391
const irep_idt & id() const
Definition: irep.h:407
void make_nil()
Definition: irep.h:465
void swap(irept &irep)
Definition: irep.h:453
const irep_idt & get(const irep_namet &name) const
Definition: irep.cpp:45
std::size_t get_message_count(unsigned level) const
Definition: message.h:56
source_locationt source_location
Definition: message.h:247
mstreamt & error() const
Definition: message.h:399
message_handlert & get_message_handler()
Definition: message.h:184
@ M_ERROR
Definition: message.h:170
static eomt eom
Definition: message.h:297
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:49
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:91
const symbolt & lookup(const irep_idt &name) const
Lookup a symbol in the namespace.
Definition: namespace.h:43
const symbol_table_baset & get_symbol_table() const
Return first symbol table registered with the namespace.
Definition: namespace.h:123
Structure type, corresponds to C style structs.
Definition: std_types.h:231
Base type for structs and unions.
Definition: std_types.h:62
const componentst & components() const
Definition: std_types.h:147
std::vector< componentt > componentst
Definition: std_types.h:140
symbolt & get_writeable_ref(const irep_idt &name)
Find a symbol in the symbol table for read-write access.
const symbolst & symbols
Read-only field, used to look up symbols given their names.
const symbolt & lookup_ref(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
The symbol table.
Definition: symbol_table.h:14
virtual void erase(const symbolst::const_iterator &entry) override
Remove a symbol from the symbol table.
virtual std::pair< symbolt &, bool > insert(symbolt symbol) override
Author: Diffblue Ltd.
Symbol table entry.
Definition: symbol.h:28
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
bool is_extern
Definition: symbol.h:66
bool is_macro
Definition: symbol.h:61
irep_idt module
Name of module the symbol belongs to.
Definition: symbol.h:43
bool is_static_lifetime
Definition: symbol.h:65
bool is_type
Definition: symbol.h:61
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
typet type
Type of symbol.
Definition: symbol.h:31
irep_idt name
The unique identifier.
Definition: symbol.h:40
exprt value
Initial value of symbol.
Definition: symbol.h:34
irep_idt mode
Language mode.
Definition: symbol.h:49
The type of an expression, extends irept.
Definition: type.h:28
const typet & subtype() const
Definition: type.h:47
const exprt & op() const
Definition: std_expr.h:293
cpp_declarationt & to_cpp_declaration(irept &irep)
C++ Language Type Checking.
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.
symbol_exprt cpp_symbol_expr(const symbolt &symbol)
Definition: cpp_util.cpp:14
std::string type2cpp(const typet &type, const namespacet &ns)
Definition: expr2cpp.cpp:497
std::string expr2cpp(const exprt &expr, const namespacet &ns)
Definition: expr2cpp.cpp:490
const std::string & id2string(const irep_idt &d)
Definition: irep.h:49
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:503
const codet & to_code(const exprt &expr)
Definition: std_code.h:153
const unary_exprt & to_unary_expr(const exprt &expr)
Cast an exprt to a unary_exprt.
Definition: std_expr.h:328
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition: std_types.h:308
const struct_union_typet & to_struct_union_type(const typet &type)
Cast a typet to a struct_union_typet.
Definition: std_types.h:214
Symbol table entry.