-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathtemplate_map.cpp
262 lines (215 loc) · 5.91 KB
/
template_map.cpp
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*******************************************************************\
Module: C++ Language Type Checking
Author: Daniel Kroening, [email protected]
\*******************************************************************/
/// \file
/// C++ Language Type Checking
#include "template_map.h"
#include <util/invariant.h>
#include <util/pointer_expr.h>
#include <util/std_expr.h>
#include "cpp_template_parameter.h"
#include "cpp_template_type.h"
#include <ostream>
void template_mapt::apply(typet &type) const
{
if(type.id()==ID_array)
{
apply(to_array_type(type).element_type());
apply(to_array_type(type).size());
}
else if(type.id()==ID_pointer)
{
apply(to_pointer_type(type).base_type());
}
else if(type.id()==ID_struct ||
type.id()==ID_union)
{
for(auto &c : to_struct_union_type(type).components())
{
typet &subtype = c.type();
apply(subtype);
}
}
else if(type.id() == ID_template_parameter_symbol_type)
{
type_mapt::const_iterator m_it =
type_map.find(to_template_parameter_symbol_type(type).get_identifier());
if(m_it!=type_map.end())
{
type=m_it->second;
return;
}
}
else if(type.id()==ID_code)
{
apply(to_code_type(type).return_type());
irept::subt ¶meters=type.add(ID_parameters).get_sub();
for(auto ¶meter : parameters)
{
if(parameter.id() == ID_parameter)
apply(static_cast<typet &>(parameter.add(ID_type)));
}
}
else if(type.id()==ID_merged_type)
{
for(typet &subtype : to_type_with_subtypes(type).subtypes())
apply(subtype);
}
}
void template_mapt::apply(exprt &expr) const
{
apply(expr.type());
if(expr.id()==ID_symbol)
{
expr_mapt::const_iterator m_it =
expr_map.find(to_symbol_expr(expr).get_identifier());
if(m_it!=expr_map.end())
{
expr=m_it->second;
return;
}
}
Forall_operands(it, expr)
apply(*it);
}
exprt template_mapt::lookup(const irep_idt &identifier) const
{
type_mapt::const_iterator t_it=
type_map.find(identifier);
if(t_it!=type_map.end())
{
exprt e(ID_type);
e.type()=t_it->second;
return e;
}
expr_mapt::const_iterator e_it=
expr_map.find(identifier);
if(e_it!=expr_map.end())
return e_it->second;
return static_cast<const exprt &>(get_nil_irep());
}
typet template_mapt::lookup_type(const irep_idt &identifier) const
{
type_mapt::const_iterator t_it=
type_map.find(identifier);
if(t_it!=type_map.end())
return t_it->second;
return static_cast<const typet &>(get_nil_irep());
}
exprt template_mapt::lookup_expr(const irep_idt &identifier) const
{
expr_mapt::const_iterator e_it=
expr_map.find(identifier);
if(e_it!=expr_map.end())
return e_it->second;
return static_cast<const exprt &>(get_nil_irep());
}
void template_mapt::print(std::ostream &out) const
{
for(const auto &mapping : type_map)
out << mapping.first << " = " << mapping.second.pretty() << '\n';
for(const auto &mapping : expr_map)
out << mapping.first << " = " << mapping.second.pretty() << '\n';
}
void template_mapt::build(
const template_typet &template_type,
const cpp_template_args_tct &template_args)
{
const template_typet::template_parameterst &template_parameters=
template_type.template_parameters();
cpp_template_args_tct::argumentst instance=
template_args.arguments();
template_typet::template_parameterst::const_iterator t_it=
template_parameters.begin();
if(instance.size()<template_parameters.size())
{
// check for default parameters
for(std::size_t i=instance.size();
i<template_parameters.size();
i++)
{
const template_parametert ¶m=template_parameters[i];
if(param.has_default_argument())
instance.push_back(param.default_argument());
else
break;
}
}
// these should have been typechecked before
DATA_INVARIANT(
instance.size() == template_parameters.size(),
"template instantiation expected to match declaration");
for(cpp_template_args_tct::argumentst::const_iterator
i_it=instance.begin();
i_it!=instance.end();
i_it++, t_it++)
{
set(*t_it, *i_it);
}
}
void template_mapt::set(
const template_parametert ¶meter,
const exprt &value)
{
if(parameter.id()==ID_type)
{
if(parameter.id()!=ID_type)
UNREACHABLE; // typechecked before!
typet tmp=value.type();
irep_idt identifier=parameter.type().get(ID_identifier);
type_map[identifier]=tmp;
}
else
{
// must be non-type
if(value.id()==ID_type)
UNREACHABLE; // typechecked before!
irep_idt identifier=parameter.get(ID_identifier);
expr_map[identifier]=value;
}
}
void template_mapt::build_unassigned(
const template_typet &template_type)
{
for(const auto &t : template_type.template_parameters())
{
if(t.id()==ID_type)
{
typet tmp(ID_unassigned);
tmp.set(ID_identifier, t.type().get(ID_identifier));
tmp.add_source_location()=t.source_location();
type_map[t.type().get(ID_identifier)]=tmp;
}
else
{
exprt tmp(ID_unassigned, t.type());
tmp.set(ID_identifier, t.get(ID_identifier));
tmp.add_source_location()=t.source_location();
expr_map[t.get(ID_identifier)]=tmp;
}
}
}
cpp_template_args_tct template_mapt::build_template_args(
const template_typet &template_type) const
{
const template_typet::template_parameterst &template_parameters=
template_type.template_parameters();
cpp_template_args_tct template_args;
template_args.arguments().resize(template_parameters.size());
for(std::size_t i=0; i<template_parameters.size(); i++)
{
const template_parametert &t=template_parameters[i];
if(t.id()==ID_type)
{
template_args.arguments()[i]=
exprt(ID_type, lookup_type(t.type().get(ID_identifier)));
}
else
{
template_args.arguments()[i]=
lookup_expr(t.get(ID_identifier));
}
}
return template_args;
}