Skip to content

Commit 83f13b8

Browse files
committed
SMV: cleanup smv_ranget
Minor cleanup on smv_ranget.
1 parent 081b322 commit 83f13b8

File tree

4 files changed

+48
-31
lines changed

4 files changed

+48
-31
lines changed

src/smvlang/Makefile

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
SRC = smv_language.cpp smv_parser.cpp smv_typecheck.cpp expr2smv.cpp \
2-
smv_y.tab.cpp lex.yy.cpp smv_parse_tree.cpp
1+
SRC = smv_language.cpp \
2+
smv_parser.cpp \
3+
smv_typecheck.cpp \
4+
expr2smv.cpp \
5+
smv_y.tab.cpp \
6+
lex.yy.cpp \
7+
smv_parse_tree.cpp \
8+
smv_range.cpp
39

410
include ../config.inc
511
include ../common

src/smvlang/smv_range.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*******************************************************************\
2+
3+
Module: SMV Typechecking
4+
5+
Author: Daniel Kroening, [email protected]
6+
7+
\*******************************************************************/
8+
9+
#include "smv_range.h"
10+
11+
#include <ostream>
12+
13+
std::ostream &operator<<(std::ostream &out, const smv_ranget &range)
14+
{
15+
return out << range.from << ".." << range.to;
16+
}

src/smvlang/smv_range.h

+19-16
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Author: Daniel Kroening, [email protected]
1111

1212
#include <util/arith_tools.h>
1313

14+
#include <iosfwd>
15+
1416
class smv_ranget
1517
{
1618
public:
@@ -41,11 +43,14 @@ class smv_ranget
4143
mp_max(to, other.to);
4244
}
4345

44-
void to_type(typet &dest) const
46+
typet to_type() const
4547
{
46-
dest = typet(ID_range);
47-
dest.set(ID_from, integer2string(from));
48-
dest.set(ID_to, integer2string(to));
48+
return range_typet{from, to};
49+
}
50+
51+
static smv_ranget from_type(const range_typet &src)
52+
{
53+
return {src.get_from(), src.get_to()};
4954
}
5055

5156
bool is_bool() const
@@ -58,32 +63,30 @@ class smv_ranget
5863
return from == to;
5964
}
6065

61-
smv_ranget &operator+(const smv_ranget &other)
66+
smv_ranget operator+(const smv_ranget &other) const
6267
{
63-
from += other.from;
64-
to += other.to;
65-
return *this;
68+
return {from + other.from, to + other.to};
6669
}
6770

68-
smv_ranget &operator-(const smv_ranget &other)
71+
smv_ranget operator-(const smv_ranget &other) const
6972
{
70-
from -= other.from;
71-
to -= other.to;
72-
return *this;
73+
return {from - other.from, to - other.to};
7374
}
7475

75-
smv_ranget &operator*(const smv_ranget &other)
76+
smv_ranget operator*(const smv_ranget &other) const
7677
{
7778
mp_integer p1 = from * other.from;
7879
mp_integer p2 = from * other.to;
7980
mp_integer p3 = to * other.from;
8081
mp_integer p4 = to * other.to;
8182

82-
from = std::min(p1, std::min(p2, std::min(p3, p4)));
83-
to = std::max(p1, std::max(p2, std::max(p3, p4)));
83+
mp_integer from = std::min(p1, std::min(p2, std::min(p3, p4)));
84+
mp_integer to = std::max(p1, std::max(p2, std::max(p3, p4)));
8485

85-
return *this;
86+
return {std::move(from), std::move(to)};
8687
}
88+
89+
friend std::ostream &operator<<(std::ostream &, const smv_ranget &);
8790
};
8891

8992
#endif // CPROVER_SMV_RANGE_H

src/smvlang/smv_typecheck.cpp

+5-13
Original file line numberDiff line numberDiff line change
@@ -472,14 +472,10 @@ smv_ranget smv_typecheckt::convert_type(const typet &src)
472472
}
473473
else if(src.id()==ID_range)
474474
{
475-
auto from = string2integer(src.get_string(ID_from));
476-
auto to = string2integer(src.get_string(ID_to));
475+
dest = smv_ranget::from_type(to_range_type(src));
477476

478-
if(from > to)
477+
if(dest.from > dest.to)
479478
throw errort().with_location(src.source_location()) << "range is empty";
480-
481-
dest.from = from;
482-
dest.to = to;
483479
}
484480
else if(src.id()==ID_enumeration)
485481
{
@@ -548,9 +544,7 @@ typet smv_typecheckt::type_union(
548544
}
549545
else
550546
{
551-
typet tmp;
552-
range1.to_type(tmp);
553-
return tmp;
547+
return range1.to_type();
554548
}
555549
}
556550

@@ -744,7 +738,7 @@ void smv_typecheckt::typecheck_expr_rec(
744738
else
745739
assert(false);
746740

747-
new_range.to_type(expr.type());
741+
expr.type() = new_range.to_type();
748742
}
749743
}
750744
else if(dest_type.id() != ID_range)
@@ -762,9 +756,7 @@ void smv_typecheckt::typecheck_expr_rec(
762756

763757
if(dest_type.is_nil())
764758
{
765-
expr.type()=typet(ID_range);
766-
expr.type().set(ID_from, integer2string(int_value));
767-
expr.type().set(ID_to, integer2string(int_value));
759+
expr.type() = range_typet{int_value, int_value};
768760
}
769761
else
770762
{

0 commit comments

Comments
 (0)