Skip to content

Commit 7792612

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 228f512 commit 7792612

File tree

3 files changed

+39
-42
lines changed

3 files changed

+39
-42
lines changed

regression/smt2_solver/basic-bv1/test.desc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
KNOWNBUG
1+
CORE
22
basic-bv1.smt2
33

44
^EXIT=0$

src/solvers/smt2/smt2_parser.cpp

+36-39
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();
@@ -1131,12 +1139,9 @@ smt2_parsert::function_signature_definition()
11311139
throw error("expected symbol in parameter");
11321140

11331141
irep_idt id = smt2_tokenizer.get_buffer();
1134-
parameters.push_back(id);
11351142
domain.push_back(sort());
11361143

1137-
auto &entry=id_map[id];
1138-
entry.type = domain.back();
1139-
entry.definition=nil_exprt();
1144+
parameters.push_back(add_fresh_id(id, exprt(ID_nil, domain.back())));
11401145

11411146
if(next_token() != smt2_tokenizert::CLOSE)
11421147
throw error("expected ')' at end of parameter");
@@ -1196,12 +1201,8 @@ void smt2_parsert::command(const std::string &c)
11961201
irep_idt id = smt2_tokenizer.get_buffer();
11971202
auto type = sort();
11981203

1199-
if(id_map.find(id)!=id_map.end())
1204+
if(add_fresh_id(id, exprt(ID_nil, type)) != id)
12001205
throw error() << "identifier `" << id << "' defined twice";
1201-
1202-
auto &entry = id_map[id];
1203-
entry.type = type;
1204-
entry.definition = nil_exprt();
12051206
}
12061207
else if(c=="declare-fun")
12071208
{
@@ -1211,12 +1212,8 @@ void smt2_parsert::command(const std::string &c)
12111212
irep_idt id = smt2_tokenizer.get_buffer();
12121213
auto type = function_signature_declaration();
12131214

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

12261223
const irep_idt id = smt2_tokenizer.get_buffer();
12271224

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

@@ -1240,23 +1234,25 @@ void smt2_parsert::command(const std::string &c)
12401234
}
12411235

12421236
// create the entry
1243-
auto &entry = id_map[id];
1244-
entry.type = type;
1245-
entry.definition = value;
1237+
if(add_fresh_id(id, value) != id)
1238+
throw error() << "identifier `" << id << "' defined twice";
12461239
}
12471240
else if(c=="define-fun")
12481241
{
12491242
if(next_token() != smt2_tokenizert::SYMBOL)
12501243
throw error("expected a symbol after define-fun");
12511244

1252-
const irep_idt id = smt2_tokenizer.get_buffer();
1245+
// save the renaming map
1246+
renaming_mapt old_renaming_map = renaming_map;
12531247

1254-
if(id_map.find(id)!=id_map.end())
1255-
throw error() << "identifier `" << id << "' defined twice";
1248+
const irep_idt id = smt2_tokenizer.get_buffer();
12561249

12571250
const auto signature = function_signature_definition();
12581251
const auto body = expression();
12591252

1253+
// restore renamings
1254+
std::swap(renaming_map, old_renaming_map);
1255+
12601256
// check type of body
12611257
if(signature.type.id() == ID_mathematical_function)
12621258
{
@@ -1276,10 +1272,11 @@ void smt2_parsert::command(const std::string &c)
12761272
}
12771273

12781274
// create the entry
1279-
auto &entry = id_map[id];
1280-
entry.type = signature.type;
1281-
entry.parameters = signature.parameters;
1282-
entry.definition = body;
1275+
if(add_fresh_id(id, body) != id)
1276+
throw error() << "identifier `" << id << "' defined twice";
1277+
1278+
id_map.at(id).type = signature.type;
1279+
id_map.at(id).parameters = signature.parameters;
12831280
}
12841281
else if(c=="exit")
12851282
{

src/solvers/smt2/smt2_parser.h

+2-2
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)