Skip to content

Commit

Permalink
Support implicit conversion from specific node IDs to node ID categor…
Browse files Browse the repository at this point in the history
…ies (carbon-language#3799)

Co-authored-by: Josh L <[email protected]>
  • Loading branch information
josh11b and josh11b authored Mar 20, 2024
1 parent cf361a8 commit f97a543
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
13 changes: 4 additions & 9 deletions toolchain/check/handle_operator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,18 @@
namespace Carbon::Check {

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

// Common logic for binary operator handlers.
static auto HandleBinaryOperator(Context& context, Parse::NodeId node_id,
Operator op) -> bool {
// TODO: Support implicit conversion from specific node IDs to node ID
// categories and change this function to take an `AnyExprId` directly.
auto expr_node_id = static_cast<Parse::AnyExprId>(node_id);
static auto HandleBinaryOperator(Context& context,
Parse::AnyExprId expr_node_id, Operator op)
-> bool {
auto rhs_id = context.node_stack().PopExpr();
auto lhs_id = context.node_stack().PopExpr();
auto result_id =
Expand Down
7 changes: 6 additions & 1 deletion toolchain/parse/node_ids.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ const NodeKind& NodeIdForKind<K>::Kind = K;
// NodeId that matches any NodeKind whose `category()` overlaps with `Category`.
template <NodeCategory Category>
struct NodeIdInCategory : public NodeId {
// TODO: Support conversion from `NodeIdForKind<Kind>` if `Kind::category()`
// Support conversion from `NodeIdForKind<Kind>` if Kind's category
// overlaps with `Category`.
template <const NodeKind& Kind>
// NOLINTNEXTLINE(google-explicit-constructor)
NodeIdInCategory(NodeIdForKind<Kind> node_id) : NodeId(node_id) {
CARBON_CHECK(!!(Kind.category() & Category));
}

constexpr explicit NodeIdInCategory(NodeId node_id) : NodeId(node_id) {}
// NOLINTNEXTLINE(google-explicit-constructor)
Expand Down

0 comments on commit f97a543

Please sign in to comment.