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 }
cpp_typecheckt::convert
void convert(cpp_linkage_spect &)
Definition: cpp_typecheck_linkage_spec.cpp:14
UNREACHABLE
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:503
struct_union_typet::components
const componentst & components() const
Definition: std_types.h:147
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
code_blockt
A codet representing sequential composition of program statements.
Definition: std_code.h:168
symbol_tablet
The symbol table.
Definition: symbol_table.h:14
to_unary_expr
const unary_exprt & to_unary_expr(const exprt &expr)
Cast an exprt to a unary_exprt.
Definition: std_expr.h:328
symbol_table_baset::lookup_ref
const symbolt & lookup_ref(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
Definition: symbol_table_base.h:104
typet::subtype
const typet & subtype() const
Definition: type.h:47
cpp_parse_treet
Definition: cpp_parse_tree.h:20
symbolt::is_macro
bool is_macro
Definition: symbol.h:61
cpp_typecheckt::convert_function
void convert_function(symbolt &symbol)
Definition: cpp_typecheck_function.cpp:82
to_struct_type
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition: std_types.h:308
cpp_typecheckt::typecheck_method_bodies
void typecheck_method_bodies()
Definition: cpp_typecheck_method_bodies.cpp:18
to_struct_union_type
const struct_union_typet & to_struct_union_type(const typet &type)
Cast a typet to a struct_union_typet.
Definition: std_types.h:214
cpp_typecheckt::cpp_scopes
cpp_scopest cpp_scopes
Definition: cpp_typecheck.h:104
cpp_idt::this_expr
exprt this_expr
Definition: cpp_id.h:76
irept::make_nil
void make_nil()
Definition: irep.h:465
typet
The type of an expression, extends irept.
Definition: type.h:28
cpp_typecheckt::this_struct_type
const struct_typet & this_struct_type()
Definition: cpp_typecheck.cpp:63
to_cpp_declaration
cpp_declarationt & to_cpp_declaration(irept &irep)
Definition: cpp_declaration.h:146
struct_union_typet
Base type for structs and unions.
Definition: std_types.h:62
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
irept::add
irept & add(const irep_namet &name)
Definition: irep.cpp:116
cpp_typecheckt::clean_up
void clean_up()
Definition: cpp_typecheck.cpp:265
builtin_factory
bool builtin_factory(const irep_idt &identifier, symbol_tablet &symbol_table, message_handlert &mh)
Check whether given identifier is a compiler built-in.
Definition: builtin_factory.cpp:97
cpp_typecheckt::builtin_factory
bool builtin_factory(const irep_idt &)
Definition: cpp_typecheck.cpp:325
namespacet::lookup
const symbolt & lookup(const irep_idt &name) const
Lookup a symbol in the namespace.
Definition: namespace.h:43
cpp_typecheckt::default_assignop_value
void default_assignop_value(const symbolt &symbol, cpp_declaratort &declarator)
Generate code for the implicit default assignment operator.
Definition: cpp_typecheck_constructor.cpp:342
cpp_typecheckt::current_linkage_spec
irep_idt current_linkage_spec
Definition: cpp_typecheck.h:107
exprt
Base class for all expressions.
Definition: expr.h:54
struct_union_typet::componentst
std::vector< componentt > componentst
Definition: std_types.h:140
cpp_typecheckt::cpp_constructor
optionalt< codet > cpp_constructor(const source_locationt &source_location, const exprt &object, const exprt::operandst &operands)
Definition: cpp_constructor.cpp:22
symbolt::base_name
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
expr2cpp.h
messaget::eom
static eomt eom
Definition: message.h:297
expr2cpp
std::string expr2cpp(const exprt &expr, const namespacet &ns)
Definition: expr2cpp.cpp:490
cpp_typecheckt::static_and_dynamic_initialization
void static_and_dynamic_initialization()
Initialization of static objects:
Definition: cpp_typecheck.cpp:148
cpp_typecheckt::contains_cpp_name
bool contains_cpp_name(const exprt &)
Definition: cpp_typecheck.cpp:330
cpp_itemt::source_location
const source_locationt & source_location() const
Definition: cpp_item.h:143
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:86
to_code
const codet & to_code(const exprt &expr)
Definition: std_code.h:153
cpp_itemt::get_linkage_spec
cpp_linkage_spect & get_linkage_spec()
Definition: cpp_item.h:57
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:91
cpp_typecheckt::cpp_is_pod
bool cpp_is_pod(const typet &type) const
Definition: cpp_is_pod.cpp:16
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:82
irept::get_bool
bool get_bool(const irep_namet &name) const
Definition: irep.cpp:58
irept::is_not_nil
bool is_not_nil() const
Definition: irep.h:391
c_typecheck_baset::module
const irep_idt module
Definition: c_typecheck_base.h:69
symbolt::mode
irep_idt mode
Language mode.
Definition: symbol.h:49
messaget::error
mstreamt & error() const
Definition: message.h:399
cpp_itemt::get_using
cpp_usingt & get_using()
Definition: cpp_item.h:107
cpp_itemt::is_linkage_spec
bool is_linkage_spec() const
Definition: cpp_item.h:69
cpp_util.h
symbol_table_baset::get_writeable_ref
symbolt & get_writeable_ref(const irep_idt &name)
Find a symbol in the symbol table for read-write access.
Definition: symbol_table_base.h:121
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:49
cpp_scopest::current_scope
cpp_scopet & current_scope()
Definition: cpp_scopes.h:32
messaget::mstreamt::source_location
source_locationt source_location
Definition: message.h:247
type2cpp
std::string type2cpp(const typet &type, const namespacet &ns)
Definition: expr2cpp.cpp:497
cpp_symbol_expr
symbol_exprt cpp_symbol_expr(const symbolt &symbol)
Definition: cpp_util.cpp:14
messaget::M_ERROR
@ M_ERROR
Definition: message.h:170
source_location.h
symbol_tablet::insert
virtual std::pair< symbolt &, bool > insert(symbolt symbol) override
Author: Diffblue Ltd.
Definition: symbol_table.cpp:17
c_typecheck_baset::symbol_table
symbol_tablet & symbol_table
Definition: c_typecheck_base.h:68
cpp_typecheckt::deferred_typechecking
std::unordered_set< irep_idt > deferred_typechecking
Definition: cpp_typecheck.h:590
irept::swap
void swap(irept &irep)
Definition: irep.h:453
code_typet
Base type of functions.
Definition: std_types.h:539
cpp_typecheckt
Definition: cpp_typecheck.h:40
symbol.h
Symbol table entry.
irept::id
const irep_idt & id() const
Definition: irep.h:407
message_handlert
Definition: message.h:28
cpp_typecheckt::cpp_parse_tree
cpp_parse_treet & cpp_parse_tree
Definition: cpp_typecheck.h:106
exprt::operandst
std::vector< exprt > operandst
Definition: expr.h:56
code_blockt::add
void add(const codet &code)
Definition: std_code.h:206
unary_exprt::op
const exprt & op() const
Definition: std_expr.h:293
cpp_declarator.h
C++ Language Type Checking.
cpp_itemt::is_static_assert
bool is_static_assert() const
Definition: cpp_item.h:138
cpp_typecheck.h
C++ Language Type Checking.
builtin_factory.h
cpp_itemt::is_using
bool is_using() const
Definition: cpp_item.h:119
symbolt::value
exprt value
Initial value of symbol.
Definition: symbol.h:34
messaget::get_message_handler
message_handlert & get_message_handler()
Definition: message.h:184
namespacet::get_symbol_table
const symbol_table_baset & get_symbol_table() const
Return first symbol table registered with the namespace.
Definition: namespace.h:123
struct_typet
Structure type, corresponds to C style structs.
Definition: std_types.h:231
symbolt::is_extern
bool is_extern
Definition: symbol.h:66
namespace_baset::follow
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:49
irept::get
const irep_idt & get(const irep_namet &name) const
Definition: irep.cpp:45
symbolt::location
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
symbolt
Symbol table entry.
Definition: symbol.h:28
symbol_table_baset::symbols
const symbolst & symbols
Read-only field, used to look up symbols given their names.
Definition: symbol_table_base.h:30
symbolt::is_type
bool is_type
Definition: symbol.h:61
cpp_typecheckt::do_not_typechecked
void do_not_typechecked()
Definition: cpp_typecheck.cpp:213
irept::get_sub
subt & get_sub()
Definition: irep.h:467
symbolt::is_static_lifetime
bool is_static_lifetime
Definition: symbol.h:65
cpp_itemt::is_namespace_spec
bool is_namespace_spec() const
Definition: cpp_item.h:94
cpp_declaratort::value
exprt & value()
Definition: cpp_declarator.h:42
cpp_itemt::get_static_assert
cpp_static_assertt & get_static_assert()
Definition: cpp_item.h:132
symbol_tablet::erase
virtual void erase(const symbolst::const_iterator &entry) override
Remove a symbol from the symbol table.
Definition: symbol_table.cpp:90
cpp_typecheckt::to_string
std::string to_string(const typet &) override
Definition: cpp_typecheck.cpp:81
exprt::operands
operandst & operands()
Definition: expr.h:92
exprt::add_source_location
source_locationt & add_source_location()
Definition: expr.h:235
symbolt::module
irep_idt module
Name of module the symbol belongs to.
Definition: symbol.h:43
cpp_itemt::get_namespace_spec
cpp_namespace_spect & get_namespace_spec()
Definition: cpp_item.h:82
cpp_typecheckt::dynamic_initializations
dynamic_initializationst dynamic_initializations
Definition: cpp_typecheck.h:588
cpp_typecheckt::disable_access_control
bool disable_access_control
Definition: cpp_typecheck.h:589
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40
cpp_parse_treet::items
itemst items
Definition: cpp_parse_tree.h:25
cpp_itemt
Definition: cpp_item.h:22
cpp_declaratort
Definition: cpp_declarator.h:20
message_handlert::get_message_count
std::size_t get_message_count(unsigned level) const
Definition: message.h:56
cpp_typecheckt::typecheck
void typecheck() override
typechecking main method
Definition: cpp_typecheck.cpp:46
cpp_itemt::is_declaration
bool is_declaration() const
Definition: cpp_item.h:44