@@ -131,30 +131,6 @@ exprt::operandst smt2_parsert::operands()
131131 return result;
132132}
133133
134- irep_idt smt2_parsert::add_fresh_id (
135- const irep_idt &id,
136- idt::kindt kind,
137- const exprt &expr)
138- {
139- auto &count=renaming_counters[id];
140- irep_idt new_id;
141- do
142- {
143- new_id=id2string (id)+' #' +std::to_string (count);
144- count++;
145- } while (!id_map
146- .emplace (
147- std::piecewise_construct,
148- std::forward_as_tuple (new_id),
149- std::forward_as_tuple (kind, expr))
150- .second );
151-
152- // record renaming
153- renaming_map[id] = new_id;
154-
155- return new_id;
156- }
157-
158134void smt2_parsert::add_unique_id (const irep_idt &id, const exprt &expr)
159135{
160136 if (!id_map
@@ -169,16 +145,6 @@ void smt2_parsert::add_unique_id(const irep_idt &id, const exprt &expr)
169145 }
170146}
171147
172- irep_idt smt2_parsert::rename_id (const irep_idt &id) const
173- {
174- auto it=renaming_map.find (id);
175-
176- if (it==renaming_map.end ())
177- return id;
178- else
179- return it->second ;
180- }
181-
182148exprt smt2_parsert::let_expression ()
183149{
184150 if (next_token () != smt2_tokenizert::OPEN)
@@ -254,7 +220,7 @@ exprt smt2_parsert::quantifier_expression(irep_idt id)
254220 if (next_token () != smt2_tokenizert::OPEN)
255221 throw error () << " expected bindings after " << id;
256222
257- std::vector<symbol_exprt> bindings;
223+ binding_exprt::variablest bindings;
258224
259225 while (smt2_tokenizer.peek () == smt2_tokenizert::OPEN)
260226 {
@@ -276,18 +242,23 @@ exprt smt2_parsert::quantifier_expression(irep_idt id)
276242 if (next_token () != smt2_tokenizert::CLOSE)
277243 throw error (" expected ')' at end of bindings" );
278244
279- // save the renaming map
280- renaming_mapt old_renaming_map = renaming_map ;
245+ // we may hide identifiers in outer scopes
246+ std::vector<std::pair<irep_idt, idt>> saved_ids ;
281247
282- // go forwards, add to id_map, renaming if need be
248+ // add the bindings to the id_map
283249 for (auto &b : bindings)
284250 {
285- const irep_idt id =
286- add_fresh_id (b.get_identifier (), idt::BINDING, exprt (ID_nil, b.type ()));
287-
288- b.set_identifier (id);
251+ auto insert_result =
252+ id_map.insert ({b.get_identifier (), idt{idt::BINDING, b.type ()}});
253+ if (!insert_result.second ) // already there
254+ {
255+ auto &id_entry = *insert_result.first ;
256+ saved_ids.emplace_back (id_entry.first , std::move (id_entry.second ));
257+ id_entry.second = idt{idt::BINDING, b.type ()};
258+ }
289259 }
290260
261+ // now parse, with bindings in place
291262 exprt expr=expression ();
292263
293264 if (next_token () != smt2_tokenizert::CLOSE)
@@ -299,8 +270,9 @@ exprt smt2_parsert::quantifier_expression(irep_idt id)
299270 for (const auto &b : bindings)
300271 id_map.erase (b.get_identifier ());
301272
302- // restore renaming map
303- renaming_map = old_renaming_map;
273+ // restore any previous ids
274+ for (auto &saved_id : saved_ids)
275+ id_map.insert (std::move (saved_id));
304276
305277 // go backwards, build quantified expression
306278 for (auto r_it=bindings.rbegin (); r_it!=bindings.rend (); r_it++)
@@ -603,20 +575,18 @@ exprt smt2_parsert::function_application()
603575 auto op = operands ();
604576
605577 // rummage through id_map
606- const irep_idt final_id = rename_id (id);
607- auto id_it = id_map.find (final_id);
578+ auto id_it = id_map.find (id);
608579 if (id_it != id_map.end ())
609580 {
610581 if (id_it->second .type .id () == ID_mathematical_function)
611582 {
612- return function_application (
613- symbol_exprt (final_id, id_it->second .type ), op);
583+ return function_application (symbol_exprt (id, id_it->second .type ), op);
614584 }
615585 else
616- return symbol_exprt (final_id , id_it->second .type );
586+ return symbol_exprt (id , id_it->second .type );
617587 }
618-
619- throw error () << " unknown function symbol '" << id << ' \' ' ;
588+ else
589+ throw error () << " unknown function symbol '" << id << ' \' ' ;
620590 }
621591 break ;
622592
@@ -916,11 +886,10 @@ exprt smt2_parsert::expression()
916886 return e_it->second ();
917887
918888 // rummage through id_map
919- const irep_idt final_id = rename_id (identifier);
920- auto id_it = id_map.find (final_id);
889+ auto id_it = id_map.find (identifier);
921890 if (id_it != id_map.end ())
922891 {
923- symbol_exprt symbol_expr (final_id , id_it->second .type );
892+ symbol_exprt symbol_expr (identifier , id_it->second .type );
924893 if (smt2_tokenizer.token_is_quoted_symbol ())
925894 symbol_expr.set (ID_C_quoted, true );
926895 return std::move (symbol_expr);
@@ -1393,9 +1362,7 @@ smt2_parsert::function_signature_definition()
13931362
13941363 irep_idt id = smt2_tokenizer.get_buffer ();
13951364 domain.push_back (sort ());
1396-
1397- parameters.push_back (
1398- add_fresh_id (id, idt::PARAMETER, exprt (ID_nil, domain.back ())));
1365+ parameters.push_back (id);
13991366
14001367 if (next_token () != smt2_tokenizert::CLOSE)
14011368 throw error (" expected ')' at end of parameter" );
@@ -1497,16 +1464,35 @@ void smt2_parsert::setup_commands()
14971464 if (next_token () != smt2_tokenizert::SYMBOL)
14981465 throw error (" expected a symbol after define-fun" );
14991466
1500- // save the renaming map
1501- renaming_mapt old_renaming_map = renaming_map;
1502-
15031467 const irep_idt id = smt2_tokenizer.get_buffer ();
15041468
15051469 const auto signature = function_signature_definition ();
1470+
1471+ // put the parameters into the scope and take care of hiding
1472+ std::vector<std::pair<irep_idt, idt>> hidden_ids;
1473+
1474+ for (const auto &pair : signature.ids_and_types ())
1475+ {
1476+ auto insert_result =
1477+ id_map.insert ({pair.first , idt{idt::PARAMETER, pair.second }});
1478+ if (!insert_result.second ) // already there
1479+ {
1480+ auto &id_entry = *insert_result.first ;
1481+ hidden_ids.emplace_back (id_entry.first , std::move (id_entry.second ));
1482+ id_entry.second = idt{idt::PARAMETER, pair.second };
1483+ }
1484+ }
1485+
1486+ // now parse body with parameter ids in place
15061487 const auto body = expression ();
15071488
1508- // restore renamings
1509- std::swap (renaming_map, old_renaming_map);
1489+ // remove the parameter ids
1490+ for (auto &id : signature.parameters )
1491+ id_map.erase (id);
1492+
1493+ // restore the hidden ids, if any
1494+ for (auto &hidden_id : hidden_ids)
1495+ id_map.insert (std::move (hidden_id));
15101496
15111497 // check type of body
15121498 if (signature.type .id () == ID_mathematical_function)
0 commit comments