mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 11:41:06 +01:00
Add an EnumMaskBase type (#6053)
This is a bit of an experiment to see if there's a reasonable way to write a shared enum type, rather than writing per-case wrappers for things like `HasTypeQualifiers` or the printing. I think it's a bit borderline complexity right now, but I'm not sure I can reduce it much further. This changes from things like `Internal::EnumClassName##RawEnum` to `Internal::EnumClassName##Data::RawEnum` so that the enum entries can have back references to bit shifts without needing to know the containing type name. Because I'm trying to reduce duplication between mask and non-mask enums, I did this to non-mask enums too. This was motivated by #6035 adding another enum mask (which will grow more entries, and is intended to switch if this is accepted), but I'm not using that PR as a base here because I didn't want the merge dependency.
This commit is contained in:
@@ -8,30 +8,7 @@
|
||||
|
||||
namespace Carbon::Parse {
|
||||
|
||||
// Returns a string form of the node category for printing.
|
||||
static auto NodeCategoryToString(NodeCategory::RawEnumType category)
|
||||
-> llvm::StringLiteral {
|
||||
#define CARBON_NODE_CATEGORY_TO_STRING(Name) \
|
||||
case NodeCategory::Name: \
|
||||
return #Name;
|
||||
|
||||
switch (category) {
|
||||
CARBON_NODE_CATEGORY(CARBON_NODE_CATEGORY_TO_STRING)
|
||||
CARBON_NODE_CATEGORY_TO_STRING(None)
|
||||
}
|
||||
|
||||
#undef CARBON_NODE_CATEGORY_TO_STRING
|
||||
}
|
||||
|
||||
auto NodeCategory::Print(llvm::raw_ostream& out) const -> void {
|
||||
llvm::ListSeparator sep("|");
|
||||
auto value = value_;
|
||||
do {
|
||||
// The lowest set bit in the value, or 0 (`None`) if no bits are set.
|
||||
auto lowest_bit = static_cast<RawEnumType>(value & -value);
|
||||
out << sep << NodeCategoryToString(lowest_bit);
|
||||
value &= ~lowest_bit;
|
||||
} while (value);
|
||||
}
|
||||
CARBON_DEFINE_ENUM_MASK_NAMES(NodeCategory) = {
|
||||
CARBON_NODE_CATEGORY(CARBON_ENUM_MASK_NAME_STRING)};
|
||||
|
||||
} // namespace Carbon::Parse
|
||||
|
||||
Reference in New Issue
Block a user