Skip to content

Commit

Permalink
Remove uses of dynamic_cast from qualifierst hierarchy
Browse files Browse the repository at this point in the history
There is no need to use virtual methods as we always know from context
the exact type we are working with.
  • Loading branch information
tautschnig committed Aug 6, 2024
1 parent b335979 commit 02ae48e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 58 deletions.
28 changes: 9 additions & 19 deletions jbmc/src/java_bytecode/java_qualifiers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,36 +51,26 @@ void java_qualifierst::write(typet &src) const
type_checked_cast<annotated_typet>(src).get_annotations() = annotations;
}

qualifierst &java_qualifierst::operator+=(const qualifierst &other)
java_qualifierst &java_qualifierst::operator+=(const java_qualifierst &other)
{
c_qualifierst::operator+=(other);
auto jq = dynamic_cast<const java_qualifierst *>(&other);
if(jq != nullptr)
{
std::copy(
jq->annotations.begin(),
jq->annotations.end(),
std::back_inserter(annotations));
}
std::copy(
other.annotations.begin(),
other.annotations.end(),
std::back_inserter(annotations));
return *this;
}

bool java_qualifierst::operator==(const qualifierst &other) const
bool java_qualifierst::operator==(const java_qualifierst &other) const
{
auto jq = dynamic_cast<const java_qualifierst *>(&other);
if(jq == nullptr)
return false;
return c_qualifierst::operator==(other) && annotations == jq->annotations;
return c_qualifierst::operator==(other) && annotations == other.annotations;
}

bool java_qualifierst::is_subset_of(const qualifierst &other) const
bool java_qualifierst::is_subset_of(const java_qualifierst &other) const
{
if(!c_qualifierst::is_subset_of(other))
return false;
auto jq = dynamic_cast<const java_qualifierst *>(&other);
if(jq == nullptr)
return annotations.empty();
auto &other_a = jq->annotations;
auto &other_a = other.annotations;
for(const auto &annotation : annotations)
{
if(std::find(other_a.begin(), other_a.end(), annotation) == other_a.end())
Expand Down
10 changes: 7 additions & 3 deletions jbmc/src/java_bytecode/java_qualifiers.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class java_qualifierst : public c_qualifierst
public:
virtual std::unique_ptr<qualifierst> clone() const override;

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

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

virtual bool is_subset_of(const qualifierst &other) const override;
virtual bool operator==(const qualifierst &other) const override;
bool is_subset_of(const java_qualifierst &other) const;
bool operator==(const java_qualifierst &other) const;
bool operator!=(const java_qualifierst &other) const
{
return !(*this == other);
}

virtual std::string as_string() const override;
};
Expand Down
68 changes: 32 additions & 36 deletions src/ansi-c/c_qualifiers.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,13 @@ class qualifierst
public:
virtual std::unique_ptr<qualifierst> clone() const = 0;

virtual qualifierst &operator+=(const qualifierst &b) = 0;

virtual std::size_t count() const = 0;

virtual void clear() = 0;

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

// Comparisons
virtual bool is_subset_of(const qualifierst &q) const = 0;
virtual bool operator==(const qualifierst &other) const = 0;
bool operator!=(const qualifierst &other) const
{
return !(*this == other);
}

// String conversion
virtual std::string as_string() const = 0;
friend std::ostream &operator<<(std::ostream &, const qualifierst &);
Expand Down Expand Up @@ -107,41 +97,47 @@ class c_qualifierst : public qualifierst

static void clear(typet &dest);

virtual bool is_subset_of(const qualifierst &other) const override
bool is_subset_of(const c_qualifierst &other) const
{
const c_qualifierst *cq = dynamic_cast<const c_qualifierst *>(&other);
return (!is_constant || cq->is_constant) &&
(!is_volatile || cq->is_volatile) &&
(!is_restricted || cq->is_restricted) &&
(!is_atomic || cq->is_atomic) && (!is_ptr32 || cq->is_ptr32) &&
(!is_ptr64 || cq->is_ptr64) && (!is_nodiscard || cq->is_nodiscard) &&
(!is_noreturn || cq->is_noreturn);
return (!is_constant || other.is_constant) &&
(!is_volatile || other.is_volatile) &&
(!is_restricted || other.is_restricted) &&
(!is_atomic || other.is_atomic) && (!is_ptr32 || other.is_ptr32) &&
(!is_ptr64 || other.is_ptr64) &&
(!is_nodiscard || other.is_nodiscard) &&
(!is_noreturn || other.is_noreturn);

// is_transparent_union isn't checked
}

virtual bool operator==(const qualifierst &other) const override
bool operator==(const c_qualifierst &other) const
{
const c_qualifierst *cq = dynamic_cast<const c_qualifierst *>(&other);
return is_constant == cq->is_constant && is_volatile == cq->is_volatile &&
is_restricted == cq->is_restricted && is_atomic == cq->is_atomic &&
is_ptr32 == cq->is_ptr32 && is_ptr64 == cq->is_ptr64 &&
is_transparent_union == cq->is_transparent_union &&
is_nodiscard == cq->is_nodiscard && is_noreturn == cq->is_noreturn;
return is_constant == other.is_constant &&
is_volatile == other.is_volatile &&
is_restricted == other.is_restricted &&
is_atomic == other.is_atomic && is_ptr32 == other.is_ptr32 &&
is_ptr64 == other.is_ptr64 &&
is_transparent_union == other.is_transparent_union &&
is_nodiscard == other.is_nodiscard &&
is_noreturn == other.is_noreturn;
}

bool operator!=(const c_qualifierst &other) const
{
return !(*this == other);
}

virtual qualifierst &operator+=(const qualifierst &other) override
c_qualifierst &operator+=(const c_qualifierst &other)
{
const c_qualifierst *cq = dynamic_cast<const c_qualifierst *>(&other);
is_constant |= cq->is_constant;
is_volatile |= cq->is_volatile;
is_restricted |= cq->is_restricted;
is_atomic |= cq->is_atomic;
is_ptr32 |= cq->is_ptr32;
is_ptr64 |= cq->is_ptr64;
is_transparent_union |= cq->is_transparent_union;
is_nodiscard |= cq->is_nodiscard;
is_noreturn |= cq->is_noreturn;
is_constant |= other.is_constant;
is_volatile |= other.is_volatile;
is_restricted |= other.is_restricted;
is_atomic |= other.is_atomic;
is_ptr32 |= other.is_ptr32;
is_ptr64 |= other.is_ptr64;
is_transparent_union |= other.is_transparent_union;
is_nodiscard |= other.is_nodiscard;
is_noreturn |= other.is_noreturn;
return *this;
}

Expand Down

0 comments on commit 02ae48e

Please sign in to comment.