Skip to content

Commit

Permalink
remove MIM_ENUM_OPERATORS macro magic in favor of fe::BitEnum
Browse files Browse the repository at this point in the history
  • Loading branch information
leissa committed Oct 21, 2024
1 parent 808946b commit 1850412
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 34 deletions.
1 change: 0 additions & 1 deletion docs/Doxyfile.in
Original file line number Diff line number Diff line change
Expand Up @@ -2518,7 +2518,6 @@ EXPAND_AS_DEFINED = CODE \
CODE2 \
CODE3 \
CODE4 \
MIM_ENUM_OPERATORS \
MIM_1_8_16_32_64 \
MIM_8_16_32_64 \
MIM_16_32_64 \
Expand Down
2 changes: 1 addition & 1 deletion external/fe
Submodule fe updated 3 files
+1 −0 CMakeLists.txt
+57 −0 include/fe/enum.h
+19 −0 tests/test.cpp
18 changes: 15 additions & 3 deletions include/mim/def.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <fe/assert.h>
#include <fe/cast.h>
#include <fe/enum.h>

#include "mim/config.h"

Expand Down Expand Up @@ -77,7 +78,7 @@ template<class To> using VarMap = GIDMap<const Var*, To>;
using VarSet = GIDSet<const Var*>;
using Var2Var = VarMap<const Var*>;
using Vars = PooledSet<const Var*>;
///@{
///@}

//------------------------------------------------------------------------------

Expand Down Expand Up @@ -139,6 +140,12 @@ enum class Sort { Term, Type, Kind, Space, Univ, Level };

//------------------------------------------------------------------------------

using fe::operator&;
using fe::operator|;
using fe::operator^;
using fe::operator<=>;
using fe::operator==;

/// @name Dep
///@{
enum class Dep : unsigned {
Expand All @@ -149,10 +156,15 @@ enum class Dep : unsigned {
Proxy = 1 << 3,
Var = 1 << 4,
};

MIM_ENUM_OPERATORS(Dep)
///@}

} // namespace mim
#ifndef DOXYGEN
template<> struct fe::is_bit_enum<mim::Dep> : std::true_type {};
#endif

