Skip to content

Commit 627bb47

Browse files
committed
SMV: introduce smv_identifier_exprt
This introduces a class for the identifiers used in the SMV parse tree. The type checker subsequently turns them into symbols, when this is appropriate.
1 parent eb37218 commit 627bb47

File tree

4 files changed

+52
-13
lines changed

4 files changed

+52
-13
lines changed

src/hw_cbmc_irep_ids.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ IREP_ID_ONE(smv_extend)
2626
IREP_ID_ONE(smv_max)
2727
IREP_ID_ONE(smv_min)
2828
IREP_ID_ONE(smv_next)
29+
IREP_ID_ONE(smv_identifier)
2930
IREP_ID_ONE(smv_iff)
3031
IREP_ID_TWO(C_smv_iff, "#smv_iff")
3132
IREP_ID_ONE(smv_resize)

src/smvlang/parser.y

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ Function: merge_complex_identifier
142142

143143
irep_idt merge_complex_identifier(const exprt &expr)
144144
{
145-
if(expr.id() == ID_symbol)
146-
return to_symbol_expr(expr).get_identifier();
145+
if(expr.id() == ID_smv_identifier)
146+
return expr.get(ID_identifier);
147147
else if(expr.id() == ID_member)
148148
{
149149
auto &member_expr = to_member_expr(expr);
@@ -677,7 +677,7 @@ enum_element: IDENTIFIER_Token
677677
$$=$1;
678678
PARSER.module->enum_set.insert(stack_expr($1).id_string());
679679

680-
exprt expr(ID_symbol);
680+
exprt expr(ID_smv_identifier);
681681
expr.set(ID_identifier, stack_expr($1).id());
682682
PARSER.set_source_location(expr);
683683
PARSER.module->add_enum(std::move(expr));
@@ -970,7 +970,7 @@ variable_identifier: complex_identifier
970970
}
971971
else // not an enum, probably a variable
972972
{
973-
init($$, ID_symbol);
973+
init($$, ID_smv_identifier);
974974
stack_expr($$).set(ID_identifier, id);
975975
auto var_it = PARSER.module->vars.find(id);
976976
if(var_it!= PARSER.module->vars.end())
@@ -983,7 +983,7 @@ variable_identifier: complex_identifier
983983
// Not in the NuSMV grammar.
984984
const irep_idt &id=stack_expr($1).id();
985985

986-
init($$, ID_symbol);
986+
init($$, ID_smv_identifier);
987987
stack_expr($$).set(ID_identifier, id);
988988
PARSER.module->vars[id];
989989
}
@@ -994,7 +994,7 @@ complex_identifier:
994994
{
995995
$$ = $1;
996996
irep_idt identifier = stack_expr($$).id();
997-
stack_expr($$).id(ID_symbol);
997+
stack_expr($$).id(ID_smv_identifier);
998998
stack_expr($$).set(ID_identifier, identifier);
999999
}
10001000
| complex_identifier DOT_Token QIDENTIFIER_Token

src/smvlang/smv_expr.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,4 +326,40 @@ inline smv_word1_exprt &to_smv_word1_expr(exprt &expr)
326326
return static_cast<smv_word1_exprt &>(expr);
327327
}
328328

329+
// parse tree only -- used for identifiers, which may turn into
330+
// symbols or enums
331+
class smv_identifier_exprt : public nullary_exprt
332+
{
333+
public:
334+
explicit smv_identifier_exprt(irep_idt _identifier)
335+
: nullary_exprt{ID_smv_identifier, typet{}}
336+
{
337+
identifier(_identifier);
338+
}
339+
340+
irep_idt identifier() const
341+
{
342+
return get(ID_identifier);
343+
}
344+
345+
void identifier(irep_idt _identifier)
346+
{
347+
set(ID_identifier, _identifier);
348+
}
349+
};
350+
351+
inline const smv_identifier_exprt &to_smv_identifier_expr(const exprt &expr)
352+
{
353+
PRECONDITION(expr.id() == ID_smv_identifier);
354+
smv_identifier_exprt::check(expr);
355+
return static_cast<const smv_identifier_exprt &>(expr);
356+
}
357+
358+
inline smv_identifier_exprt &to_smv_identifier_expr(exprt &expr)
359+
{
360+
PRECONDITION(expr.id() == ID_smv_identifier);
361+
smv_identifier_exprt::check(expr);
362+
return static_cast<smv_identifier_exprt &>(expr);
363+
}
364+
329365
#endif // CPROVER_SMV_EXPR_H

src/smvlang/smv_typecheck.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ void smv_typecheckt::flatten_hierarchy(smv_parse_treet::modulet &smv_module)
203203
for(auto &op : inst.operands())
204204
convert(op);
205205

206-
auto instance_base_name = to_symbol_expr(item.expr).get_identifier();
206+
auto instance_base_name = to_smv_identifier_expr(item.expr).identifier();
207207

208208
instantiate(
209209
smv_module,
@@ -1701,7 +1701,7 @@ void smv_typecheckt::convert(exprt &expr)
17011701
for(auto &op : expr.operands())
17021702
convert(op);
17031703

1704-
if(expr.id()==ID_symbol)
1704+
if(expr.id() == ID_smv_identifier)
17051705
{
17061706
const std::string &identifier=expr.get_string(ID_identifier);
17071707

@@ -1711,6 +1711,7 @@ void smv_typecheckt::convert(exprt &expr)
17111711
std::string id = module + "::var::" + identifier;
17121712

17131713
expr.set(ID_identifier, id);
1714+
expr.id(ID_symbol);
17141715
}
17151716
else if(expr.id()=="smv_nondet_choice" ||
17161717
expr.id()=="smv_union")
@@ -1914,7 +1915,7 @@ void smv_typecheckt::create_var_symbols(
19141915
{
19151916
if(item.is_var())
19161917
{
1917-
irep_idt base_name = to_symbol_expr(item.expr).get_identifier();
1918+
irep_idt base_name = to_smv_identifier_expr(item.expr).identifier();
19181919
irep_idt identifier = module + "::var::" + id2string(base_name);
19191920

19201921
auto symbol_ptr = symbol_table.lookup(identifier);
@@ -1953,8 +1954,9 @@ void smv_typecheckt::create_var_symbols(
19531954
}
19541955
else if(item.is_define())
19551956
{
1956-
const auto &symbol_expr = to_symbol_expr(to_equal_expr(item.expr).lhs());
1957-
irep_idt base_name = symbol_expr.get_identifier();
1957+
const auto &identifier_expr =
1958+
to_smv_identifier_expr(to_equal_expr(item.expr).lhs());
1959+
irep_idt base_name = identifier_expr.identifier();
19581960
irep_idt identifier = module + "::var::" + id2string(base_name);
19591961
typet type;
19601962
type.make_nil();
@@ -1973,13 +1975,13 @@ void smv_typecheckt::create_var_symbols(
19731975
symbol.value = nil_exprt{};
19741976
symbol.is_input = true;
19751977
symbol.is_state_var = false;
1976-
symbol.location = symbol_expr.source_location();
1978+
symbol.location = identifier_expr.source_location();
19771979

19781980
symbol_table.insert(std::move(symbol));
19791981
}
19801982
else if(item.is_enum())
19811983
{
1982-
irep_idt base_name = to_symbol_expr(item.expr).get_identifier();
1984+
irep_idt base_name = to_smv_identifier_expr(item.expr).identifier();
19831985
irep_idt identifier = module + "::var::" + id2string(base_name);
19841986

19851987
auto symbol_ptr = symbol_table.lookup(identifier);

0 commit comments

Comments
 (0)