From 42087331807d6c6354d712da356e6a8037d96b23 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Sat, 11 May 2024 04:39:39 +0000 Subject: [PATCH] refactor(ast): order AST type fields in visitation order (#3228) Reorder the fields in a couple of AST types. `walk_*` functions in `oxc_traverse` visit the fields in the order they appear in AST. So this change means that decorators are visited before classes and methods that they decorate. --- crates/oxc_ast/src/ast/js.rs | 4 +- crates/oxc_ast/src/ast_builder.rs | 2 +- crates/oxc_traverse/src/ancestor.rs | 1088 +++++++++++++-------------- crates/oxc_traverse/src/walk.rs | 32 +- 4 files changed, 564 insertions(+), 562 deletions(-) diff --git a/crates/oxc_ast/src/ast/js.rs b/crates/oxc_ast/src/ast/js.rs index 44abfb9cd..930b2f954 100644 --- a/crates/oxc_ast/src/ast/js.rs +++ b/crates/oxc_ast/src/ast/js.rs @@ -2343,13 +2343,13 @@ pub struct Class<'a> { pub r#type: ClassType, #[cfg_attr(feature = "serialize", serde(flatten))] pub span: Span, + pub decorators: Vec<'a, Decorator<'a>>, pub id: Option>, pub super_class: Option>, pub body: Box<'a, ClassBody<'a>>, pub type_parameters: Option>>, pub super_type_parameters: Option>>, pub implements: Option>>, - pub decorators: Vec<'a, Decorator<'a>>, /// Valid Modifiers: `export`, `abstract` pub modifiers: Modifiers<'a>, } @@ -2499,6 +2499,7 @@ pub struct MethodDefinition<'a> { pub r#type: MethodDefinitionType, #[cfg_attr(feature = "serialize", serde(flatten))] pub span: Span, + pub decorators: Vec<'a, Decorator<'a>>, pub key: PropertyKey<'a>, pub value: Box<'a, Function<'a>>, // FunctionExpression pub kind: MethodDefinitionKind, @@ -2507,7 +2508,6 @@ pub struct MethodDefinition<'a> { pub r#override: bool, pub optional: bool, pub accessibility: Option, - pub decorators: Vec<'a, Decorator<'a>>, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] diff --git a/crates/oxc_ast/src/ast_builder.rs b/crates/oxc_ast/src/ast_builder.rs index c12ab5c29..06584ea18 100644 --- a/crates/oxc_ast/src/ast_builder.rs +++ b/crates/oxc_ast/src/ast_builder.rs @@ -937,13 +937,13 @@ impl<'a> AstBuilder<'a> { self.alloc(Class { r#type, span, + decorators, id, super_class, body, type_parameters, super_type_parameters, implements, - decorators, modifiers, }) } diff --git a/crates/oxc_traverse/src/ancestor.rs b/crates/oxc_traverse/src/ancestor.rs index 5f2516255..d6f7b9c00 100644 --- a/crates/oxc_traverse/src/ancestor.rs +++ b/crates/oxc_traverse/src/ancestor.rs @@ -151,17 +151,17 @@ pub(crate) enum AncestorType { ArrowFunctionExpressionTypeParameters = 124, ArrowFunctionExpressionReturnType = 125, YieldExpressionArgument = 126, - ClassId = 127, - ClassSuperClass = 128, - ClassBody = 129, - ClassTypeParameters = 130, - ClassSuperTypeParameters = 131, - ClassImplements = 132, - ClassDecorators = 133, + ClassDecorators = 127, + ClassId = 128, + ClassSuperClass = 129, + ClassBody = 130, + ClassTypeParameters = 131, + ClassSuperTypeParameters = 132, + ClassImplements = 133, ClassBodyBody = 134, - MethodDefinitionKey = 135, - MethodDefinitionValue = 136, - MethodDefinitionDecorators = 137, + MethodDefinitionDecorators = 135, + MethodDefinitionKey = 136, + MethodDefinitionValue = 137, PropertyDefinitionKey = 138, PropertyDefinitionValue = 139, PropertyDefinitionTypeAnnotation = 140, @@ -547,6 +547,7 @@ pub enum Ancestor<'a> { AncestorType::ArrowFunctionExpressionReturnType as u16, YieldExpressionArgument(YieldExpressionWithoutArgument<'a>) = AncestorType::YieldExpressionArgument as u16, + ClassDecorators(ClassWithoutDecorators<'a>) = AncestorType::ClassDecorators as u16, ClassId(ClassWithoutId<'a>) = AncestorType::ClassId as u16, ClassSuperClass(ClassWithoutSuperClass<'a>) = AncestorType::ClassSuperClass as u16, ClassBody(ClassWithoutBody<'a>) = AncestorType::ClassBody as u16, @@ -554,13 +555,12 @@ pub enum Ancestor<'a> { ClassSuperTypeParameters(ClassWithoutSuperTypeParameters<'a>) = AncestorType::ClassSuperTypeParameters as u16, ClassImplements(ClassWithoutImplements<'a>) = AncestorType::ClassImplements as u16, - ClassDecorators(ClassWithoutDecorators<'a>) = AncestorType::ClassDecorators as u16, ClassBodyBody(ClassBodyWithoutBody<'a>) = AncestorType::ClassBodyBody as u16, + MethodDefinitionDecorators(MethodDefinitionWithoutDecorators<'a>) = + AncestorType::MethodDefinitionDecorators as u16, MethodDefinitionKey(MethodDefinitionWithoutKey<'a>) = AncestorType::MethodDefinitionKey as u16, MethodDefinitionValue(MethodDefinitionWithoutValue<'a>) = AncestorType::MethodDefinitionValue as u16, - MethodDefinitionDecorators(MethodDefinitionWithoutDecorators<'a>) = - AncestorType::MethodDefinitionDecorators as u16, PropertyDefinitionKey(PropertyDefinitionWithoutKey<'a>) = AncestorType::PropertyDefinitionKey as u16, PropertyDefinitionValue(PropertyDefinitionWithoutValue<'a>) = @@ -1268,13 +1268,13 @@ impl<'a> Ancestor<'a> { pub fn is_class(&self) -> bool { matches!( self, - Self::ClassId(_) + Self::ClassDecorators(_) + | Self::ClassId(_) | Self::ClassSuperClass(_) | Self::ClassBody(_) | Self::ClassTypeParameters(_) | Self::ClassSuperTypeParameters(_) | Self::ClassImplements(_) - | Self::ClassDecorators(_) ) } @@ -1287,9 +1287,9 @@ impl<'a> Ancestor<'a> { pub fn is_method_definition(&self) -> bool { matches!( self, - Self::MethodDefinitionKey(_) + Self::MethodDefinitionDecorators(_) + | Self::MethodDefinitionKey(_) | Self::MethodDefinitionValue(_) - | Self::MethodDefinitionDecorators(_) ) } @@ -5886,6 +5886,7 @@ impl<'a> YieldExpressionWithoutArgument<'a> { pub(crate) const OFFSET_CLASS_TYPE: usize = offset_of!(Class, r#type); pub(crate) const OFFSET_CLASS_SPAN: usize = offset_of!(Class, span); +pub(crate) const OFFSET_CLASS_DECORATORS: usize = offset_of!(Class, decorators); pub(crate) const OFFSET_CLASS_ID: usize = offset_of!(Class, id); pub(crate) const OFFSET_CLASS_SUPER_CLASS: usize = offset_of!(Class, super_class); pub(crate) const OFFSET_CLASS_BODY: usize = offset_of!(Class, body); @@ -5893,392 +5894,8 @@ pub(crate) const OFFSET_CLASS_TYPE_PARAMETERS: usize = offset_of!(Class, type_pa pub(crate) const OFFSET_CLASS_SUPER_TYPE_PARAMETERS: usize = offset_of!(Class, super_type_parameters); pub(crate) const OFFSET_CLASS_IMPLEMENTS: usize = offset_of!(Class, implements); -pub(crate) const OFFSET_CLASS_DECORATORS: usize = offset_of!(Class, decorators); pub(crate) const OFFSET_CLASS_MODIFIERS: usize = offset_of!(Class, modifiers); -#[repr(transparent)] -#[derive(Debug)] -pub struct ClassWithoutId<'a>(pub(crate) *const Class<'a>); - -impl<'a> ClassWithoutId<'a> { - #[inline] - pub fn r#type(&self) -> &ClassType { - unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_TYPE) as *const ClassType) } - } - - #[inline] - pub fn span(&self) -> &Span { - unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_SPAN) as *const Span) } - } - - #[inline] - pub fn super_class(&self) -> &Option> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_SUPER_CLASS) as *const Option>) - } - } - - #[inline] - pub fn body(&self) -> &Box<'a, ClassBody<'a>> { - unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_BODY) as *const Box<'a, ClassBody<'a>>) } - } - - #[inline] - pub fn type_parameters(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_TYPE_PARAMETERS) - as *const Option>>) - } - } - - #[inline] - pub fn super_type_parameters(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_SUPER_TYPE_PARAMETERS) - as *const Option>>) - } - } - - #[inline] - pub fn implements(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_IMPLEMENTS) - as *const Option>>) - } - } - - #[inline] - pub fn decorators(&self) -> &Vec<'a, Decorator<'a>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_DECORATORS) as *const Vec<'a, Decorator<'a>>) - } - } - - #[inline] - pub fn modifiers(&self) -> &Modifiers<'a> { - unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_MODIFIERS) as *const Modifiers<'a>) } - } -} - -#[repr(transparent)] -#[derive(Debug)] -pub struct ClassWithoutSuperClass<'a>(pub(crate) *const Class<'a>); - -impl<'a> ClassWithoutSuperClass<'a> { - #[inline] - pub fn r#type(&self) -> &ClassType { - unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_TYPE) as *const ClassType) } - } - - #[inline] - pub fn span(&self) -> &Span { - unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_SPAN) as *const Span) } - } - - #[inline] - pub fn id(&self) -> &Option> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_ID) as *const Option>) - } - } - - #[inline] - pub fn body(&self) -> &Box<'a, ClassBody<'a>> { - unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_BODY) as *const Box<'a, ClassBody<'a>>) } - } - - #[inline] - pub fn type_parameters(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_TYPE_PARAMETERS) - as *const Option>>) - } - } - - #[inline] - pub fn super_type_parameters(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_SUPER_TYPE_PARAMETERS) - as *const Option>>) - } - } - - #[inline] - pub fn implements(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_IMPLEMENTS) - as *const Option>>) - } - } - - #[inline] - pub fn decorators(&self) -> &Vec<'a, Decorator<'a>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_DECORATORS) as *const Vec<'a, Decorator<'a>>) - } - } - - #[inline] - pub fn modifiers(&self) -> &Modifiers<'a> { - unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_MODIFIERS) as *const Modifiers<'a>) } - } -} - -#[repr(transparent)] -#[derive(Debug)] -pub struct ClassWithoutBody<'a>(pub(crate) *const Class<'a>); - -impl<'a> ClassWithoutBody<'a> { - #[inline] - pub fn r#type(&self) -> &ClassType { - unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_TYPE) as *const ClassType) } - } - - #[inline] - pub fn span(&self) -> &Span { - unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_SPAN) as *const Span) } - } - - #[inline] - pub fn id(&self) -> &Option> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_ID) as *const Option>) - } - } - - #[inline] - pub fn super_class(&self) -> &Option> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_SUPER_CLASS) as *const Option>) - } - } - - #[inline] - pub fn type_parameters(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_TYPE_PARAMETERS) - as *const Option>>) - } - } - - #[inline] - pub fn super_type_parameters(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_SUPER_TYPE_PARAMETERS) - as *const Option>>) - } - } - - #[inline] - pub fn implements(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_IMPLEMENTS) - as *const Option>>) - } - } - - #[inline] - pub fn decorators(&self) -> &Vec<'a, Decorator<'a>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_DECORATORS) as *const Vec<'a, Decorator<'a>>) - } - } - - #[inline] - pub fn modifiers(&self) -> &Modifiers<'a> { - unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_MODIFIERS) as *const Modifiers<'a>) } - } -} - -#[repr(transparent)] -#[derive(Debug)] -pub struct ClassWithoutTypeParameters<'a>(pub(crate) *const Class<'a>); - -impl<'a> ClassWithoutTypeParameters<'a> { - #[inline] - pub fn r#type(&self) -> &ClassType { - unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_TYPE) as *const ClassType) } - } - - #[inline] - pub fn span(&self) -> &Span { - unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_SPAN) as *const Span) } - } - - #[inline] - pub fn id(&self) -> &Option> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_ID) as *const Option>) - } - } - - #[inline] - pub fn super_class(&self) -> &Option> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_SUPER_CLASS) as *const Option>) - } - } - - #[inline] - pub fn body(&self) -> &Box<'a, ClassBody<'a>> { - unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_BODY) as *const Box<'a, ClassBody<'a>>) } - } - - #[inline] - pub fn super_type_parameters(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_SUPER_TYPE_PARAMETERS) - as *const Option>>) - } - } - - #[inline] - pub fn implements(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_IMPLEMENTS) - as *const Option>>) - } - } - - #[inline] - pub fn decorators(&self) -> &Vec<'a, Decorator<'a>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_DECORATORS) as *const Vec<'a, Decorator<'a>>) - } - } - - #[inline] - pub fn modifiers(&self) -> &Modifiers<'a> { - unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_MODIFIERS) as *const Modifiers<'a>) } - } -} - -#[repr(transparent)] -#[derive(Debug)] -pub struct ClassWithoutSuperTypeParameters<'a>(pub(crate) *const Class<'a>); - -impl<'a> ClassWithoutSuperTypeParameters<'a> { - #[inline] - pub fn r#type(&self) -> &ClassType { - unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_TYPE) as *const ClassType) } - } - - #[inline] - pub fn span(&self) -> &Span { - unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_SPAN) as *const Span) } - } - - #[inline] - pub fn id(&self) -> &Option> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_ID) as *const Option>) - } - } - - #[inline] - pub fn super_class(&self) -> &Option> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_SUPER_CLASS) as *const Option>) - } - } - - #[inline] - pub fn body(&self) -> &Box<'a, ClassBody<'a>> { - unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_BODY) as *const Box<'a, ClassBody<'a>>) } - } - - #[inline] - pub fn type_parameters(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_TYPE_PARAMETERS) - as *const Option>>) - } - } - - #[inline] - pub fn implements(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_IMPLEMENTS) - as *const Option>>) - } - } - - #[inline] - pub fn decorators(&self) -> &Vec<'a, Decorator<'a>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_DECORATORS) as *const Vec<'a, Decorator<'a>>) - } - } - - #[inline] - pub fn modifiers(&self) -> &Modifiers<'a> { - unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_MODIFIERS) as *const Modifiers<'a>) } - } -} - -#[repr(transparent)] -#[derive(Debug)] -pub struct ClassWithoutImplements<'a>(pub(crate) *const Class<'a>); - -impl<'a> ClassWithoutImplements<'a> { - #[inline] - pub fn r#type(&self) -> &ClassType { - unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_TYPE) as *const ClassType) } - } - - #[inline] - pub fn span(&self) -> &Span { - unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_SPAN) as *const Span) } - } - - #[inline] - pub fn id(&self) -> &Option> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_ID) as *const Option>) - } - } - - #[inline] - pub fn super_class(&self) -> &Option> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_SUPER_CLASS) as *const Option>) - } - } - - #[inline] - pub fn body(&self) -> &Box<'a, ClassBody<'a>> { - unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_BODY) as *const Box<'a, ClassBody<'a>>) } - } - - #[inline] - pub fn type_parameters(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_TYPE_PARAMETERS) - as *const Option>>) - } - } - - #[inline] - pub fn super_type_parameters(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_SUPER_TYPE_PARAMETERS) - as *const Option>>) - } - } - - #[inline] - pub fn decorators(&self) -> &Vec<'a, Decorator<'a>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_CLASS_DECORATORS) as *const Vec<'a, Decorator<'a>>) - } - } - - #[inline] - pub fn modifiers(&self) -> &Modifiers<'a> { - unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_MODIFIERS) as *const Modifiers<'a>) } - } -} - #[repr(transparent)] #[derive(Debug)] pub struct ClassWithoutDecorators<'a>(pub(crate) *const Class<'a>); @@ -6343,6 +5960,389 @@ impl<'a> ClassWithoutDecorators<'a> { } } +#[repr(transparent)] +#[derive(Debug)] +pub struct ClassWithoutId<'a>(pub(crate) *const Class<'a>); + +impl<'a> ClassWithoutId<'a> { + #[inline] + pub fn r#type(&self) -> &ClassType { + unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_TYPE) as *const ClassType) } + } + + #[inline] + pub fn span(&self) -> &Span { + unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_SPAN) as *const Span) } + } + + #[inline] + pub fn decorators(&self) -> &Vec<'a, Decorator<'a>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_DECORATORS) as *const Vec<'a, Decorator<'a>>) + } + } + + #[inline] + pub fn super_class(&self) -> &Option> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_SUPER_CLASS) as *const Option>) + } + } + + #[inline] + pub fn body(&self) -> &Box<'a, ClassBody<'a>> { + unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_BODY) as *const Box<'a, ClassBody<'a>>) } + } + + #[inline] + pub fn type_parameters(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_TYPE_PARAMETERS) + as *const Option>>) + } + } + + #[inline] + pub fn super_type_parameters(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_SUPER_TYPE_PARAMETERS) + as *const Option>>) + } + } + + #[inline] + pub fn implements(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_IMPLEMENTS) + as *const Option>>) + } + } + + #[inline] + pub fn modifiers(&self) -> &Modifiers<'a> { + unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_MODIFIERS) as *const Modifiers<'a>) } + } +} + +#[repr(transparent)] +#[derive(Debug)] +pub struct ClassWithoutSuperClass<'a>(pub(crate) *const Class<'a>); + +impl<'a> ClassWithoutSuperClass<'a> { + #[inline] + pub fn r#type(&self) -> &ClassType { + unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_TYPE) as *const ClassType) } + } + + #[inline] + pub fn span(&self) -> &Span { + unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_SPAN) as *const Span) } + } + + #[inline] + pub fn decorators(&self) -> &Vec<'a, Decorator<'a>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_DECORATORS) as *const Vec<'a, Decorator<'a>>) + } + } + + #[inline] + pub fn id(&self) -> &Option> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_ID) as *const Option>) + } + } + + #[inline] + pub fn body(&self) -> &Box<'a, ClassBody<'a>> { + unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_BODY) as *const Box<'a, ClassBody<'a>>) } + } + + #[inline] + pub fn type_parameters(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_TYPE_PARAMETERS) + as *const Option>>) + } + } + + #[inline] + pub fn super_type_parameters(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_SUPER_TYPE_PARAMETERS) + as *const Option>>) + } + } + + #[inline] + pub fn implements(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_IMPLEMENTS) + as *const Option>>) + } + } + + #[inline] + pub fn modifiers(&self) -> &Modifiers<'a> { + unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_MODIFIERS) as *const Modifiers<'a>) } + } +} + +#[repr(transparent)] +#[derive(Debug)] +pub struct ClassWithoutBody<'a>(pub(crate) *const Class<'a>); + +impl<'a> ClassWithoutBody<'a> { + #[inline] + pub fn r#type(&self) -> &ClassType { + unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_TYPE) as *const ClassType) } + } + + #[inline] + pub fn span(&self) -> &Span { + unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_SPAN) as *const Span) } + } + + #[inline] + pub fn decorators(&self) -> &Vec<'a, Decorator<'a>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_DECORATORS) as *const Vec<'a, Decorator<'a>>) + } + } + + #[inline] + pub fn id(&self) -> &Option> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_ID) as *const Option>) + } + } + + #[inline] + pub fn super_class(&self) -> &Option> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_SUPER_CLASS) as *const Option>) + } + } + + #[inline] + pub fn type_parameters(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_TYPE_PARAMETERS) + as *const Option>>) + } + } + + #[inline] + pub fn super_type_parameters(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_SUPER_TYPE_PARAMETERS) + as *const Option>>) + } + } + + #[inline] + pub fn implements(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_IMPLEMENTS) + as *const Option>>) + } + } + + #[inline] + pub fn modifiers(&self) -> &Modifiers<'a> { + unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_MODIFIERS) as *const Modifiers<'a>) } + } +} + +#[repr(transparent)] +#[derive(Debug)] +pub struct ClassWithoutTypeParameters<'a>(pub(crate) *const Class<'a>); + +impl<'a> ClassWithoutTypeParameters<'a> { + #[inline] + pub fn r#type(&self) -> &ClassType { + unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_TYPE) as *const ClassType) } + } + + #[inline] + pub fn span(&self) -> &Span { + unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_SPAN) as *const Span) } + } + + #[inline] + pub fn decorators(&self) -> &Vec<'a, Decorator<'a>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_DECORATORS) as *const Vec<'a, Decorator<'a>>) + } + } + + #[inline] + pub fn id(&self) -> &Option> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_ID) as *const Option>) + } + } + + #[inline] + pub fn super_class(&self) -> &Option> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_SUPER_CLASS) as *const Option>) + } + } + + #[inline] + pub fn body(&self) -> &Box<'a, ClassBody<'a>> { + unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_BODY) as *const Box<'a, ClassBody<'a>>) } + } + + #[inline] + pub fn super_type_parameters(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_SUPER_TYPE_PARAMETERS) + as *const Option>>) + } + } + + #[inline] + pub fn implements(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_IMPLEMENTS) + as *const Option>>) + } + } + + #[inline] + pub fn modifiers(&self) -> &Modifiers<'a> { + unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_MODIFIERS) as *const Modifiers<'a>) } + } +} + +#[repr(transparent)] +#[derive(Debug)] +pub struct ClassWithoutSuperTypeParameters<'a>(pub(crate) *const Class<'a>); + +impl<'a> ClassWithoutSuperTypeParameters<'a> { + #[inline] + pub fn r#type(&self) -> &ClassType { + unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_TYPE) as *const ClassType) } + } + + #[inline] + pub fn span(&self) -> &Span { + unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_SPAN) as *const Span) } + } + + #[inline] + pub fn decorators(&self) -> &Vec<'a, Decorator<'a>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_DECORATORS) as *const Vec<'a, Decorator<'a>>) + } + } + + #[inline] + pub fn id(&self) -> &Option> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_ID) as *const Option>) + } + } + + #[inline] + pub fn super_class(&self) -> &Option> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_SUPER_CLASS) as *const Option>) + } + } + + #[inline] + pub fn body(&self) -> &Box<'a, ClassBody<'a>> { + unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_BODY) as *const Box<'a, ClassBody<'a>>) } + } + + #[inline] + pub fn type_parameters(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_TYPE_PARAMETERS) + as *const Option>>) + } + } + + #[inline] + pub fn implements(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_IMPLEMENTS) + as *const Option>>) + } + } + + #[inline] + pub fn modifiers(&self) -> &Modifiers<'a> { + unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_MODIFIERS) as *const Modifiers<'a>) } + } +} + +#[repr(transparent)] +#[derive(Debug)] +pub struct ClassWithoutImplements<'a>(pub(crate) *const Class<'a>); + +impl<'a> ClassWithoutImplements<'a> { + #[inline] + pub fn r#type(&self) -> &ClassType { + unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_TYPE) as *const ClassType) } + } + + #[inline] + pub fn span(&self) -> &Span { + unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_SPAN) as *const Span) } + } + + #[inline] + pub fn decorators(&self) -> &Vec<'a, Decorator<'a>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_DECORATORS) as *const Vec<'a, Decorator<'a>>) + } + } + + #[inline] + pub fn id(&self) -> &Option> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_ID) as *const Option>) + } + } + + #[inline] + pub fn super_class(&self) -> &Option> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_SUPER_CLASS) as *const Option>) + } + } + + #[inline] + pub fn body(&self) -> &Box<'a, ClassBody<'a>> { + unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_BODY) as *const Box<'a, ClassBody<'a>>) } + } + + #[inline] + pub fn type_parameters(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_TYPE_PARAMETERS) + as *const Option>>) + } + } + + #[inline] + pub fn super_type_parameters(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_CLASS_SUPER_TYPE_PARAMETERS) + as *const Option>>) + } + } + + #[inline] + pub fn modifiers(&self) -> &Modifiers<'a> { + unsafe { &*((self.0 as *const u8).add(OFFSET_CLASS_MODIFIERS) as *const Modifiers<'a>) } + } +} + pub(crate) const OFFSET_CLASS_BODY_SPAN: usize = offset_of!(ClassBody, span); pub(crate) const OFFSET_CLASS_BODY_BODY: usize = offset_of!(ClassBody, body); @@ -6359,6 +6359,8 @@ impl<'a> ClassBodyWithoutBody<'a> { pub(crate) const OFFSET_METHOD_DEFINITION_TYPE: usize = offset_of!(MethodDefinition, r#type); pub(crate) const OFFSET_METHOD_DEFINITION_SPAN: usize = offset_of!(MethodDefinition, span); +pub(crate) const OFFSET_METHOD_DEFINITION_DECORATORS: usize = + offset_of!(MethodDefinition, decorators); pub(crate) const OFFSET_METHOD_DEFINITION_KEY: usize = offset_of!(MethodDefinition, key); pub(crate) const OFFSET_METHOD_DEFINITION_VALUE: usize = offset_of!(MethodDefinition, value); pub(crate) const OFFSET_METHOD_DEFINITION_KIND: usize = offset_of!(MethodDefinition, kind); @@ -6369,149 +6371,6 @@ pub(crate) const OFFSET_METHOD_DEFINITION_OVERRIDE: usize = pub(crate) const OFFSET_METHOD_DEFINITION_OPTIONAL: usize = offset_of!(MethodDefinition, optional); pub(crate) const OFFSET_METHOD_DEFINITION_ACCESSIBILITY: usize = offset_of!(MethodDefinition, accessibility); -pub(crate) const OFFSET_METHOD_DEFINITION_DECORATORS: usize = - offset_of!(MethodDefinition, decorators); - -#[repr(transparent)] -#[derive(Debug)] -pub struct MethodDefinitionWithoutKey<'a>(pub(crate) *const MethodDefinition<'a>); - -impl<'a> MethodDefinitionWithoutKey<'a> { - #[inline] - pub fn r#type(&self) -> &MethodDefinitionType { - unsafe { - &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_TYPE) - as *const MethodDefinitionType) - } - } - - #[inline] - pub fn span(&self) -> &Span { - unsafe { &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_SPAN) as *const Span) } - } - - #[inline] - pub fn value(&self) -> &Box<'a, Function<'a>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_VALUE) - as *const Box<'a, Function<'a>>) - } - } - - #[inline] - pub fn kind(&self) -> &MethodDefinitionKind { - unsafe { - &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_KIND) - as *const MethodDefinitionKind) - } - } - - #[inline] - pub fn computed(&self) -> &bool { - unsafe { &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_COMPUTED) as *const bool) } - } - - #[inline] - pub fn r#static(&self) -> &bool { - unsafe { &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_STATIC) as *const bool) } - } - - #[inline] - pub fn r#override(&self) -> &bool { - unsafe { &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_OVERRIDE) as *const bool) } - } - - #[inline] - pub fn optional(&self) -> &bool { - unsafe { &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_OPTIONAL) as *const bool) } - } - - #[inline] - pub fn accessibility(&self) -> &Option { - unsafe { - &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_ACCESSIBILITY) - as *const Option) - } - } - - #[inline] - pub fn decorators(&self) -> &Vec<'a, Decorator<'a>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_DECORATORS) - as *const Vec<'a, Decorator<'a>>) - } - } -} - -#[repr(transparent)] -#[derive(Debug)] -pub struct MethodDefinitionWithoutValue<'a>(pub(crate) *const MethodDefinition<'a>); - -impl<'a> MethodDefinitionWithoutValue<'a> { - #[inline] - pub fn r#type(&self) -> &MethodDefinitionType { - unsafe { - &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_TYPE) - as *const MethodDefinitionType) - } - } - - #[inline] - pub fn span(&self) -> &Span { - unsafe { &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_SPAN) as *const Span) } - } - - #[inline] - pub fn key(&self) -> &PropertyKey<'a> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_KEY) as *const PropertyKey<'a>) - } - } - - #[inline] - pub fn kind(&self) -> &MethodDefinitionKind { - unsafe { - &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_KIND) - as *const MethodDefinitionKind) - } - } - - #[inline] - pub fn computed(&self) -> &bool { - unsafe { &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_COMPUTED) as *const bool) } - } - - #[inline] - pub fn r#static(&self) -> &bool { - unsafe { &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_STATIC) as *const bool) } - } - - #[inline] - pub fn r#override(&self) -> &bool { - unsafe { &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_OVERRIDE) as *const bool) } - } - - #[inline] - pub fn optional(&self) -> &bool { - unsafe { &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_OPTIONAL) as *const bool) } - } - - #[inline] - pub fn accessibility(&self) -> &Option { - unsafe { - &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_ACCESSIBILITY) - as *const Option) - } - } - - #[inline] - pub fn decorators(&self) -> &Vec<'a, Decorator<'a>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_DECORATORS) - as *const Vec<'a, Decorator<'a>>) - } - } -} #[repr(transparent)] #[derive(Debug)] @@ -6583,6 +6442,147 @@ impl<'a> MethodDefinitionWithoutDecorators<'a> { } } +#[repr(transparent)] +#[derive(Debug)] +pub struct MethodDefinitionWithoutKey<'a>(pub(crate) *const MethodDefinition<'a>); + +impl<'a> MethodDefinitionWithoutKey<'a> { + #[inline] + pub fn r#type(&self) -> &MethodDefinitionType { + unsafe { + &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_TYPE) + as *const MethodDefinitionType) + } + } + + #[inline] + pub fn span(&self) -> &Span { + unsafe { &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_SPAN) as *const Span) } + } + + #[inline] + pub fn decorators(&self) -> &Vec<'a, Decorator<'a>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_DECORATORS) + as *const Vec<'a, Decorator<'a>>) + } + } + + #[inline] + pub fn value(&self) -> &Box<'a, Function<'a>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_VALUE) + as *const Box<'a, Function<'a>>) + } + } + + #[inline] + pub fn kind(&self) -> &MethodDefinitionKind { + unsafe { + &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_KIND) + as *const MethodDefinitionKind) + } + } + + #[inline] + pub fn computed(&self) -> &bool { + unsafe { &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_COMPUTED) as *const bool) } + } + + #[inline] + pub fn r#static(&self) -> &bool { + unsafe { &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_STATIC) as *const bool) } + } + + #[inline] + pub fn r#override(&self) -> &bool { + unsafe { &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_OVERRIDE) as *const bool) } + } + + #[inline] + pub fn optional(&self) -> &bool { + unsafe { &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_OPTIONAL) as *const bool) } + } + + #[inline] + pub fn accessibility(&self) -> &Option { + unsafe { + &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_ACCESSIBILITY) + as *const Option) + } + } +} + +#[repr(transparent)] +#[derive(Debug)] +pub struct MethodDefinitionWithoutValue<'a>(pub(crate) *const MethodDefinition<'a>); + +impl<'a> MethodDefinitionWithoutValue<'a> { + #[inline] + pub fn r#type(&self) -> &MethodDefinitionType { + unsafe { + &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_TYPE) + as *const MethodDefinitionType) + } + } + + #[inline] + pub fn span(&self) -> &Span { + unsafe { &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_SPAN) as *const Span) } + } + + #[inline] + pub fn decorators(&self) -> &Vec<'a, Decorator<'a>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_DECORATORS) + as *const Vec<'a, Decorator<'a>>) + } + } + + #[inline] + pub fn key(&self) -> &PropertyKey<'a> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_KEY) as *const PropertyKey<'a>) + } + } + + #[inline] + pub fn kind(&self) -> &MethodDefinitionKind { + unsafe { + &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_KIND) + as *const MethodDefinitionKind) + } + } + + #[inline] + pub fn computed(&self) -> &bool { + unsafe { &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_COMPUTED) as *const bool) } + } + + #[inline] + pub fn r#static(&self) -> &bool { + unsafe { &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_STATIC) as *const bool) } + } + + #[inline] + pub fn r#override(&self) -> &bool { + unsafe { &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_OVERRIDE) as *const bool) } + } + + #[inline] + pub fn optional(&self) -> &bool { + unsafe { &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_OPTIONAL) as *const bool) } + } + + #[inline] + pub fn accessibility(&self) -> &Option { + unsafe { + &*((self.0 as *const u8).add(OFFSET_METHOD_DEFINITION_ACCESSIBILITY) + as *const Option) + } + } +} + pub(crate) const OFFSET_PROPERTY_DEFINITION_TYPE: usize = offset_of!(PropertyDefinition, r#type); pub(crate) const OFFSET_PROPERTY_DEFINITION_SPAN: usize = offset_of!(PropertyDefinition, span); pub(crate) const OFFSET_PROPERTY_DEFINITION_KEY: usize = offset_of!(PropertyDefinition, key); diff --git a/crates/oxc_traverse/src/walk.rs b/crates/oxc_traverse/src/walk.rs index 4e0f623f4..94c5e20d0 100644 --- a/crates/oxc_traverse/src/walk.rs +++ b/crates/oxc_traverse/src/walk.rs @@ -2346,10 +2346,16 @@ pub(crate) unsafe fn walk_class<'a, Tr: Traverse<'a>>( ctx: &mut TraverseCtx<'a>, ) { traverser.enter_class(&mut *node, ctx); - ctx.push_stack(Ancestor::ClassId(ancestor::ClassWithoutId(node))); + ctx.push_stack(Ancestor::ClassDecorators(ancestor::ClassWithoutDecorators(node))); + for item in (*((node as *mut u8).add(ancestor::OFFSET_CLASS_DECORATORS) as *mut Vec)) + .iter_mut() + { + walk_decorator(traverser, item as *mut _, ctx); + } if let Some(field) = &mut *((node as *mut u8).add(ancestor::OFFSET_CLASS_ID) as *mut Option) { + ctx.retag_stack(AncestorType::ClassId); walk_binding_identifier(traverser, field as *mut _, ctx); } if let Some(field) = @@ -2385,12 +2391,6 @@ pub(crate) unsafe fn walk_class<'a, Tr: Traverse<'a>>( walk_ts_class_implements(traverser, item as *mut _, ctx); } } - ctx.retag_stack(AncestorType::ClassDecorators); - for item in (*((node as *mut u8).add(ancestor::OFFSET_CLASS_DECORATORS) as *mut Vec)) - .iter_mut() - { - walk_decorator(traverser, item as *mut _, ctx); - } ctx.pop_stack(); traverser.exit_class(&mut *node, ctx); } @@ -2444,7 +2444,16 @@ pub(crate) unsafe fn walk_method_definition<'a, Tr: Traverse<'a>>( ctx: &mut TraverseCtx<'a>, ) { traverser.enter_method_definition(&mut *node, ctx); - ctx.push_stack(Ancestor::MethodDefinitionKey(ancestor::MethodDefinitionWithoutKey(node))); + ctx.push_stack(Ancestor::MethodDefinitionDecorators( + ancestor::MethodDefinitionWithoutDecorators(node), + )); + for item in (*((node as *mut u8).add(ancestor::OFFSET_METHOD_DEFINITION_DECORATORS) + as *mut Vec)) + .iter_mut() + { + walk_decorator(traverser, item as *mut _, ctx); + } + ctx.retag_stack(AncestorType::MethodDefinitionKey); walk_property_key( traverser, (node as *mut u8).add(ancestor::OFFSET_METHOD_DEFINITION_KEY) as *mut PropertyKey, @@ -2457,13 +2466,6 @@ pub(crate) unsafe fn walk_method_definition<'a, Tr: Traverse<'a>>( as *mut Box)) as *mut _, ctx, ); - ctx.retag_stack(AncestorType::MethodDefinitionDecorators); - for item in (*((node as *mut u8).add(ancestor::OFFSET_METHOD_DEFINITION_DECORATORS) - as *mut Vec)) - .iter_mut() - { - walk_decorator(traverser, item as *mut _, ctx); - } ctx.pop_stack(); traverser.exit_method_definition(&mut *node, ctx); }