From 02ae48e8ef064889a81278930db86b81ad5e80a9 Mon Sep 17 00:00:00 2001 From: Michael Tautschnig Date: Tue, 6 Aug 2024 17:04:12 +0000 Subject: [PATCH] Remove uses of `dynamic_cast` from qualifierst hierarchy There is no need to use virtual methods as we always know from context the exact type we are working with. --- jbmc/src/java_bytecode/java_qualifiers.cpp | 28 +++------ jbmc/src/java_bytecode/java_qualifiers.h | 10 +++- src/ansi-c/c_qualifiers.h | 68 ++++++++++------------ 3 files changed, 48 insertions(+), 58 deletions(-) diff --git a/jbmc/src/java_bytecode/java_qualifiers.cpp b/jbmc/src/java_bytecode/java_qualifiers.cpp index c462e5471f8..acff3ae93ab 100644 --- a/jbmc/src/java_bytecode/java_qualifiers.cpp +++ b/jbmc/src/java_bytecode/java_qualifiers.cpp @@ -51,36 +51,26 @@ void java_qualifierst::write(typet &src) const type_checked_cast(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(&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(&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(&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()) diff --git a/jbmc/src/java_bytecode/java_qualifiers.h b/jbmc/src/java_bytecode/java_qualifiers.h index e85bbfa6d93..3a235f64778 100644 --- a/jbmc/src/java_bytecode/java_qualifiers.h +++ b/jbmc/src/java_bytecode/java_qualifiers.h @@ -25,7 +25,7 @@ class java_qualifierst : public c_qualifierst public: virtual std::unique_ptr clone() const override; - virtual qualifierst &operator+=(const qualifierst &other) override; + java_qualifierst &operator+=(const java_qualifierst &other); const std::vector &get_annotations() const { @@ -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; }; diff --git a/src/ansi-c/c_qualifiers.h b/src/ansi-c/c_qualifiers.h index f75ceaad02f..052cf17f824 100644 --- a/src/ansi-c/c_qualifiers.h +++ b/src/ansi-c/c_qualifiers.h @@ -35,8 +35,6 @@ class qualifierst public: virtual std::unique_ptr clone() const = 0; - virtual qualifierst &operator+=(const qualifierst &b) = 0; - virtual std::size_t count() const = 0; virtual void clear() = 0; @@ -44,14 +42,6 @@ class qualifierst 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 &); @@ -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(&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(&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(&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; }