mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 20:30:14 +01:00
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.
57 lines
1.9 KiB
C++
57 lines
1.9 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
|
|
|
|
#ifndef CARBON_TOOLCHAIN_PARSE_NODE_CATEGORY_H_
|
|
#define CARBON_TOOLCHAIN_PARSE_NODE_CATEGORY_H_
|
|
|
|
#include "common/enum_mask_base.h"
|
|
#include "common/ostream.h"
|
|
|
|
namespace Carbon::Parse {
|
|
|
|
// An X-macro for node categories. Uses should look like:
|
|
//
|
|
// #define CARBON_NODE_CATEGORY_FOR_XYZ(Name) ...
|
|
// CARBON_NODE_CATEGORY(CARBON_NODE_CATEGORY_FOR_XYZ)
|
|
// #undef CARBON_NODE_CATEGORY_FOR_XYZ
|
|
#define CARBON_NODE_CATEGORY(X) \
|
|
X(Decl) \
|
|
X(Expr) \
|
|
X(ImplAs) \
|
|
X(IntConst) \
|
|
X(MemberExpr) \
|
|
X(MemberName) \
|
|
X(Modifier) \
|
|
X(NonExprName) \
|
|
X(PackageName) \
|
|
X(Pattern) \
|
|
X(Requirement) \
|
|
X(Statement)
|
|
|
|
// We expect this to grow, so are using a bigger size than needed.
|
|
CARBON_DEFINE_RAW_ENUM_MASK(NodeCategory, uint32_t) {
|
|
CARBON_NODE_CATEGORY(CARBON_RAW_ENUM_MASK_ENUMERATOR)
|
|
};
|
|
|
|
// Represents a set of keyword modifiers, using a separate bit per modifier.
|
|
class NodeCategory : public CARBON_ENUM_MASK_BASE(NodeCategory) {
|
|
public:
|
|
CARBON_NODE_CATEGORY(CARBON_ENUM_MASK_CONSTANT_DECL)
|
|
|
|
using EnumMaskBase::EnumMaskBase;
|
|
// Provide implicit conversion because the raw enum is used in templates.
|
|
explicit(false) constexpr NodeCategory(RawEnumType value) {
|
|
*this = EnumBase::Make(value);
|
|
}
|
|
};
|
|
|
|
#define CARBON_NODE_CATEGORY_WITH_TYPE(X) \
|
|
CARBON_ENUM_MASK_CONSTANT_DEFINITION(NodeCategory, X)
|
|
CARBON_NODE_CATEGORY(CARBON_NODE_CATEGORY_WITH_TYPE)
|
|
#undef CARBON_NODE_CATEGORY_WITH_TYPE
|
|
|
|
} // namespace Carbon::Parse
|
|
|
|
#endif // CARBON_TOOLCHAIN_PARSE_NODE_CATEGORY_H_
|