cprover
cpp_typecheck_declaration.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 
15 
17 {
18  // see if the declaration is empty
19  if(declaration.is_empty())
20  return;
21 
22  // Record the function bodies so we can check them later.
23  // This function is used recursively, so we save them.
24  method_bodiest old_method_bodies;
25  old_method_bodies.swap(method_bodies);
26 
27  // templates are done in a dedicated function
28  if(declaration.is_template())
29  convert_template_declaration(declaration);
30  else
32 
34  b.swap(method_bodies);
36  method_bodies.swap(old_method_bodies);
37 }
38 
40  cpp_declarationt &declaration,
41  codet &code)
42 {
43  codet new_code(ID_decl_block);
44  new_code.reserve_operands(declaration.declarators().size());
45 
46  // unnamed object
47  std::string identifier="#anon_union"+std::to_string(anon_counter++);
48 
49  irept name(ID_name);
50  name.set(ID_identifier, identifier);
51  name.set(ID_C_source_location, declaration.source_location());
52 
53  cpp_namet cpp_name;
54  cpp_name.move_to_sub(name);
55  cpp_declaratort declarator;
56  declarator.name()=cpp_name;
57 
58  cpp_declarator_convertert cpp_declarator_converter(*this);
59 
60  const symbolt &symbol=
61  cpp_declarator_converter.convert(declaration, declarator);
62 
63  if(!cpp_is_pod(declaration.type()))
64  {
65  error().source_location=follow(declaration.type()).source_location();
66  error() << "anonymous union is not POD" << eom;
67  throw 0;
68  }
69 
70  codet decl_statement(ID_decl);
71  decl_statement.reserve_operands(2);
72  decl_statement.copy_to_operands(cpp_symbol_expr(symbol));
73 
74  new_code.move_to_operands(decl_statement);
75 
76  // do scoping
77  symbolt union_symbol=symbol_table.symbols[follow(symbol.type).get(ID_name)];
78  const irept::subt &components=union_symbol.type.add(ID_components).get_sub();
79 
80  forall_irep(it, components)
81  {
82  if(it->find(ID_type).id()==ID_code)
83  {
84  error().source_location=union_symbol.type.source_location();
85  error() << "anonymous union `" << union_symbol.base_name
86  << "' shall not have function members" << eom;
87  throw 0;
88  }
89 
90  const irep_idt &base_name=it->get(ID_base_name);
91 
92  if(cpp_scopes.current_scope().contains(base_name))
93  {
94  error().source_location=union_symbol.type.source_location();
95  error() << "identifier `" << base_name << "' already in scope"
96  << eom;
97  throw 0;
98  }
99 
100  cpp_idt &id=cpp_scopes.current_scope().insert(base_name);
102  id.identifier=it->get(ID_name);
103  id.class_identifier=union_symbol.name;
104  id.is_member=true;
105  }
106 
107  symbol_table.symbols[union_symbol.name].type.set(
108  "#unnamed_object", symbol.base_name);
109 
110  code.swap(new_code);
111 }
112 
114  cpp_declarationt &declaration)
115 {
116  assert(!declaration.is_template());
117 
118  // we first check if this is a typedef
119  typet &declaration_type=declaration.type();
120  bool is_typedef=declaration.is_typedef();
121 
122  // the name anonymous tag types
123  declaration.name_anon_struct_union();
124 
125  // do the type of the declaration
126  typecheck_type(declaration_type);
127 
128  // Elaborate any class template instance _unless_ we do a typedef.
129  // These are only elaborated on usage!
130  if(!is_typedef)
131  elaborate_class_template(declaration_type);
132 
133  // Special treatment for anonymous unions
134  if(declaration.declarators().empty() &&
135  follow(declaration.type()).get_bool(ID_C_is_anonymous))
136  {
137  typet final_type=follow(declaration.type());
138 
139  if(final_type.id()!=ID_union)
140  {
141  error().source_location=final_type.source_location();
142  error() << "top-level declaration does not declare anything"
143  << eom;
144  throw 0;
145  }
146 
147  codet dummy;
148  convert_anonymous_union(declaration, dummy);
149  }
150 
151  // do the declarators (optional)
152  for(auto &d : declaration.declarators())
153  {
154  // copy the declarator (we destroy the original)
155  cpp_declaratort declarator=d;
156 
157  cpp_declarator_convertert cpp_declarator_converter(*this);
158 
159  cpp_declarator_converter.is_typedef=is_typedef;
160 
161  symbolt &symbol=cpp_declarator_converter.convert(
162  declaration_type, declaration.storage_spec(),
163  declaration.member_spec(), declarator);
164 
165  // any template instance to remember?
166  if(declaration.find(ID_C_template).is_not_nil())
167  {
168  symbol.type.set(ID_C_template, declaration.find(ID_C_template));
169  symbol.type.set(
170  ID_C_template_arguments,
171  declaration.find(ID_C_template_arguments));
172  }
173 
174  // replace declarator by symbol expression
175  exprt tmp=cpp_symbol_expr(symbol);
176  d.swap(tmp);
177 
178  // is there a constructor to be called for the declarator?
179  if(symbol.is_lvalue &&
180  declarator.init_args().has_operands())
181  {
182  symbol.value=
184  symbol.location,
185  cpp_symbol_expr(symbol),
186  declarator.init_args().operands());
187  }
188  }
189 }
bool is_typedef() const
C++ Language Type Checking.
The type of an expression.
Definition: type.h:20
irep_idt name
The unique identifier.
Definition: symbol.h:46
const typet & follow(const typet &src) const
Definition: namespace.cpp:66
void typecheck_type(typet &type)
bool is_not_nil() const
Definition: irep.h:104
const cpp_storage_spect & storage_spec() const
void convert_non_template_declaration(cpp_declarationt &declaration)
std::vector< irept > subt
Definition: irep.h:91
void move_to_sub(irept &irep)
Definition: irep.cpp:204
void name_anon_struct_union()
void copy_to_operands(const exprt &expr)
Definition: expr.cpp:61
void move_to_operands(exprt &expr)
Definition: expr.cpp:28
exprt value
Initial value of symbol.
Definition: symbol.h:40
typet & type()
Definition: expr.h:60
Symbol table entry.This is a symbol in the symbol table, stored in an object of type symbol_tablet...
Definition: symbol.h:33
static mstreamt & eom(mstreamt &m)
Definition: message.h:193
const cpp_member_spect & member_spec() const
subt & get_sub()
Definition: irep.h:245
void convert(cpp_linkage_spect &)
symbol_tablet & symbol_table
const irep_idt & id() const
Definition: irep.h:189
void elaborate_class_template(const typet &type)
elaborate class template instances
const declaratorst & declarators() const
symbolst symbols
Definition: symbol_table.h:57
exprt & init_args()
source_locationt source_location
Definition: message.h:175
bool cpp_is_pod(const typet &type) const
Definition: cpp_is_pod.cpp:14
id_classt id_class
Definition: cpp_id.h:51
const irep_idt & get(const irep_namet &name) const
Definition: irep.cpp:213
Base class for tree-like data structures with sharing.
Definition: irep.h:87
void convert_template_declaration(cpp_declarationt &declaration)
C++ Language Type Checking.
unsigned anon_counter
bool has_operands() const
Definition: expr.h:67
const source_locationt & source_location() const
Definition: type.h:95
void typecheck_method_bodies(method_bodiest &)
bool is_template() const
method_bodiest method_bodies
typet type
Type of symbol.
Definition: symbol.h:37
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:43
void convert_anonymous_union(cpp_declarationt &declaration, codet &new_code)
Base class for all expressions.
Definition: expr.h:46
bool contains(const irep_idt &base_name)
Definition: cpp_scope.cpp:212
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:52
cpp_scopet & current_scope()
Definition: cpp_scopes.h:33
symbolt & convert(const typet &type, const cpp_storage_spect &storage_spec, const cpp_member_spect &member_spec, cpp_declaratort &declarator)
const source_locationt & source_location() const
Definition: expr.h:142
irept & add(const irep_namet &name)
Definition: irep.cpp:306
void swap(irept &irep)
Definition: irep.h:231
mstreamt & error()
Definition: message.h:223
cpp_namet & name()
Definition: cpp_id.h:28
A statement in a programming language.
Definition: std_code.h:19
exprt cpp_symbol_expr(const symbolt &symbol)
Definition: cpp_util.cpp:14
std::list< method_bodyt > method_bodiest
operandst & operands()
Definition: expr.h:70
cpp_idt & insert(const irep_idt &_base_name)
Definition: cpp_scope.h:48
const irept & find(const irep_namet &name) const
Definition: irep.cpp:285
bool is_empty() const
codet cpp_constructor(const source_locationt &source_location, const exprt &object, const exprt::operandst &operands)
void set(const irep_namet &name, const irep_idt &value)
Definition: irep.h:214
void reserve_operands(operandst::size_type n)
Definition: expr.h:108
bool is_lvalue
Definition: symbol.h:71
#define forall_irep(it, irep)
Definition: irep.h:62
cpp_scopest cpp_scopes