Skip to content

Commit 4816beb

Browse files
author
Daniel Kroening
committed
enable conversion from irep_idt to enum class
1 parent 46b31a4 commit 4816beb

File tree

3 files changed

+69
-12
lines changed

3 files changed

+69
-12
lines changed

src/util/dstring.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Author: Daniel Kroening, [email protected]
2020

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

165-
private:
162+
protected:
166163
#ifdef __GNUC__
167164
constexpr
168165
#endif

src/util/irep.h

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

3131
#ifdef USE_DSTRING
32-
typedef dstringt irep_idt;
33-
typedef dstringt irep_namet;
32+
typedef irep_idt irep_namet;
3433
// NOLINTNEXTLINE(readability/identifiers)
3534
typedef dstring_hash irep_id_hash;
3635
#else

src/util/irep_ids.h

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,80 @@ 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():dstringt()
55+
{
56+
}
57+
58+
// this is safe for static objects
59+
#ifdef __GNUC__
60+
constexpr
61+
#endif
62+
static irep_idt make_from_table_index(unsigned no)
63+
{
64+
return irep_idt(no);
65+
}
66+
67+
#ifdef __GNUC__
68+
// This conversion allows the use of irep_idts
69+
// in switch ... case statements.
70+
constexpr operator idt() const { return static_cast<idt>(no); }
71+
#endif
72+
73+
// this one is not safe for static objects
74+
// NOLINTNEXTLINE(runtime/explicit)
75+
irep_idt(const char *s):dstringt(s)
76+
{
77+
}
78+
79+
// this one is not safe for static objects
80+
// NOLINTNEXTLINE(runtime/explicit)
81+
irep_idt(const std::string &s):dstringt(s)
82+
{
83+
}
84+
85+
protected:
86+
#ifdef __GNUC__
87+
constexpr
88+
#endif
89+
explicit irep_idt(unsigned _no):dstringt(_no)
90+
{
91+
}
92+
};
93+
94+
// NOLINTNEXTLINE [allow specialisation within 'std']
95+
namespace std
96+
{
97+
/// Default hash function of `dstringt` for use with STL containers.
98+
template <>
99+
struct hash<irep_idt> // NOLINT(readability/identifiers)
100+
{
101+
size_t operator()(const irep_idt &irep_id) const
102+
{
103+
return irep_id.hash();
104+
}
105+
};
106+
}
107+
47108
#ifdef __GNUC__
48109
#define IREP_ID_ONE(the_id) \
49-
constexpr dstringt ID_##the_id=dstringt::make_from_table_index( \
110+
constexpr irep_idt ID_##the_id=irep_idt::make_from_table_index( \
50111
static_cast<unsigned>(idt::id_##the_id));
51112
#define IREP_ID_TWO(the_id, str) \
52-
constexpr dstringt ID_##the_id=dstringt::make_from_table_index( \
113+
constexpr irep_idt ID_##the_id=irep_idt::make_from_table_index( \
53114
static_cast<unsigned>(idt::id_##the_id));
54115
#else
55116
#define IREP_ID_ONE(the_id) \
56-
const dstringt ID_##the_id=dstringt::make_from_table_index( \
117+
const irep_idt ID_##the_id=irep_idt::make_from_table_index( \
57118
static_cast<unsigned>(idt::id_##the_id));
58119
#define IREP_ID_TWO(the_id, str) \
59-
const const dstringt ID_##the_id=dstringt::make_from_table_index( \
120+
const const irep_idt ID_##the_id=irep_idt::make_from_table_index( \
60121
static_cast<unsigned>(idt::id_##the_id));
61122
#endif
62123

0 commit comments

Comments
 (0)