Skip to content

Commit a9af8bb

Browse files
author
Daniel Kroening
committed
enable conversion from irep_idt to enum class
1 parent 020069f commit a9af8bb

File tree

3 files changed

+84
-21
lines changed

3 files changed

+84
-21
lines changed

src/util/dstring.h

Lines changed: 4 additions & 7 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,8 +159,8 @@ class dstringt final
162159
return as_string().end();
163160
}
164161

165-
private:
166-
#ifdef __GNUC__
162+
protected:
163+
#ifdef __GNUC__
167164
constexpr
168165
#endif
169166
explicit dstringt(unsigned _no):no(_no)

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: 79 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,87 @@ 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 not safe for static objects
79+
// NOLINTNEXTLINE(runtime/explicit)
80+
irep_idt(const char *s) : dstringt(s)
81+
{
82+
}
83+
84+
// this one is not safe for static objects
85+
// NOLINTNEXTLINE(runtime/explicit)
86+
irep_idt(const std::string &s) : dstringt(s)
87+
{
88+
}
89+
90+
protected:
91+
#ifdef __GNUC__
92+
constexpr
93+
#endif
94+
explicit irep_idt(unsigned _no)
95+
: dstringt(_no)
96+
{
97+
}
98+
};
99+
100+
// NOLINTNEXTLINE [allow specialisation within 'std']
101+
namespace std
102+
{
103+
/// Default hash function of `dstringt` for use with STL containers.
104+
template <>
105+
struct hash<irep_idt> // NOLINT(readability/identifiers)
106+
{
107+
size_t operator()(const irep_idt &irep_id) const
108+
{
109+
return irep_id.hash();
110+
}
111+
};
112+
} // namespace std
113+
47114
#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));
115+
#define IREP_ID_ONE(the_id) \
116+
constexpr irep_idt ID_##the_id = \
117+
irep_idt::make_from_table_index(static_cast<unsigned>(idt::id_##the_id));
118+
#define IREP_ID_TWO(the_id, str) \
119+
constexpr irep_idt ID_##the_id = \
120+
irep_idt::make_from_table_index(static_cast<unsigned>(idt::id_##the_id));
54121
#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));
122+
#define IREP_ID_ONE(the_id) \
123+
const irep_idt ID_##the_id = \
124+
irep_idt::make_from_table_index(static_cast<unsigned>(idt::id_##the_id));
125+
#define IREP_ID_TWO(the_id, str) \
126+
const const irep_idt ID_##the_id = \
127+
irep_idt::make_from_table_index(static_cast<unsigned>(idt::id_##the_id));
61128
#endif
62129

63130
template <>

0 commit comments

Comments
 (0)