namespace mim {

/// Use as mixin to wrap all kind of Def::proj and Def::projs variants.
#define MIM_PROJ(NAME, CONST) \
nat_t num_##NAME##s() CONST { return ((const Def*)NAME())->num_projs(); } \
Expand Down
6 changes: 4 additions & 2 deletions include/mim/plug/core/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ enum class Mode : nat_t {
nusw = nuw | nsw,
};

MIM_ENUM_OPERATORS(Mode)

/// Give Mode as mim::plug::math::Mode, mim::nat_t or Ref.
using VMode = std::variant<Mode, nat_t, Ref>;

Expand Down Expand Up @@ -129,3 +127,7 @@ constexpr bool is_associative(plug::core::wrap id) { return is_commutative(id);
///@}

} // namespace mim

#ifndef DOXYGEN
template<> struct fe::is_bit_enum<mim::plug::core::Mode> : std::true_type {};
#endif
6 changes: 4 additions & 2 deletions include/mim/plug/math/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ enum class Mode : nat_t {
};
// clang-format on

MIM_ENUM_OPERATORS(Mode)

/// Give Mode as mim::plug::math::Mode, mim::nat_t or Ref.
using VMode = std::variant<Mode, nat_t, Ref>;

Expand Down Expand Up @@ -132,3 +130,7 @@ constexpr bool is_associative(plug::math::arith id) { return is_commutative(id);
///@}

} // namespace mim

#ifndef DOXYGEN
template<> struct fe::is_bit_enum<mim::plug::math::Mode> : std::true_type {};
#endif
20 changes: 0 additions & 20 deletions include/mim/util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,24 +182,4 @@ template<class K, class V> using GIDNodeMap = absl::node_hash_map<K, V, GIDHash<
template<class K> using GIDNodeSet = absl::node_hash_set<K, GIDHash<K>, GIDEq<K>>;
///@}

/// Use this to declare all kind of bit and comparison operators for an `enum` @p E.
/// Note that the bit operators return @p E's underlying type and not the original `enum` @p E.
/// This is because the result may not be a valid `enum` value.
/// For the same reason, it doesn't make sense to declare operators such as `&=`.
#define MIM_ENUM_OPERATORS(E) \
constexpr auto operator&( E x, E y) { return std::underlying_type_t<E>(x) & std::underlying_type_t<E>(y); } \
constexpr auto operator&(std::underlying_type_t<E> x, E y) { return x & std::underlying_type_t<E>(y); } \
constexpr auto operator&( E x, std::underlying_type_t<E> y) { return std::underlying_type_t<E>(x) & y ; } \
constexpr auto operator|( E x, E y) { return std::underlying_type_t<E>(x) | std::underlying_type_t<E>(y); } \
constexpr auto operator|(std::underlying_type_t<E> x, E y) { return x | std::underlying_type_t<E>(y); } \
constexpr auto operator|( E x, std::underlying_type_t<E> y) { return std::underlying_type_t<E>(x) | y ; } \
constexpr auto operator^( E x, E y) { return std::underlying_type_t<E>(x) ^ std::underlying_type_t<E>(y); } \
constexpr auto operator^(std::underlying_type_t<E> x, E y) { return x ^ std::underlying_type_t<E>(y); } \
constexpr auto operator^( E x, std::underlying_type_t<E> y) { return std::underlying_type_t<E>(x) ^ y ; } \
constexpr std::strong_ordering operator<=>(std::underlying_type_t<E> x, E y) { return x <=> std::underlying_type_t<E>(y); } \
constexpr std::strong_ordering operator<=>(E x, std::underlying_type_t<E> y) { return std::underlying_type_t<E>(x) <=> y; } \
constexpr bool operator==(std::underlying_type_t<E> x, E y) { return x == std::underlying_type_t<E>(y); } \
constexpr bool operator==(E x, std::underlying_type_t<E> y) { return std::underlying_type_t<E>(x) == y; }
// clang-format on

} // namespace mim
20 changes: 15 additions & 5 deletions src/mim/ast/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ void AST::bootstrap(Sym plugin, std::ostream& h) {
--tab;
tab.print(h, "}};\n\n");

if (!annex.subs.empty()) tab.print(h, "MIM_ENUM_OPERATORS({})\n", sym.tag);
print(outer_namespace.emplace_back(), "template<> constexpr size_t Annex::Num<plug::{}::{}> = {};\n", plugin, sym.tag, annex.subs.size());

if (auto norm = annex.normalizer) {
Expand Down Expand Up @@ -127,19 +126,30 @@ void AST::bootstrap(Sym plugin, std::ostream& h) {

tab.print(h, "}} // namespace plug::{}\n\n", plugin);

tab.print(h, "#ifndef DOXYGEN // don't include in Doxygen documentation\n");
tab.print(h, "#ifndef DOXYGEN // don't include in Doxygen documentation\n\n");
for (const auto& line : outer_namespace) tab.print(h, "{}", line.str());
tab.print(h, "\n");

// emit helpers for non-function axiom
for (const auto& [tag, ax] : infos) {
const auto& sym = ax.sym;
auto sym = ax.sym;
if (ax.is_pi() || sym.plugin != plugin) continue; // from function or other plugin?
tab.print(h, "template<> struct Axiom::Match<plug::{}::{}> {{ using type = Axiom; }};\n", sym.plugin, sym.tag);
}

tab.print(h, "#endif\n");
tab.print(h, "}} // namespace mim\n");
tab.print(h, "\n#endif\n");
tab.print(h, "}} // namespace mim\n\n");

tab.print(h, "#ifndef DOXYGEN // don't include in Doxygen documentation\n\n");
for (const auto& [key, annex] : infos) {
if (!annex.subs.empty()) {
auto sym = annex.sym;
tab.print(h, "template<> struct fe::is_bit_enum<mim::plug::{}::{}> : std::true_type {{}};\n", sym.plugin,
sym.tag);
}
}

tab.print(h, "\n#endif\n");
}

/*
Expand Down

0 comments on commit 1850412

Please sign in to comment.