Skip to content

Commit c638239

Browse files
committed
smt2 id_map: Do not use nil_typet to detect map insert
Use std::map's emplace API for that purpose, and while at it also refactor all inserts into id_map.
1 parent 85d61b5 commit c638239

File tree

2 files changed

+32
-40
lines changed

2 files changed

+32
-40
lines changed

src/solvers/smt2/smt2_parser.cpp

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -118,22 +118,33 @@ exprt::operandst smt2_parsert::operands()
118118
return result;
119119
}
120120

121-
irep_idt smt2_parsert::get_fresh_id(const irep_idt &id)
121+
irep_idt smt2_parsert::add_fresh_id(const irep_idt &id, const exprt &expr)
122122
{
123-
if(id_map[id].type.is_nil())
123+
if(id_map
124+
.emplace(
125+
std::piecewise_construct,
126+
std::forward_as_tuple(id),
127+
std::forward_as_tuple(expr))
128+
.second)
129+
{
124130
return id; // id not yet used
131+
}
125132

126133
auto &count=renaming_counters[id];
127134
irep_idt new_id;
128135
do
129136
{
130137
new_id=id2string(id)+'#'+std::to_string(count);
131138
count++;
132-
}
133-
while(id_map.find(new_id)!=id_map.end());
139+
} while(!id_map
140+
.emplace(
141+
std::piecewise_construct,
142+
std::forward_as_tuple(new_id),
143+
std::forward_as_tuple(expr))
144+
.second);
134145

135146
// record renaming
136-
renaming_map[id]=new_id;
147+
renaming_map[id] = new_id;
137148

138149
return new_id;
139150
}
@@ -184,10 +195,7 @@ exprt smt2_parsert::let_expression()
184195
for(auto &b : bindings)
185196
{
186197
// get a fresh id for it
187-
b.first=get_fresh_id(b.first);
188-
auto &entry=id_map[b.first];
189-
entry.type=b.second.type();
190-
entry.definition=b.second;
198+
b.first = add_fresh_id(b.first, b.second);
191199
}
192200

193201
exprt expr=expression();
@@ -246,9 +254,9 @@ exprt smt2_parsert::quantifier_expression(irep_idt id)
246254
// go forwards, add to id_map
247255
for(const auto &b : bindings)
248256
{
249-
auto &entry=id_map[b.get_identifier()];
250-
entry.type=b.type();
251-
entry.definition=nil_exprt();
257+
const irep_idt id =
258+
add_fresh_id(b.get_identifier(), exprt(ID_nil, b.type()));
259+
CHECK_RETURN(id == b.get_identifier());
252260
}
253261

254262
exprt expr=expression();
@@ -1134,9 +1142,8 @@ smt2_parsert::function_signature_definition()
11341142
parameters.push_back(id);
11351143
domain.push_back(sort());
11361144

1137-
auto &entry=id_map[id];
1138-
entry.type = domain.back();
1139-
entry.definition=nil_exprt();
1145+
if(add_fresh_id(id, exprt(ID_nil, domain.back())) != id)
1146+
CHECK_RETURN(false);
11401147

11411148
if(next_token() != smt2_tokenizert::CLOSE)
11421149
throw error("expected ')' at end of parameter");
@@ -1196,12 +1203,8 @@ void smt2_parsert::command(const std::string &c)
11961203
irep_idt id = smt2_tokenizer.get_buffer();
11971204
auto type = sort();
11981205

1199-
if(id_map.find(id)!=id_map.end())
1206+
if(add_fresh_id(id, exprt(ID_nil, type)) != id)
12001207
throw error() << "identifier `" << id << "' defined twice";
1201-
1202-
auto &entry = id_map[id];
1203-
entry.type = type;
1204-
entry.definition = nil_exprt();
12051208
}
12061209
else if(c=="declare-fun")
12071210
{
@@ -1211,12 +1214,8 @@ void smt2_parsert::command(const std::string &c)
12111214
irep_idt id = smt2_tokenizer.get_buffer();
12121215
auto type = function_signature_declaration();
12131216

1214-
if(id_map.find(id)!=id_map.end())
1217+
if(add_fresh_id(id, exprt(ID_nil, type)) != id)
12151218
throw error() << "identifier `" << id << "' defined twice";
1216-
1217-
auto &entry = id_map[id];
1218-
entry.type = type;
1219-
entry.definition = nil_exprt();
12201219
}
12211220
else if(c == "define-const")
12221221
{
@@ -1225,9 +1224,6 @@ void smt2_parsert::command(const std::string &c)
12251224

12261225
const irep_idt id = smt2_tokenizer.get_buffer();
12271226

1228-
if(id_map.find(id) != id_map.end())
1229-
throw error() << "identifier `" << id << "' defined twice";
1230-
12311227
const auto type = sort();
12321228
const auto value = expression();
12331229

@@ -1240,9 +1236,8 @@ void smt2_parsert::command(const std::string &c)
12401236
}
12411237

12421238
// create the entry
1243-
auto &entry = id_map[id];
1244-
entry.type = type;
1245-
entry.definition = value;
1239+
if(add_fresh_id(id, value) != id)
1240+
throw error() << "identifier `" << id << "' defined twice";
12461241
}
12471242
else if(c=="define-fun")
12481243
{
@@ -1251,9 +1246,6 @@ void smt2_parsert::command(const std::string &c)
12511246

12521247
const irep_idt id = smt2_tokenizer.get_buffer();
12531248

1254-
if(id_map.find(id)!=id_map.end())
1255-
throw error() << "identifier `" << id << "' defined twice";
1256-
12571249
const auto signature = function_signature_definition();
12581250
const auto body = expression();
12591251

@@ -1276,10 +1268,10 @@ void smt2_parsert::command(const std::string &c)
12761268
}
12771269

12781270
// create the entry
1279-
auto &entry = id_map[id];
1280-
entry.type = signature.type;
1281-
entry.parameters = signature.parameters;
1282-
entry.definition = body;
1271+
if(add_fresh_id(id, body) != id)
1272+
throw error() << "identifier `" << id << "' defined twice";
1273+
1274+
id_map.at(id).parameters = signature.parameters;
12831275
}
12841276
else if(c=="exit")
12851277
{

src/solvers/smt2/smt2_parser.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class smt2_parsert
3333

3434
struct idt
3535
{
36-
idt():type(nil_typet())
36+
explicit idt(const exprt &expr) : type(expr.type()), definition(expr)
3737
{
3838
}
3939

@@ -84,7 +84,7 @@ class smt2_parsert
8484
renaming_mapt renaming_map;
8585
using renaming_counterst=std::map<irep_idt, unsigned>;
8686
renaming_counterst renaming_counters;
87-
irep_idt get_fresh_id(const irep_idt &);
87+
irep_idt add_fresh_id(const irep_idt &, const exprt &);
8888
irep_idt rename_id(const irep_idt &) const;
8989

9090
struct signature_with_parameter_idst

0 commit comments

Comments
 (0)