Skip to content

Commit d635850

Browse files
authored
Merge pull request #8405 from tautschnig/qualifiers-no-dynamic_cast
Remove uses of `dynamic_cast` from qualifierst hierarchy
2 parents f244575 + 02ae48e commit d635850

File tree

3 files changed

+48
-58
lines changed

3 files changed

+48
-58
lines changed

jbmc/src/java_bytecode/java_qualifiers.cpp

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,36 +51,26 @@ void java_qualifierst::write(typet &src) const
5151
type_checked_cast<annotated_typet>(src).get_annotations() = annotations;
5252
}
5353

54-
qualifierst &java_qualifierst::operator+=(const qualifierst &other)
54+
java_qualifierst &java_qualifierst::operator+=(const java_qualifierst &other)
5555
{
5656
c_qualifierst::operator+=(other);
57-
auto jq = dynamic_cast<const java_qualifierst *>(&other);
58-
if(jq != nullptr)
59-
{
60-
std::copy(
61-
jq->annotations.begin(),
62-
jq->annotations.end(),
63-
std::back_inserter(annotations));
64-
}
57+
std::copy(
58+
other.annotations.begin(),
59+
other.annotations.end(),
60+
std::back_inserter(annotations));
6561
return *this;
6662
}
6763

68-
bool java_qualifierst::operator==(const qualifierst &other) const
64+
bool java_qualifierst::operator==(const java_qualifierst &other) const
6965
{
70-
auto jq = dynamic_cast<const java_qualifierst *>(&other);
71-
if(jq == nullptr)
72-
return false;
73-
return c_qualifierst::operator==(other) && annotations == jq->annotations;
66+
return c_qualifierst::operator==(other) && annotations == other.annotations;
7467
}
7568

