Skip to content

Commit 75ac3db

Browse files
author
Daniel Kroening
committed
enable conversion from irep_idt to enum class
1 parent 8a6ed22 commit 75ac3db

File tree

3 files changed

+94
-21
lines changed

3 files changed

+94
-21
lines changed

src/util/dstring.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Author: Daniel Kroening, [email protected]
2121

2222
/// \ref dstringt has one field, an unsigned integer \ref no which is an index
2323
/// into a static table of strings. This makes it expensive to create a new
24-
/// string(because you have to look through the whole table to see if it is
24+
/// string (because you have to look through the whole table to see if it is
2525
/// already there, and add it if it isn't) but very cheap to compare strings
2626
/// (just compare the two integers). It also means that when you have lots of
2727
/// copies of the same string you only have to store the whole string once,
@@ -30,10 +30,7 @@ Author: Daniel Kroening, [email protected]
3030
/// `irep_idt` and `irep_namet` are typedef-ed to \ref dstringt in irep.h unless
3131
/// `USE_STD_STRING` is set.
3232
///
33-
///
34-
/// Note: Marked final to disable inheritance. No virtual destructor, so
35-
/// runtime-polymorphic use would be unsafe.
36-
class dstringt final
33+
class dstringt
3734
{
3835
public:
3936
// this is safe for static objects
@@ -183,8 +180,8 @@ class dstringt final
183180
return as_string().end();
184181
}
185182

186-
private:
187-
#ifdef __GNUC__
183+
protected:
184+
#ifdef __GNUC__
188185
constexpr
189186
#endif
190187
explicit dstringt(unsigned _no):no(_no)

src/util/irep.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ Author: Daniel Kroening, [email protected]
2727
#endif
2828

2929
#ifdef USE_DSTRING
30-
typedef dstringt irep_idt;
31-
typedef dstringt irep_namet;
30+
typedef irep_idt irep_namet;
3231
// NOLINTNEXTLINE(readability/identifiers)
3332
typedef dstring_hash irep_id_hash;
3433
#else

src/util/irep_ids.h

Lines changed: 89 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,97 @@ enum class idt : unsigned
4444
#include "irep_ids.def" // NOLINT(build/include)
4545
};
4646

47+
class irep_idt final : public dstringt
48+
{
49+
public:
50+
// this is safe for static objects
51+
#ifdef __GNUC__
52+
constexpr
53+
#endif
54+
irep_idt()
55+
: dstringt()
56+
{
57+
}
58+
59+
// this is safe for static objects
60+
#ifdef __GNUC__
61+
constexpr
62+
#endif
63+
static irep_idt
64+
make_from_table_index(unsigned no)
65+
{
66+
return irep_idt(no);
67+
}
68+
69+
#ifdef __GNUC__
70+
// This conversion allows the use of irep_idts
71+
// in switch ... case statements.
72+
constexpr operator idt() const
73+
{
74+
return static_cast<idt>(no);
75+
}
76+
#endif
77+
78+
// this one is safe for static objects
79+
#ifdef __GNUC__
80+
constexpr
81+
#endif
82+
// NOLINTNEXTLINE(runtime/explicit)
83+
irep_idt(dstringt s)
84+
: dstringt(s)
85+
{
86+
}
87+
88+
// this one is not safe for static objects
89+
// NOLINTNEXTLINE(runtime/explicit)
90+
irep_idt(const char *s) : dstringt(s)
91+
{
92+
}
93+
94+
// this one is not safe for static objects
95+
// NOLINTNEXTLINE(runtime/explicit)
96+
irep_idt(const std::string &s) : dstringt(s)
97+
{
98+
}
99+
100+
protected:
101+
#ifdef __GNUC__
102+
constexpr
103+
#endif
104+
explicit irep_idt(unsigned _no)
105+
: dstringt(_no)
106+
{
107+
}
108+
};
109+
110+
// NOLINTNEXTLINE [allow specialisation within 'std']
111+
namespace std
112+
{
113+
/// Default hash function of `dstringt` for use with STL containers.
114+
template <>
115+
struct hash<irep_idt> // NOLINT(readability/identifiers)
116+
{
117+
size_t operator()(const irep_idt &irep_id) const
118+
{
119+
return irep_id.hash();
120+
}
121+
};
122+
} // namespace std
123+
47124
#ifdef __GNUC__
48-
#define IREP_ID_ONE(the_id) \
49-
constexpr dstringt ID_##the_id=dstringt::make_from_table_index( \
50-
static_cast<unsigned>(idt::id_##the_id));
51-
#define IREP_ID_TWO(the_id, str) \
52-
constexpr dstringt ID_##the_id=dstringt::make_from_table_index( \
53-
static_cast<unsigned>(idt::id_##the_id));
125+
#define IREP_ID_ONE(the_id) \
126+
constexpr irep_idt ID_##the_id = \
127+
irep_idt::make_from_table_index(static_cast<unsigned>(idt::id_##the_id));
128+
#define IREP_ID_TWO(the_id, str) \
129+
constexpr irep_idt ID_##the_id = \
130+
irep_idt::make_from_table_index(static_cast<unsigned>(idt::id_##the_id));
54131
#else
55-
#define IREP_ID_ONE(the_id) \
56-
const dstringt ID_##the_id=dstringt::make_from_table_index( \
57-
static_cast<unsigned>(idt::id_##the_id));
58-
#define IREP_ID_TWO(the_id, str) \
59-
const const dstringt ID_##the_id=dstringt::make_from_table_index( \
60-
static_cast<unsigned>(idt::id_##the_id));
132+
#define IREP_ID_ONE(the_id) \
133+
const irep_idt ID_##the_id = \
134+
irep_idt::make_from_table_index(static_cast<unsigned>(idt::id_##the_id));
135+
#define IREP_ID_TWO(the_id, str) \
136+
const const irep_idt ID_##the_id = \
137+
irep_idt::make_from_table_index(static_cast<unsigned>(idt::id_##the_id));
61138
#endif
62139

63140
template <>

0 commit comments

Comments
 (0)