cprover
cpp_is_pod.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 bool cpp_typecheckt::cpp_is_pod(const typet &type) const
15 {
16  if(type.id()==ID_struct)
17  {
18  // Not allowed in PODs:
19  // * Non-PODs
20  // * Constructors/Destructors
21  // * virtuals
22  // * private/protected, unless static
23  // * overloading assignment operator
24  // * Base classes
25 
26  const struct_typet &struct_type=to_struct_type(type);
27 
28  if(!type.find(ID_bases).get_sub().empty())
29  return false;
30 
31  const struct_typet::componentst &components=
32  struct_type.components();
33 
34  for(struct_typet::componentst::const_iterator
35  it=components.begin();
36  it!=components.end();
37  it++)
38  {
39  if(it->get_bool(ID_is_type))
40  continue;
41 
42  if(it->get_base_name()=="operator=")
43  return false;
44 
45  if(it->get_bool(ID_is_virtual))
46  return false;
47 
48  const typet &sub_type=it->type();
49 
50  if(sub_type.id()==ID_code)
51  {
52  if(it->get_bool(ID_is_virtual))
53  return false;
54 
55  const typet &return_type=to_code_type(sub_type).return_type();
56 
57  if(return_type.id()==ID_constructor ||
58  return_type.id()==ID_destructor)
59  return false;
60  }
61  else if(it->get(ID_access)!=ID_public &&
62  !it->get_bool(ID_is_static))
63  return false;
64 
65  if(!cpp_is_pod(sub_type))
66  return false;
67  }
68 
69  return true;
70  }
71  else if(type.id()==ID_array)
72  {
73  return cpp_is_pod(type.subtype());
74  }
75  else if(type.id()==ID_pointer)
76  {
77  if(is_reference(type)) // references are not PODs
78  return false;
79 
80  // but pointers are PODs!
81  return true;
82  }
83  else if(type.id()==ID_symbol)
84  {
85  const symbolt &symb = lookup(to_symbol_type(type));
86  DATA_INVARIANT(symb.is_type, "type symbol is a type");
87  return cpp_is_pod(symb.type);
88  }
89 
90  // everything else is POD
91  return true;
92 }
The type of an expression.
Definition: type.h:22
const code_typet & to_code_type(const typet &type)
Cast a generic typet to a code_typet.
Definition: std_types.h:993
std::vector< componentt > componentst
Definition: std_types.h:243
const symbol_typet & to_symbol_type(const typet &type)
Cast a generic typet to a symbol_typet.
Definition: std_types.h:139
const componentst & components() const
Definition: std_types.h:245
Symbol table entry.This is a symbol in the symbol table, stored in an object of type symbol_tablet...
Definition: symbol.h:30
Structure type.
Definition: std_types.h:297
subt & get_sub()
Definition: irep.h:317
const irep_idt & id() const
Definition: irep.h:259
bool cpp_is_pod(const typet &type) const
Definition: cpp_is_pod.cpp:14
bool is_reference(const typet &type)
TO_BE_DOCUMENTED.
Definition: std_types.cpp:105
C++ Language Type Checking.
const struct_typet & to_struct_type(const typet &type)
Cast a generic typet to a struct_typet.
Definition: std_types.h:318
typet type
Type of symbol.
Definition: symbol.h:34
bool is_type
Definition: symbol.h:63
const typet & subtype() const
Definition: type.h:33
#define DATA_INVARIANT(CONDITION, REASON)
Definition: invariant.h:278
const irept & find(const irep_namet &name) const
Definition: irep.cpp:285
const typet & return_type() const
Definition: std_types.h:895
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See namespace_baset::lookup().
Definition: namespace.cpp:136