Skip to content

Commit f97a543

Browse files
authored
Support implicit conversion from specific node IDs to node ID categories (#3799)
Co-authored-by: Josh L <[email protected]>
1 parent cf361a8 commit f97a543

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

toolchain/check/handle_operator.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,18 @@
1111
namespace Carbon::Check {
1212

1313
// Common logic for unary operator handlers.
14-
static auto HandleUnaryOperator(Context& context, Parse::NodeId node_id,
14+
static auto HandleUnaryOperator(Context& context, Parse::AnyExprId expr_node_id,
1515
Operator op) -> bool {
16-
// TODO: Support implicit conversion from specific node IDs to node ID
17-
// categories and change this function to take an `AnyExprId` directly.
18-
auto expr_node_id = static_cast<Parse::AnyExprId>(node_id);
1916
auto operand_id = context.node_stack().PopExpr();
2017
auto result_id = BuildUnaryOperator(context, expr_node_id, op, operand_id);
2118
context.node_stack().Push(expr_node_id, result_id);
2219
return true;
2320
}
2421

2522
// Common logic for binary operator handlers.
26-
static auto HandleBinaryOperator(Context& context, Parse::NodeId node_id,
27-
Operator op) -> bool {
28-
// TODO: Support implicit conversion from specific node IDs to node ID
29-
// categories and change this function to take an `AnyExprId` directly.
30-
auto expr_node_id = static_cast<Parse::AnyExprId>(node_id);
23+
static auto HandleBinaryOperator(Context& context,
24+
Parse::AnyExprId expr_node_id, Operator op)
25+
-> bool {
3126
auto rhs_id = context.node_stack().PopExpr();
3227
auto lhs_id = context.node_stack().PopExpr();
3328
auto result_id =

toolchain/parse/node_ids.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,13 @@ const NodeKind& NodeIdForKind<K>::Kind = K;
5252
// NodeId that matches any NodeKind whose `category()` overlaps with `Category`.
5353
template <NodeCategory Category>
5454
struct NodeIdInCategory : public NodeId {
55-
// TODO: Support conversion from `NodeIdForKind<Kind>` if `Kind::category()`
55+
// Support conversion from `NodeIdForKind<Kind>` if Kind's category
5656
// overlaps with `Category`.
57+
template <const NodeKind& Kind>
58+
// NOLINTNEXTLINE(google-explicit-constructor)
59+
NodeIdInCategory(NodeIdForKind<Kind> node_id) : NodeId(node_id) {
60+
CARBON_CHECK(!!(Kind.category() & Category));
61+
}
5762

5863
constexpr explicit NodeIdInCategory(NodeId node_id) : NodeId(node_id) {}
5964
// NOLINTNEXTLINE(google-explicit-constructor)

0 commit comments

Comments
 (0)