oxc icon indicating copy to clipboard operation
oxc copied to clipboard

refactor(semantic): simplify getting `AstNodeId`s for CFG

Open overlookmotel opened this issue 5 months ago • 4 comments

Remove ast_node_records, and the functions that push and pop it, from SemanticBuilder.

The purpose of that apparatus is to do something quite simple - get the AstNodeId of an Expression from its parent, for the control flow graph (CFG). As nodes are created in order, a simpler way to do this is to get what the next AstNodeId will be before visiting the Expression.

This implementation has to work around a current shortcoming with IndexVec - converting Nodes::len() to an AstNodeId involves a panicking bounds-check, which adds overhead even when CFG is disabled. So for now get_next_node_id returns the "id" as a u32, and we convert it to AstNodeId only after the node has been created, which guarantees that the u32 is a valid AstNodeId (not out of bounds), because IndexVec::push also performs that same bounds check.

If we implement https://github.com/oxc-project/backlog/issues/109, get_next_node_index could become get_next_node_id and return an AstNodeId, and the unsafe code could be removed.

overlookmotel avatar Sep 10 '24 10:09 overlookmotel