diff --git a/crates/tsrs_ast/src/ast.rs b/crates/tsrs_ast/src/ast.rs index 28458b7e2..8877f76dd 100644 --- a/crates/tsrs_ast/src/ast.rs +++ b/crates/tsrs_ast/src/ast.rs @@ -130,7 +130,7 @@ pub fn new_node_factory_ex(hooks: NodeFactoryHooks, scratch: bool) -> NodeFactor fn node_header(kind: Kind, data_tag: NodeDataTag) -> Node { Node { - header: OwnedCell::new(NodeHeaderWord::new(kind, data_tag)), + header: NodeHeader::new(kind, data_tag), flags: OwnedCell::new(NodeFlags::None), id: AtomicU32::new(0), loc: OwnedCell::new(undefined_text_range()), @@ -182,7 +182,7 @@ fn new_node_with_rare_hooked( ) -> P { let () = T::SAME_OFFSET; let mut header = node_header(kind, T::TAG); - header.header = OwnedCell::new(header.header.get().with_rare_tail()); + header.header = header.header.with_rare_tail(); // SAFETY: as in `new_node` (`NodeAllocRare` is `repr(C)` with the header first). let n = unsafe { P::new(NodeAllocRare { node: header, data, rare }).cast::() }; if let Some(on_create) = &hooks.on_create { @@ -200,7 +200,7 @@ fn new_node_with_rare_scratch( ) -> P { let () = T::SAME_OFFSET; let mut header = node_header(kind, T::TAG); - header.header = OwnedCell::new(header.header.get().with_rare_tail()); + header.header = header.header.with_rare_tail(); // SAFETY: as in `new_node_with_rare_hooked`, but this allocation is owned by the active scratch region. let n = unsafe { P::new_scratch(NodeAllocRare { node: header, data, rare }).cast::() }; if let Some(on_create) = &hooks.on_create { @@ -213,7 +213,7 @@ fn new_node_with_rare_scratch( fn new_node_with_rare_plain(kind: Kind, data: T, rare: T::Rare) -> P { let () = T::SAME_OFFSET; let mut header = node_header(kind, T::TAG); - header.header = OwnedCell::new(header.header.get().with_rare_tail()); + header.header = header.header.with_rare_tail(); // SAFETY: as in `new_node_plain` (`NodeAllocRare` is `repr(C)` with the header first). unsafe { P::new(NodeAllocRare { node: header, data, rare }).cast::() } } @@ -227,7 +227,7 @@ pub(crate) fn rare_tail(data: &T) -> Option<&'static T::Rare> { let node: &Node = unsafe { tsrs_core::ptr::arena_neighbor(data, -(std::mem::offset_of!(NodeAlloc, data) as isize)) }; - if !node.header.get().has_rare_tail() { + if !node.header.has_rare_tail() { return None; } let off = std::mem::offset_of!(NodeAllocRare, rare) @@ -529,11 +529,12 @@ impl ModifierList { /// `NodeData`, `as_*()` and the generated accessors read it in place. Data structs without fields (`Token`, /// `KeywordTypeNode`, ...) allocate only the header. /// -/// The header is 24 bytes: the kind, the data tag and the parent pointer share one word (`NodeHeaderWord`), -/// and the id is stored in 32 bits (ordinary ids are 31-bit; the high bit tags ids from a checker's reserved +/// The header is 24 bytes. Four-byte parent handles share eight bytes with separately stored kind, data tag +/// and marker fields; eight-byte pointers use the packed `NodeHeaderWord`. The id is stored in 32 bits +/// (ordinary ids are 31-bit; the high bit tags ids from a checker's reserved /// 64-bit block range, decoded by `get_node_id`). pub struct Node { - header: OwnedCell, + header: NodeHeader, pub flags: OwnedCell, pub(crate) id: AtomicU32, pub(crate) loc: OwnedCell, // set_loc() (compact identifiers derive their text from the end) @@ -541,6 +542,151 @@ pub struct Node { const _: () = assert!(std::mem::size_of::() == 24); +/// Separates immutable metadata from four-byte parents while retaining the packed form for eight-byte pointers. +type NodeHeader = <[(); std::mem::size_of::>()] as NodeHeaderLayout>::Header; + +trait NodeHeaderLayout { + type Header: NodeHeaderAccess; +} + +impl NodeHeaderLayout for [(); 4] { + type Header = HandleNodeHeader; +} + +impl NodeHeaderLayout for [(); 8] { + type Header = PointerNodeHeader; +} + +trait NodeHeaderAccess: Sized { + fn new(kind: Kind, data_tag: NodeDataTag) -> Self; + fn kind(&self) -> Kind; + fn data_tag(&self) -> NodeDataTag; + fn parent(&self) -> Option>; + fn set_parent(&self, parent: Option>); + fn with_rare_tail(self) -> Self; + fn has_rare_tail(&self) -> bool; + fn has_trailing_comma(&self) -> bool; + fn mark_trailing_comma(&self); +} + +#[repr(C)] +struct HandleNodeHeader { + parent: OwnedCell>>, + kind: Kind, + data_tag: NodeDataTag, + markers: OwnedCell, + _word_alignment: [u64; 0], +} + +impl NodeHeaderAccess for HandleNodeHeader { + #[inline] + fn new(kind: Kind, data_tag: NodeDataTag) -> Self { + Self { + parent: OwnedCell::new(None), + kind, + data_tag, + markers: OwnedCell::new(0), + _word_alignment: [], + } + } + + #[inline] + fn kind(&self) -> Kind { + self.kind + } + + #[inline] + fn data_tag(&self) -> NodeDataTag { + self.data_tag + } + + #[inline] + fn parent(&self) -> Option> { + self.parent.get() + } + + #[inline] + fn set_parent(&self, parent: Option>) { + self.parent.set(parent); + } + + #[inline] + fn with_rare_tail(mut self) -> Self { + self.markers = OwnedCell::new(self.markers.get() | 1); + self + } + + #[inline] + fn has_rare_tail(&self) -> bool { + self.markers.get() & 1 != 0 + } + + #[inline] + fn has_trailing_comma(&self) -> bool { + self.markers.get() & 2 != 0 + } + + #[inline] + fn mark_trailing_comma(&self) { + self.markers.set(self.markers.get() | 2); + } +} + +struct PointerNodeHeader { + word: OwnedCell, +} + +impl NodeHeaderAccess for PointerNodeHeader { + #[inline] + fn new(kind: Kind, data_tag: NodeDataTag) -> Self { + Self { word: OwnedCell::new(NodeHeaderWord::new(kind, data_tag)) } + } + + #[inline] + fn kind(&self) -> Kind { + self.word.get().kind() + } + + #[inline] + fn data_tag(&self) -> NodeDataTag { + self.word.get().data_tag() + } + + #[inline] + fn parent(&self) -> Option> { + self.word.get().parent() + } + + #[inline] + fn set_parent(&self, parent: Option>) { + self.word.set(self.word.get().with_parent(parent)); + } + + #[inline] + fn with_rare_tail(mut self) -> Self { + self.word = OwnedCell::new(self.word.get().with_rare_tail()); + self + } + + #[inline] + fn has_rare_tail(&self) -> bool { + self.word.get().has_rare_tail() + } + + #[inline] + fn has_trailing_comma(&self) -> bool { + self.word.get().has_trailing_comma() + } + + #[inline] + fn mark_trailing_comma(&self) { + self.word.set(self.word.get().with_trailing_comma()); + } +} + +const _: () = assert!(std::mem::size_of::() == 8); +const _: () = assert!(std::mem::align_of::() == std::mem::align_of::()); + /// A node's kind, data tag and parent in one word: the parent in the low 45 bits (`P::pack`), the kind in the next 9 bits, the data /// tag in the 8 above, the rare bit (the data struct is followed by its rare tail, `NodeRareTail`) and the /// trailing-comma bit. The latter replaces the old `-2` synthetic-position sentinel. Only the parent and @@ -664,13 +810,13 @@ pub(crate) trait NodeRareTail: NodePayload { impl Node { #[inline] pub fn kind(&self) -> Kind { - self.header.get().kind() + self.header.kind() } /// Which data struct follows this node's header. #[inline] pub fn data_tag(&self) -> NodeDataTag { - self.header.get().data_tag() + self.header.data_tag() } /// The data struct after this node's header. Callers check `data_tag() == T::TAG` first. @@ -722,21 +868,21 @@ impl Node { } #[inline] pub fn parent(&self) -> Option> { - self.header.get().parent() + self.header.parent() } #[inline] pub fn set_parent(&self, parent: Option>) { - self.header.set(self.header.get().with_parent(parent)); + self.header.set_parent(parent); } #[inline] pub fn has_trailing_comma_marker(&self) -> bool { - self.header.get().has_trailing_comma() + self.header.has_trailing_comma() } #[inline] pub fn mark_trailing_comma(&self) { - self.header.set(self.header.get().with_trailing_comma()); + self.header.mark_trailing_comma(); } /// Go `IterChildren`: the children in `for_each_child` order. diff --git a/crates/tsrs_ast/tests/integration/ast_test.rs b/crates/tsrs_ast/tests/integration/ast_test.rs index 3fd1ede15..62cf330bd 100644 --- a/crates/tsrs_ast/tests/integration/ast_test.rs +++ b/crates/tsrs_ast/tests/integration/ast_test.rs @@ -380,10 +380,11 @@ fn kinds_and_flags() { .intersects(SymbolFlags::FunctionScopedVariable) ); assert_eq!(std::mem::size_of::(), 24); + assert_eq!(std::mem::align_of::(), std::mem::align_of::()); } #[test] -fn node_header_word_keeps_kind_tag_and_parent() { +fn node_header_keeps_metadata_parent_markers_and_rare_fields() { let f = NodeFactory::default(); let parent = f.new_keyword_expression(Kind::ThisKeyword); let child = f.new_token(Kind::Unknown); @@ -402,4 +403,22 @@ fn node_header_word_keeps_kind_tag_and_parent() { ); child.set_parent(None); assert_eq!((child.kind(), child.parent()), (Kind::Unknown, None)); + + let question = f.new_token(Kind::QuestionDotToken); + let access = f.new_property_access_expression( + parent, + Some(question), + f.new_identifier("field"), + NodeFlags::OptionalChain, + ); + assert!(!access.has_trailing_comma_marker()); + for target in [Some(parent), None] { + access.set_parent(target); + access.mark_trailing_comma(); + assert_eq!(access.kind(), Kind::PropertyAccessExpression); + assert_eq!(access.data_tag(), NodeDataTag::PropertyAccessExpression); + assert_eq!(access.parent(), target); + assert!(access.has_trailing_comma_marker()); + assert_eq!(access.as_property_access_expression().question_dot_token(), Some(question)); + } }