Skip to content

Commit cc0a2aa

Browse files
authored
Merge pull request #1502 from diffblue/smv-module-map
SMV: module map in the parse tree now uses base_name
2 parents ee3d253 + 1431945 commit cc0a2aa

File tree

8 files changed

+90
-58
lines changed

8 files changed

+90
-58
lines changed

src/smvlang/expr2smv.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ std::string type2smv(const typet &type, const namespacet &ns)
10601060
}
10611061
else if(type.id() == ID_smv_module_instance)
10621062
{
1063-
auto code = id2string(to_smv_module_instance_type(type).identifier());
1063+
auto code = id2string(to_smv_module_instance_type(type).base_name());
10641064
const exprt &e=(exprt &)type;
10651065
if(e.has_operands())
10661066
{

src/smvlang/parser.y

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ Function: new_module
145145
static smv_parse_treet::modulet &new_module(YYSTYPE &location, YYSTYPE &module_name)
146146
{
147147
auto base_name = stack_expr(module_name).id_string();
148-
const std::string identifier=smv_module_symbol(base_name);
148+
const auto identifier=smv_module_symbol(base_name);
149149
PARSER.parse_tree.module_list.push_back(smv_parse_treet::modulet{});
150150
auto &module=PARSER.parse_tree.module_list.back();
151-
PARSER.parse_tree.module_map[identifier] = --PARSER.parse_tree.module_list.end();
152-
module.name = identifier;
151+
PARSER.parse_tree.module_map[base_name] = --PARSER.parse_tree.module_list.end();
152+
module.identifier = identifier;
153153
module.base_name = base_name;
154154
module.source_location = stack_expr(location).source_location();
155155
PARSER.module = &module;
@@ -592,14 +592,12 @@ module_type_specifier:
592592
module_name
593593
{
594594
init($$, ID_smv_module_instance);
595-
to_smv_module_instance_type(stack_type($$)).identifier(
596-
smv_module_symbol(stack_expr($1).id_string()));
595+
to_smv_module_instance_type(stack_type($$)).base_name(stack_expr($1).id());
597596
}
598597
| module_name '(' parameter_list ')'
599598
{
600599
init($$, ID_smv_module_instance);
601-
to_smv_module_instance_type(stack_type($$)).identifier(
602-
smv_module_symbol(stack_expr($1).id_string()));
600+
to_smv_module_instance_type(stack_type($$)).base_name(stack_expr($1).id());
603601
stack_expr($$).operands().swap(stack_expr($3).operands());
604602
}
605603
;

src/smvlang/smv_ebmc_language.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ std::optional<transition_systemt> smv_ebmc_languaget::transition_system()
8080

8181
for(const auto &module : parse_tree.module_list)
8282
show_modules.modules.emplace_back(
83-
module.name, module.base_name, "SMV", module.source_location);
83+
module.identifier, module.base_name, "SMV", module.source_location);
8484

8585
auto filename = cmdline.value_opt("outfile").value_or("-");
8686
output_filet output_file{filename};

src/smvlang/smv_language.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void smv_languaget::dependencies(
6262
const std::string &module,
6363
std::set<std::string> &module_set)
6464
{
65-
auto m_it = smv_parse_tree.module_map.find(module);
65+
auto m_it = smv_parse_tree.module_map.find(smv_module_base_name(module));
6666

6767
if(m_it == smv_parse_tree.module_map.end())
6868
return;
@@ -71,8 +71,11 @@ void smv_languaget::dependencies(
7171

7272
for(auto &element : smv_module.elements)
7373
if(element.is_var() && element.expr.type().id() == ID_smv_module_instance)
74-
module_set.insert(id2string(
75-
to_smv_module_instance_type(element.expr.type()).identifier()));
74+
{
75+
auto base_name =
76+
to_smv_module_instance_type(element.expr.type()).base_name();
77+
module_set.insert(id2string(smv_module_symbol(base_name)));
78+
}
7679
}
7780

7881
/*******************************************************************\
@@ -90,7 +93,7 @@ Function: smv_languaget::modules_provided
9093
void smv_languaget::modules_provided(std::set<std::string> &module_set)
9194
{
9295
for(const auto &module : smv_parse_tree.module_list)
93-
module_set.insert(id2string(module.name));
96+
module_set.insert(id2string(module.identifier));
9497
}
9598

9699
/*******************************************************************\

src/smvlang/smv_parse_tree.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class smv_parse_treet
3030
struct modulet
3131
{
3232
source_locationt source_location;
33-
irep_idt name, base_name;
33+
irep_idt identifier, base_name;
3434
std::vector<irep_idt> parameters;
3535

3636
struct elementt
@@ -315,6 +315,7 @@ class smv_parse_treet
315315
using module_listt = std::list<modulet>;
316316
module_listt module_list;
317317

318+
// map from module base names into the module list
318319
using module_mapt =
319320
std::unordered_map<irep_idt, module_listt::iterator, irep_id_hash>;
320321
module_mapt module_map;

src/smvlang/smv_typecheck.cpp

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Author: Daniel Kroening, kroening@kroening.com
1414
#include <util/expr_util.h>
1515
#include <util/mathematical_expr.h>
1616
#include <util/namespace.h>
17+
#include <util/prefix.h>
1718
#include <util/std_expr.h>
1819
#include <util/typecheck.h>
1920

@@ -32,13 +33,13 @@ class smv_typecheckt:public typecheckt
3233
smv_typecheckt(
3334
smv_parse_treet &_smv_parse_tree,
3435
symbol_table_baset &_symbol_table,
35-
const std::string &_module,
36+
const std::string &_module_identifier,
3637
bool _do_spec,
3738
message_handlert &_message_handler)
3839
: typecheckt(_message_handler),
3940
smv_parse_tree(_smv_parse_tree),
4041
symbol_table(_symbol_table),
41-
module(_module),
42+
module_identifier(_module_identifier),
4243
do_spec(_do_spec)
4344
{
4445
nondet_count=0;
@@ -80,7 +81,7 @@ class smv_typecheckt:public typecheckt
8081
protected:
8182
smv_parse_treet &smv_parse_tree;
8283
symbol_table_baset &symbol_table;
83-
const std::string &module;
84+
const std::string &module_identifier;
8485
bool do_spec;
8586

8687
void check_type(typet &);
@@ -104,7 +105,7 @@ class smv_typecheckt:public typecheckt
104105

105106
void instantiate(
106107
smv_parse_treet::modulet &,
107-
const irep_idt &identifier,
108+
const irep_idt &module_base_name,
108109
const irep_idt &instance,
109110
const exprt::operandst &arguments,
110111
const source_locationt &);
@@ -216,7 +217,7 @@ void smv_typecheckt::convert_ports(
216217
ports.push_back(exprt(ID_symbol));
217218
ports.back().set(
218219
ID_identifier,
219-
id2string(smv_module.name) + "::var::" + id2string(port_name));
220+
id2string(smv_module.identifier) + "::var::" + id2string(port_name));
220221
}
221222
}
222223

@@ -249,12 +250,13 @@ void smv_typecheckt::flatten_hierarchy(smv_parse_treet::modulet &smv_module)
249250
for(auto &argument : instance.arguments())
250251
convert(argument);
251252

253+
// the base name of the instance, not the module
252254
auto instance_base_name =
253255
to_smv_identifier_expr(element.expr).identifier();
254256

255257
instantiate(
256258
smv_module,
257-
instance.identifier(),
259+
instance.base_name(),
258260
instance_base_name,
259261
instance.arguments(),
260262
instance.source_location());
@@ -276,18 +278,18 @@ Function: smv_typecheckt::instantiate
276278

277279
void smv_typecheckt::instantiate(
278280
smv_parse_treet::modulet &smv_module,
279-
const irep_idt &identifier,
281+
const irep_idt &module_base_name,
280282
const irep_idt &instance,
281283
const exprt::operandst &arguments,
282284
const source_locationt &location)
283285
{
284286
// Find the module
285-
auto module_it = smv_parse_tree.module_map.find(identifier);
287+
auto module_it = smv_parse_tree.module_map.find(module_base_name);
286288

287289
if(module_it == smv_parse_tree.module_map.end())
288290
{
289291
throw errort().with_location(location)
290-
<< "submodule `" << identifier << "' not found";
292+
<< "submodule `" << module_base_name << "' not found";
291293
}
292294

293295
const auto &instantiated_module = *module_it->second;
@@ -297,7 +299,7 @@ void smv_typecheckt::instantiate(
297299
if(parameters.size() != arguments.size())
298300
{
299301
throw errort().with_location(location)
300-
<< "submodule `" << identifier << "' has wrong number of arguments";
302+
<< "submodule `" << module_base_name << "' has wrong number of arguments";
301303
}
302304

303305
rename_mapt parameter_map;
@@ -727,7 +729,7 @@ void smv_typecheckt::typecheck_expr_rec(exprt &expr, modet mode, bool next)
727729

728730
if(mode == INIT || next)
729731
{
730-
if(symbol.module==module)
732+
if(symbol.module == module_identifier)
731733
{
732734
symbol.is_input=false;
733735
symbol.is_state_var=true;
@@ -1716,7 +1718,7 @@ void smv_typecheckt::convert_expr_to(exprt &expr, const typet &dest_type)
17161718
// sets can be assigned to scalars, which yields a nondeterministic
17171719
// choice
17181720
std::string identifier =
1719-
module + "::var::" + std::to_string(nondet_count++);
1721+
module_identifier + "::var::" + std::to_string(nondet_count++);
17201722

17211723
expr.set(ID_identifier, identifier);
17221724
expr.set("#smv_nondet_choice", true);
@@ -1877,7 +1879,7 @@ void smv_typecheckt::convert(exprt &expr)
18771879
if(
18781880
smv_parse_tree.enum_set.find(identifier) == smv_parse_tree.enum_set.end())
18791881
{
1880-
std::string id = module + "::var::" + identifier;
1882+
std::string id = module_identifier + "::var::" + identifier;
18811883

18821884
expr.set(ID_identifier, id);
18831885
expr.id(ID_symbol);
@@ -1927,8 +1929,8 @@ void smv_typecheckt::convert(exprt &expr)
19271929
<< "expected operand here";
19281930
}
19291931

1930-
std::string identifier=
1931-
module+"::var::"+std::to_string(nondet_count++);
1932+
std::string identifier =
1933+
module_identifier + "::var::" + std::to_string(nondet_count++);
19321934

19331935
expr.set(ID_identifier, identifier);
19341936
expr.set("#smv_nondet_choice", true);
@@ -2249,7 +2251,8 @@ void smv_typecheckt::create_var_symbols(
22492251
{
22502252
irep_idt base_name = merge_complex_identifier(element.expr);
22512253
auto location = element.expr.source_location();
2252-
irep_idt identifier = module + "::var::" + id2string(base_name);
2254+
irep_idt identifier =
2255+
module_identifier + "::var::" + id2string(base_name);
22532256

22542257
auto symbol_ptr = symbol_table.lookup(identifier);
22552258
if(symbol_ptr != nullptr)
@@ -2266,10 +2269,10 @@ void smv_typecheckt::create_var_symbols(
22662269

22672270
symbolt symbol{identifier, std::move(type), mode};
22682271

2269-
symbol.module = modulep->name;
2272+
symbol.module = modulep->identifier;
22702273
symbol.base_name = base_name;
22712274

2272-
if(module == "smv::main")
2275+
if(module_identifier == "smv::main")
22732276
symbol.pretty_name = symbol.base_name;
22742277
else
22752278
symbol.pretty_name = strip_smv_prefix(symbol.name);
@@ -2289,7 +2292,8 @@ void smv_typecheckt::create_var_symbols(
22892292
{
22902293
irep_idt base_name = merge_complex_identifier(element.lhs());
22912294
auto location = to_equal_expr(element.expr).lhs().source_location();
2292-
irep_idt identifier = module + "::var::" + id2string(base_name);
2295+
irep_idt identifier =
2296+
module_identifier + "::var::" + id2string(base_name);
22932297

22942298
auto symbol_ptr = symbol_table.lookup(identifier);
22952299
if(symbol_ptr != nullptr)
@@ -2305,10 +2309,10 @@ void smv_typecheckt::create_var_symbols(
23052309
symbolt symbol{identifier, std::move(type), mode};
23062310

23072311
symbol.mode = mode;
2308-
symbol.module = modulep->name;
2312+
symbol.module = modulep->identifier;
23092313
symbol.base_name = base_name;
23102314

2311-
if(module == "smv::main")
2315+
if(module_identifier == "smv::main")
23122316
symbol.pretty_name = symbol.base_name;
23132317
else
23142318
symbol.pretty_name = strip_smv_prefix(symbol.name);
@@ -2323,7 +2327,8 @@ void smv_typecheckt::create_var_symbols(
23232327
else if(element.is_enum())
23242328
{
23252329
irep_idt base_name = to_smv_identifier_expr(element.expr).identifier();
2326-
irep_idt identifier = module + "::var::" + id2string(base_name);
2330+
irep_idt identifier =
2331+
module_identifier + "::var::" + id2string(base_name);
23272332

23282333
auto symbol_ptr = symbol_table.lookup(identifier);
23292334
if(symbol_ptr != nullptr)
@@ -2537,7 +2542,7 @@ void smv_typecheckt::convert(smv_parse_treet::modulet &smv_module)
25372542

25382543
module_symbol.base_name=smv_module.base_name;
25392544
module_symbol.pretty_name=smv_module.base_name;
2540-
module_symbol.name=smv_module.name;
2545+
module_symbol.name = smv_module.identifier;
25412546
module_symbol.module=module_symbol.name;
25422547
module_symbol.type=typet(ID_module);
25432548
module_symbol.mode="SMV";
@@ -2609,17 +2614,17 @@ void smv_typecheckt::convert(smv_parse_treet::modulet &smv_module)
26092614
else
26102615
spec_symbol.base_name = "spec" + std::to_string(nr++);
26112616

2612-
spec_symbol.name =
2613-
id2string(smv_module.name) + "::" + id2string(spec_symbol.base_name);
2614-
spec_symbol.module = smv_module.name;
2617+
spec_symbol.name = id2string(smv_module.identifier) +
2618+
"::" + id2string(spec_symbol.base_name);
2619+
spec_symbol.module = smv_module.identifier;
26152620
spec_symbol.type = bool_typet();
26162621
spec_symbol.is_property = true;
26172622
spec_symbol.mode = "SMV";
26182623
spec_symbol.value = element.expr;
26192624
spec_symbol.location = element.location;
26202625
spec_symbol.location.set_comment(to_string(element.expr));
26212626

2622-
if(smv_module.name == "smv::main")
2627+
if(smv_module.identifier == "smv::main")
26232628
spec_symbol.pretty_name = spec_symbol.base_name;
26242629
else
26252630
spec_symbol.pretty_name = strip_smv_prefix(spec_symbol.name);
@@ -2632,8 +2637,9 @@ void smv_typecheckt::convert(smv_parse_treet::modulet &smv_module)
26322637
}
26332638

26342639
// lowering
2635-
for(auto v_it = symbol_table.symbol_module_map.lower_bound(smv_module.name);
2636-
v_it != symbol_table.symbol_module_map.upper_bound(smv_module.name);
2640+
for(auto v_it =
2641+
symbol_table.symbol_module_map.lower_bound(smv_module.identifier);
2642+
v_it != symbol_table.symbol_module_map.upper_bound(smv_module.identifier);
26372643
v_it++)
26382644
{
26392645
auto &symbol = symbol_table.get_writeable_ref(v_it->second);
@@ -2656,18 +2662,19 @@ Function: smv_typecheckt::typecheck
26562662

26572663
void smv_typecheckt::typecheck()
26582664
{
2659-
if(module != "smv::main")
2665+
if(module_identifier != "smv::main")
26602666
return;
26612667

26622668
// check all modules for duplicate identifiers
26632669
for(auto &module : smv_parse_tree.module_list)
26642670
variable_checks(module);
26652671

2666-
auto it = smv_parse_tree.module_map.find(module);
2672+
auto module_base_name = smv_module_base_name(module_identifier);
2673+
auto it = smv_parse_tree.module_map.find(module_base_name);
26672674

26682675
if(it == smv_parse_tree.module_map.end())
26692676
{
2670-
throw errort() << "failed to find module " << module;
2677+
throw errort() << "failed to find module " << module_base_name;
26712678
}
26722679

26732680
convert(*it->second);
@@ -2688,12 +2695,12 @@ Function: smv_typecheck
26882695
bool smv_typecheck(
26892696
smv_parse_treet &smv_parse_tree,
26902697
symbol_table_baset &symbol_table,
2691-
const std::string &module,
2698+
const std::string &module_identifier,
26922699
message_handlert &message_handler,
26932700
bool do_spec)
26942701
{
26952702
smv_typecheckt smv_typecheck(
2696-
smv_parse_tree, symbol_table, module, do_spec, message_handler);
2703+
smv_parse_tree, symbol_table, module_identifier, do_spec, message_handler);
26972704
return smv_typecheck.typecheck_main();
26982705
}
26992706

@@ -2709,7 +2716,25 @@ Function: smv_module_symbol
27092716
27102717
\*******************************************************************/
27112718

2712-
std::string smv_module_symbol(const std::string &module)
2719+
irep_idt smv_module_symbol(const irep_idt &base_name)
27132720
{
2714-
return "smv::"+module;
2721+
return "smv::" + id2string(base_name);
2722+
}
2723+
2724+
/*******************************************************************\
2725+
2726+
Function: smv_module_base_name
2727+
2728+
Inputs:
2729+
2730+
Outputs:
2731+
2732+
Purpose:
2733+
2734+
\*******************************************************************/
2735+
2736+
irep_idt smv_module_base_name(const irep_idt &identifier)
2737+
{
2738+
PRECONDITION(has_prefix(id2string(identifier), "smv::"));
2739+
return id2string(identifier).substr(5, std::string::npos);
27152740
}

0 commit comments

Comments
 (0)