Refactor NodeCategory for X-macros (#5029)

Taking an approach similar to NameId in #5018
This commit is contained in:
Jon Ross-Perkins
2025-02-27 01:00:10 +00:00
committed by GitHub
parent c4c3381b18
commit 90b6f5a22c
6 changed files with 141 additions and 85 deletions
+37
View File
@@ -0,0 +1,37 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "toolchain/parse/node_category.h"
#include "llvm/ADT/StringExtras.h"
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);
}
} // namespace Carbon::Parse