76-
bool java_qualifierst::is_subset_of(const qualifierst &other) const
69+
bool java_qualifierst::is_subset_of(const java_qualifierst &other) const
7770
{
7871
if(!c_qualifierst::is_subset_of(other))
7972
return false;
80-
auto jq = dynamic_cast<const java_qualifierst *>(&other);
81-
if(jq == nullptr)
82-
return annotations.empty();
83-
auto &other_a = jq->annotations;
73+
auto &other_a = other.annotations;
8474
for(const auto &annotation : annotations)
8575
{
8676
if(std::find(other_a.begin(), other_a.end(), annotation) == other_a.end())

jbmc/src/java_bytecode/java_qualifiers.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class java_qualifierst : public c_qualifierst
2525
public:
2626
virtual std::unique_ptr<qualifierst> clone() const override;
2727

28-
virtual qualifierst &operator+=(const qualifierst &other) override;
28+
java_qualifierst &operator+=(const java_qualifierst &other);
2929

3030
const std::vector<java_annotationt> &get_annotations() const
3131
{
@@ -38,8 +38,12 @@ class java_qualifierst : public c_qualifierst
3838
virtual void read(const typet &src) override;
3939
virtual void write(typet &src) const override;
4040

41-
virtual bool is_subset_of(const qualifierst &other) const override;
42-
virtual bool operator==(const qualifierst &other) const override;
41+
bool is_subset_of(const java_qualifierst &other) const;
42+
bool operator==(const java_qualifierst &other) const;
43+
bool operator!=(const java_qualifierst &other) const
44+
{
45+
return !(*this == other);
46+
}
4347

4448
virtual std::string as_string() const override;
4549
};

src/ansi-c/c_qualifiers.h

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,13 @@ class qualifierst
3535
public:
3636
virtual std::unique_ptr<qualifierst> clone() const = 0;
3737

38-
virtual qualifierst &operator+=(const qualifierst &b) = 0;
39-
4038
virtual std::size_t count() const = 0;
4139

4240
virtual void clear() = 0;
4341

4442
virtual void read(const typet &src) = 0;
4543
virtual void write(typet &src) const = 0;
4644

47-
// Comparisons
48-
virtual bool is_subset_of(const qualifierst &q) const = 0;
49-
virtual bool operator==(const qualifierst &other) const = 0;
50-
bool operator!=(const qualifierst &other) const
51-
{
52-
return !(*this == other);
53-
}
54-
5545
// String conversion
5646
virtual std::string as_string() const = 0;
5747
friend std::ostream &operator<<(std::ostream &, const qualifierst &);
@@ -107,41 +97,47 @@ class c_qualifierst : public qualifierst
10797

10898
static void clear(typet &dest);
10999

110-
virtual bool is_subset_of(const qualifierst &other) const override
100+
bool is_subset_of(const c_qualifierst &other) const
111101
{
112-
const c_qualifierst *cq = dynamic_cast<const c_qualifierst *>(&other);
113-
return (!is_constant || cq->is_constant) &&
114-
(!is_volatile || cq->is_volatile) &&
115-
(!is_restricted || cq->is_restricted) &&
116-
(!is_atomic || cq->is_atomic) && (!is_ptr32 || cq->is_ptr32) &&
117-
(!is_ptr64 || cq->is_ptr64) && (!is_nodiscard || cq->is_nodiscard) &&
118-
(!is_noreturn || cq->is_noreturn);
102+
return (!is_constant || other.is_constant) &&
103+
(!is_volatile || other.is_volatile) &&
104+
(!is_restricted || other.is_restricted) &&
105+
(!is_atomic || other.is_atomic) && (!is_ptr32 || other.is_ptr32) &&
106+
(!is_ptr64 || other.is_ptr64) &&
107+
(!is_nodiscard || other.is_nodiscard) &&
108+
(!is_noreturn || other.is_noreturn);
119109

120110
// is_transparent_union isn't checked
121111
}
122112

123-
virtual bool operator==(const qualifierst &other) const override
113+
bool operator==(const c_qualifierst &other) const
124114
{
125-
const c_qualifierst *cq = dynamic_cast<const c_qualifierst *>(&other);
126-
return is_constant == cq->is_constant && is_volatile == cq->is_volatile &&
127-
is_restricted == cq->is_restricted && is_atomic == cq->is_atomic &&
128-
is_ptr32 == cq->is_ptr32 && is_ptr64 == cq->is_ptr64 &&
129-
is_transparent_union == cq->is_transparent_union &&
130-
is_nodiscard == cq->is_nodiscard && is_noreturn == cq->is_noreturn;
115+
return is_constant == other.is_constant &&
116+
is_volatile == other.is_volatile &&
117+
is_restricted == other.is_restricted &&
118+
is_atomic == other.is_atomic && is_ptr32 == other.is_ptr32 &&
119+
is_ptr64 == other.is_ptr64 &&
120+
is_transparent_union == other.is_transparent_union &&
121+
is_nodiscard == other.is_nodiscard &&
122+
is_noreturn == other.is_noreturn;
123+
}
124+
125+
bool operator!=(const c_qualifierst &other) const
126+
{
127+
return !(*this == other);
131128
}
132129

133-
virtual qualifierst &operator+=(const qualifierst &other) override
130+
c_qualifierst &operator+=(const c_qualifierst &other)
134131
{
135-
const c_qualifierst *cq = dynamic_cast<const c_qualifierst *>(&other);
136-
is_constant |= cq->is_constant;
137-
is_volatile |= cq->is_volatile;
138-
is_restricted |= cq->is_restricted;
139-
is_atomic |= cq->is_atomic;
140-
is_ptr32 |= cq->is_ptr32;
141-
is_ptr64 |= cq->is_ptr64;
142-
is_transparent_union |= cq->is_transparent_union;
143-
is_nodiscard |= cq->is_nodiscard;
144-
is_noreturn |= cq->is_noreturn;
132+
is_constant |= other.is_constant;
133+
is_volatile |= other.is_volatile;
134+
is_restricted |= other.is_restricted;
135+
is_atomic |= other.is_atomic;
136+
is_ptr32 |= other.is_ptr32;
137+
is_ptr64 |= other.is_ptr64;
138+
is_transparent_union |= other.is_transparent_union;
139+
is_nodiscard |= other.is_nodiscard;
140+
is_noreturn |= other.is_noreturn;
145141
return *this;
146142
}
147143

0 commit comments

Comments
 (0)