11//! Token
22
3- use std:: { fmt, mem, ptr } ;
3+ use std:: { fmt, mem} ;
44
55use oxc_span:: Span ;
66
@@ -27,7 +27,6 @@ const HAS_SEPARATOR_SHIFT: usize = 96;
2727const START_MASK : u128 = 0xFFFF_FFFF ; // 32 bits
2828const END_MASK : u128 = 0xFFFF_FFFF ; // 32 bits
2929const KIND_MASK : u128 = 0xFF ; // 8 bits
30- const BOOL_MASK : u128 = 0xFF ; // 8 bits
3130
3231const _: ( ) = {
3332 // Check flags fields are aligned on 8 and in bounds, so can be read via pointers
@@ -78,10 +77,9 @@ impl fmt::Debug for Token {
7877impl Token {
7978 #[ inline]
8079 pub ( super ) fn new_on_new_line ( ) -> Self {
81- // Start with a default token, then set the flag
82- let mut token = Self :: default ( ) ;
83- token. set_is_on_new_line ( true ) ;
84- token
80+ // Directly create token with is_on_new_line flag set
81+ // All other fields are 0 (same as default)
82+ Self ( 1 << IS_ON_NEW_LINE_SHIFT )
8583 }
8684}
8785
@@ -132,77 +130,45 @@ impl Token {
132130
133131 #[ inline]
134132 pub fn is_on_new_line ( & self ) -> bool {
135- // Use a pointer read rather than arithmetic as it produces less instructions.
136- // SAFETY: 8 bits starting at `IS_ON_NEW_LINE_SHIFT` are only set in `Token::default` and
137- // `Token::set_is_on_new_line`. Both only set these bits to 0 or 1, so valid to read as a `bool`.
138- unsafe { self . read_bool ( IS_ON_NEW_LINE_SHIFT ) }
133+ ( self . 0 >> IS_ON_NEW_LINE_SHIFT ) & 1 != 0
139134 }
140135
141136 #[ inline]
142137 pub ( crate ) fn set_is_on_new_line ( & mut self , value : bool ) {
143- self . 0 &= ! ( BOOL_MASK << IS_ON_NEW_LINE_SHIFT ) ; // Clear current `is_on_new_line` bits
144- self . 0 |= u128:: from ( value) << IS_ON_NEW_LINE_SHIFT ;
138+ self . 0 =
139+ ( self . 0 & ! ( 1 << IS_ON_NEW_LINE_SHIFT ) ) | ( u128:: from ( value) << IS_ON_NEW_LINE_SHIFT ) ;
145140 }
146141
147142 #[ inline]
148143 pub fn escaped ( & self ) -> bool {
149- // Use a pointer read rather than arithmetic as it produces less instructions.
150- // SAFETY: 8 bits starting at `ESCAPED_SHIFT` are only set in `Token::default` and
151- // `Token::set_escaped`. Both only set these bits to 0 or 1, so valid to read as a `bool`.
152- unsafe { self . read_bool ( ESCAPED_SHIFT ) }
144+ ( self . 0 >> ESCAPED_SHIFT ) & 1 != 0
153145 }
154146
155147 #[ inline]
156148 pub ( crate ) fn set_escaped ( & mut self , escaped : bool ) {
157- self . 0 &= !( BOOL_MASK << ESCAPED_SHIFT ) ; // Clear current `escaped` bits
158- self . 0 |= u128:: from ( escaped) << ESCAPED_SHIFT ;
149+ self . 0 = ( self . 0 & !( 1 << ESCAPED_SHIFT ) ) | ( u128:: from ( escaped) << ESCAPED_SHIFT ) ;
159150 }
160151
161152 #[ inline]
162153 pub fn lone_surrogates ( & self ) -> bool {
163- // Use a pointer read rather than arithmetic as it produces less instructions.
164- // SAFETY: 8 bits starting at `LONE_SURROGATES_SHIFT` are only set in `Token::default` and
165- // `Token::set_lone_surrogates`. Both only set these bits to 0 or 1, so valid to read as a `bool`.
166- unsafe { self . read_bool ( LONE_SURROGATES_SHIFT ) }
154+ ( self . 0 >> LONE_SURROGATES_SHIFT ) & 1 != 0
167155 }
168156
169157 #[ inline]
170158 pub ( crate ) fn set_lone_surrogates ( & mut self , value : bool ) {
171- self . 0 &= ! ( BOOL_MASK << LONE_SURROGATES_SHIFT ) ; // Clear current `lone_surrogates` bits
172- self . 0 |= u128:: from ( value) << LONE_SURROGATES_SHIFT ;
159+ self . 0 =
160+ ( self . 0 & ! ( 1 << LONE_SURROGATES_SHIFT ) ) | ( u128:: from ( value) << LONE_SURROGATES_SHIFT ) ;
173161 }
174162
175163 #[ inline]
176164 pub fn has_separator ( & self ) -> bool {
177- // Use a pointer read rather than arithmetic as it produces less instructions.
178- // SAFETY: 8 bits starting at `HAS_SEPARATOR_SHIFT` are only set in `Token::default` and
179- // `Token::set_has_separator`. Both only set these bits to 0 or 1, so valid to read as a `bool`.
180- unsafe { self . read_bool ( HAS_SEPARATOR_SHIFT ) }
165+ ( self . 0 >> HAS_SEPARATOR_SHIFT ) & 1 != 0
181166 }
182167
183168 #[ inline]
184169 pub ( crate ) fn set_has_separator ( & mut self , value : bool ) {
185- self . 0 &= !( BOOL_MASK << HAS_SEPARATOR_SHIFT ) ; // Clear current `has_separator` bits
186- self . 0 |= u128:: from ( value) << HAS_SEPARATOR_SHIFT ;
187- }
188-
189- /// Read `bool` from 8 bits starting at bit position `shift`.
190- ///
191- /// # SAFETY
192- /// `shift` must be the location of a valid boolean "field" in [`Token`]
193- /// e.g. `ESCAPED_SHIFT`
194- #[ expect( clippy:: inline_always) ]
195- #[ inline( always) ] // So `shift` is statically known
196- unsafe fn read_bool ( & self , shift : usize ) -> bool {
197- // Byte offset depends on endianness of the system
198- let offset = if cfg ! ( target_endian = "little" ) { shift / 8 } else { 15 - ( shift / 8 ) } ;
199- // SAFETY: Caller guarantees `shift` points to valid `bool`.
200- // This method borrows `Token`, so valid to read field via a reference - can't be aliased.
201- unsafe {
202- let field_ptr = ptr:: from_ref ( self ) . cast :: < bool > ( ) . add ( offset) ;
203- debug_assert ! ( * field_ptr. cast:: <u8 >( ) <= 1 ) ;
204- * field_ptr. as_ref ( ) . unwrap_unchecked ( )
205- }
170+ self . 0 =
171+ ( self . 0 & !( 1 << HAS_SEPARATOR_SHIFT ) ) | ( u128:: from ( value) << HAS_SEPARATOR_SHIFT ) ;
206172 }
207173}
208174
0 commit comments