-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathcpp_typecheck_declaration.cpp
More file actions
204 lines (164 loc) · 6.13 KB
/
cpp_typecheck_declaration.cpp
File metadata and controls
204 lines (164 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*******************************************************************\
Module: C++ Language Type Checking
Author: Daniel Kroening, [email protected]
\********************************************************************/
/// \file
/// C++ Language Type Checking
#include <util/c_types.h>
#include <util/symbol_table_base.h>
#include "cpp_declarator_converter.h"
#include "cpp_typecheck.h"
#include "cpp_util.h"
void cpp_typecheckt::convert(cpp_declarationt &declaration)
{
// see if the declaration is empty
if(declaration.is_empty())
return;
// The function bodies must not be checked here,
// but only at the very end when all declarations have been
// processed (or considering forward declarations at least)
// templates are done in a dedicated function
if(declaration.is_template())
convert_template_declaration(declaration);
else
convert_non_template_declaration(declaration);
}
codet cpp_typecheckt::convert_anonymous_union(cpp_declarationt &declaration)
{
codet new_code(ID_decl_block);
new_code.reserve_operands(declaration.declarators().size());
// unnamed object
std::string identifier="#anon_union"+std::to_string(anon_counter++);
const cpp_namet cpp_name(identifier, declaration.source_location());
cpp_declaratort declarator;
declarator.name()=cpp_name;
cpp_declarator_convertert cpp_declarator_converter(*this);
const symbolt &symbol=
cpp_declarator_converter.convert(declaration, declarator);
if(!cpp_is_pod(declaration.type()))
{
const typet &followed =
declaration.type().id() == ID_struct_tag
? static_cast<const typet &>(
follow_tag(to_struct_tag_type(declaration.type())))
: declaration.type().id() == ID_union_tag
? static_cast<const typet &>(
follow_tag(to_union_tag_type(declaration.type())))
: declaration.type().id() == ID_c_enum_tag
? static_cast<const typet &>(
follow_tag(to_c_enum_tag_type(declaration.type())))
: declaration.type();
error().source_location = followed.source_location();
error() << "anonymous union is not POD" << eom;
throw 0;
}
new_code.add_to_operands(code_frontend_declt(cpp_symbol_expr(symbol)));
// do scoping
symbolt &union_symbol = symbol_table.get_writeable_ref(
follow_tag(to_union_tag_type(symbol.type)).get(ID_name));
for(const auto &c : to_union_type(union_symbol.type).components())
{
if(c.type().id() == ID_code)
{
error().source_location=union_symbol.type.source_location();
error() << "anonymous union '" << union_symbol.base_name
<< "' shall not have function members" << eom;
throw 0;
}
const irep_idt &base_name = c.get_base_name();
if(cpp_scopes.current_scope().contains(base_name))
{
error().source_location=union_symbol.type.source_location();
error() << "identifier '" << base_name << "' already in scope" << eom;
throw 0;
}
cpp_idt &id=cpp_scopes.current_scope().insert(base_name);
id.id_class = cpp_idt::id_classt::SYMBOL;
id.identifier = c.get_name();
id.class_identifier=union_symbol.name;
id.is_member=true;
}
union_symbol.type.set(ID_C_unnamed_object, symbol.base_name);
return new_code;
}
void cpp_typecheckt::convert_non_template_declaration(
cpp_declarationt &declaration)
{
PRECONDITION(!declaration.is_template());
// we first check if this is a typedef
typet &declaration_type=declaration.type();
bool is_typedef=declaration.is_typedef();
// the name anonymous tag types
declaration.name_anon_struct_union();
// do the type of the declaration
if(declaration.declarators().empty() || !has_auto(declaration_type))
typecheck_type(declaration_type);
// Elaborate any class template instance _unless_ we do a typedef.
// These are only elaborated on usage!
if(!is_typedef)
elaborate_class_template(declaration_type);
// mark as 'already typechecked'
if(!declaration.declarators().empty())
already_typechecked_typet::make_already_typechecked(declaration_type);
// Special treatment for anonymous unions
if(
declaration.declarators().empty() &&
((declaration.type().id() == ID_struct_tag &&
follow_tag(to_struct_tag_type(declaration.type()))
.get_bool(ID_C_is_anonymous)) ||
(declaration.type().id() == ID_union_tag &&
follow_tag(to_union_tag_type(declaration.type()))
.get_bool(ID_C_is_anonymous)) ||
declaration.type().get_bool(ID_C_is_anonymous)))
{
if(declaration.type().id() != ID_union_tag)
{
error().source_location = declaration.type().source_location();
error() << "top-level declaration does not declare anything"
<< eom;
throw 0;
}
convert_anonymous_union(declaration);
}
// do the declarators (optional)
for(auto &d : declaration.declarators())
{
// copy the declarator (we destroy the original)
cpp_declaratort declarator=d;
cpp_declarator_convertert cpp_declarator_converter(*this);
cpp_declarator_converter.is_typedef=is_typedef;
symbolt &symbol=cpp_declarator_converter.convert(
declaration_type, declaration.storage_spec(),
declaration.member_spec(), declarator);
if(!symbol.is_type && !symbol.is_extern && symbol.type.id() == ID_empty)
{
error().source_location = symbol.location;
error() << "void-typed symbol not permitted" << eom;
throw 0;
}
// any template instance to remember?
if(declaration.find(ID_C_template).is_not_nil())
{
symbol.type.set(ID_C_template, declaration.find(ID_C_template));
symbol.type.set(
ID_C_template_arguments,
declaration.find(ID_C_template_arguments));
}
// replace declarator by symbol expression
exprt tmp=cpp_symbol_expr(symbol);
d.swap(tmp);
// is there a constructor to be called for the declarator?
if(symbol.is_lvalue &&
declarator.init_args().has_operands())
{
auto constructor = cpp_constructor(
symbol.location,
cpp_symbol_expr(symbol),
declarator.init_args().operands());
if(constructor.has_value())
symbol.value = constructor.value();
else
symbol.value = nil_exprt();
}
}
}