Skip to content

Commit 4005351

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

File tree

3 files changed

+68
-10
lines changed

3 files changed

+68
-10
lines changed

src/util/dstring.h

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

1717
#include "string_container.h"
1818

19-
// Marked final to disable inheritance.
20-
// No virtual destructor, so runtime-polymorphic use would be unsafe.
21-
class dstringt final
19+
class dstringt
2220
{
2321
public:
2422
// this is safe for static objects
@@ -137,7 +135,7 @@ class dstringt final
137135
return no;
138136
}
139137

140-
private:
138+
protected:
141139
#ifdef __GNUC__
142140
constexpr
143141
#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)