Skip to content

Commit b21d45b

Browse files
author
Daniel Kroening
committed
enable conversion from irep_idt to enum class
1 parent cfb937d commit b21d45b

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
@@ -151,7 +148,7 @@ class dstringt final
151148
return no;
152149
}
153150

154-
private:
151+
protected:
155152
#ifdef __GNUC__
156153
constexpr
157154
#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
@@ -43,19 +43,80 @@ enum class idt:unsigned
4343
#include "irep_ids.def" // NOLINT(build/include)
4444
};
4545

46+
class irep_idt final:public dstringt
47+
{
48+
public:
49+
// this is safe for static objects
50+
#ifdef __GNUC__
51+
constexpr
52+
#endif
53+
irep_idt():dstringt()
54+
{
55+
}
56+
57+
// this is safe for static objects
58+
#ifdef __GNUC__
59+
constexpr
60+
#endif
61+
static irep_idt make_from_table_index(unsigned no)
62+
{
63+
return irep_idt(no);
64+
}
65+
66+
#ifdef __GNUC__
67+
// This conversion allows the use of irep_idts
68+
// in switch ... case statements.
69+
constexpr operator idt() const { return static_cast<idt>(no); }
70+
#endif
71+
72+
// this one is not safe for static objects
73+
// NOLINTNEXTLINE(runtime/explicit)
74+
irep_idt(const char *s):dstringt(s)
75+
{
76+
}
77+
78+
// this one is not safe for static objects
79+
// NOLINTNEXTLINE(runtime/explicit)
80+
irep_idt(const std::string &s):dstringt(s)
81+
{
82+
}
83+
84+
protected:
85+
#ifdef __GNUC__
86+
constexpr
87+
#endif
88+
explicit irep_idt(unsigned _no):dstringt(_no)
89+
{
90+
}
91+
};
92+
93+
// NOLINTNEXTLINE [allow specialisation within 'std']
94+
namespace std
95+
{
96+
/// Default hash function of `dstringt` for use with STL containers.
97+
template <>
98+
struct hash<irep_idt> // NOLINT(readability/identifiers)
99+
{
100+
size_t operator()(const irep_idt &irep_id) const
101+
{
102+
return irep_id.hash();
103+
}
104+
};
105+
}
106+
46107
#ifdef __GNUC__
47108
#define IREP_ID_ONE(the_id) \
48-
constexpr dstringt ID_##the_id=dstringt::make_from_table_index( \
109+
constexpr irep_idt ID_##the_id=irep_idt::make_from_table_index( \
49110
static_cast<unsigned>(idt::id_##the_id));
50111
#define IREP_ID_TWO(the_id, str) \
51-
constexpr dstringt ID_##the_id=dstringt::make_from_table_index( \
112+
constexpr irep_idt ID_##the_id=irep_idt::make_from_table_index( \
52113
static_cast<unsigned>(idt::id_##the_id));
53114
#else
54115
#define IREP_ID_ONE(the_id) \
55-
const dstringt ID_##the_id=dstringt::make_from_table_index( \
116+
const irep_idt ID_##the_id=irep_idt::make_from_table_index( \
56117
static_cast<unsigned>(idt::id_##the_id));
57118
#define IREP_ID_TWO(the_id, str) \
58-
const const dstringt ID_##the_id=dstringt::make_from_table_index( \
119+
const const irep_idt ID_##the_id=irep_idt::make_from_table_index( \
59120
static_cast<unsigned>(idt::id_##the_id));
60121
#endif
61122

0 commit comments

Comments
 (0)