Files
carbon-lang/toolchain/parse/node_kind.cpp
T
Jon Ross-PerkinsandRichard Smith 973d721916 Some more edits to EnumBase and EnumMaskBase (#6054)
Adds a unit test, and some smaller edits:

- Remove the `=` when defining names, in order to change `}` placement
by clang-format on uses.
- context:
https://github.com/carbon-language/carbon-lang/pull/6053#discussion_r2343423178
- I believe with `EnumBase` that keeping the `=` had been a deliberate
choice, so this PR is intended to confirm that removing it is okay.
- Delete `EnumMaskBase::name`
- context:
https://github.com/carbon-language/carbon-lang/pull/6053#discussion_r2344233707
- We can't just do nothing because `EnumBase::name` uses indexing that's
incompatible with `EnumMaskBase`.
- Some small comment cleanups.
- Tests don't need to be in the `Carbon` namespace anymore, macros work
fine in other namespaces, but it's still the right namespace.
- Documentation on `EnumBase::name` seems to be referring to a prior
structure, wherein we had a macro defining the function instead of the
`Names` array.

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2025-09-12 22:59:37 +00:00

49 lines
1.4 KiB
C++

// 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_kind.h"
#include "llvm/ADT/StringExtras.h"
#include "toolchain/parse/typed_nodes.h"
namespace Carbon::Parse {
CARBON_DEFINE_ENUM_CLASS_NAMES(NodeKind) {
#define CARBON_PARSE_NODE_KIND(Name) CARBON_ENUM_CLASS_NAME_STRING(Name)
#include "toolchain/parse/node_kind.def"
};
// Check that each typed node defines a `token` member.
#define CARBON_PARSE_NODE_KIND(Name) \
static_assert(requires(Name node) { node.token; });
#include "toolchain/parse/node_kind.def"
auto NodeKind::has_bracket() const -> bool {
return definition().has_bracket();
}
auto NodeKind::bracket() const -> NodeKind { return definition().bracket(); }
auto NodeKind::has_child_count() const -> bool {
return definition().has_child_count();
}
auto NodeKind::child_count() const -> int32_t {
return definition().child_count();
}
auto NodeKind::category() const -> NodeCategory {
return definition().category();
}
auto NodeKind::definition() const -> const Definition& {
static constexpr const Definition* Table[] = {
#define CARBON_PARSE_NODE_KIND(Name) &Parse::Name::Kind,
#include "toolchain/parse/node_kind.def"
};
return *Table[AsInt()];
}
} // namespace Carbon::Parse