From 54f98973d1493e2bf555c3d7686f8107c949a1b5 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Tue, 6 Aug 2024 13:03:02 +0000 Subject: [PATCH] refactor(traverse): simpler code for entering/exiting unconditional scopes (#4685) Simplify generated code for visiting nodes which always have a scope. --- crates/oxc_traverse/scripts/lib/walk.mjs | 33 ++- crates/oxc_traverse/src/walk.rs | 288 ++++++++++------------- 2 files changed, 139 insertions(+), 182 deletions(-) diff --git a/crates/oxc_traverse/scripts/lib/walk.mjs b/crates/oxc_traverse/scripts/lib/walk.mjs index 6bb9af1a4bd80..657bfb76b496f 100644 --- a/crates/oxc_traverse/scripts/lib/walk.mjs +++ b/crates/oxc_traverse/scripts/lib/walk.mjs @@ -78,19 +78,28 @@ function generateWalkForStruct(type, types) { // but we don't take that into account. // Visitor should not do that though, so maybe it's OK. // In final version, we should not make `scope_id` fields `Cell`s to prevent this. - enterScopeCode = ` - let mut previous_scope_id = None; - if let Some(scope_id) = (*(${makeFieldCode(scopeIdField)})).get() { - previous_scope_id = Some(ctx.current_scope_id()); - ctx.set_current_scope_id(scope_id); - } - `; + if (scopeArgs.if) { + enterScopeCode = ` + let mut previous_scope_id = None; + if let Some(scope_id) = (*(${makeFieldCode(scopeIdField)})).get() { + previous_scope_id = Some(ctx.current_scope_id()); + ctx.set_current_scope_id(scope_id); + } + `; - exitScopeCode = ` - if let Some(previous_scope_id) = previous_scope_id { - ctx.set_current_scope_id(previous_scope_id); - } - `; + exitScopeCode = ` + if let Some(previous_scope_id) = previous_scope_id { + ctx.set_current_scope_id(previous_scope_id); + } + `; + } else { + enterScopeCode = ` + let previous_scope_id = ctx.current_scope_id(); + ctx.set_current_scope_id((*(${makeFieldCode(scopeIdField)})).get().unwrap()); + `; + + exitScopeCode = `ctx.set_current_scope_id(previous_scope_id);`; + } } const fieldsCodes = visitedFields.map((field, index) => { diff --git a/crates/oxc_traverse/src/walk.rs b/crates/oxc_traverse/src/walk.rs index e15eaca9f7673..482ca3710dbec 100644 --- a/crates/oxc_traverse/src/walk.rs +++ b/crates/oxc_traverse/src/walk.rs @@ -29,14 +29,12 @@ pub(crate) unsafe fn walk_program<'a, Tr: Traverse<'a>>( ctx: &mut TraverseCtx<'a>, ) { traverser.enter_program(&mut *node, ctx); - let mut previous_scope_id = None; - if let Some(scope_id) = (*((node as *mut u8).add(ancestor::OFFSET_PROGRAM_SCOPE_ID) - as *mut Cell>)) - .get() - { - previous_scope_id = Some(ctx.current_scope_id()); - ctx.set_current_scope_id(scope_id); - } + let previous_scope_id = ctx.current_scope_id(); + ctx.set_current_scope_id( + (*((node as *mut u8).add(ancestor::OFFSET_PROGRAM_SCOPE_ID) as *mut Cell>)) + .get() + .unwrap(), + ); ctx.push_stack(Ancestor::ProgramHashbang(ancestor::ProgramWithoutHashbang(node))); if let Some(field) = &mut *((node as *mut u8).add(ancestor::OFFSET_PROGRAM_HASHBANG) as *mut Option) @@ -57,9 +55,7 @@ pub(crate) unsafe fn walk_program<'a, Tr: Traverse<'a>>( ctx, ); ctx.pop_stack(); - if let Some(previous_scope_id) = previous_scope_id { - ctx.set_current_scope_id(previous_scope_id); - } + ctx.set_current_scope_id(previous_scope_id); traverser.exit_program(&mut *node, ctx); } @@ -1404,14 +1400,13 @@ pub(crate) unsafe fn walk_block_statement<'a, Tr: Traverse<'a>>( ctx: &mut TraverseCtx<'a>, ) { traverser.enter_block_statement(&mut *node, ctx); - let mut previous_scope_id = None; - if let Some(scope_id) = (*((node as *mut u8).add(ancestor::OFFSET_BLOCK_STATEMENT_SCOPE_ID) - as *mut Cell>)) - .get() - { - previous_scope_id = Some(ctx.current_scope_id()); - ctx.set_current_scope_id(scope_id); - } + let previous_scope_id = ctx.current_scope_id(); + ctx.set_current_scope_id( + (*((node as *mut u8).add(ancestor::OFFSET_BLOCK_STATEMENT_SCOPE_ID) + as *mut Cell>)) + .get() + .unwrap(), + ); ctx.push_stack(Ancestor::BlockStatementBody(ancestor::BlockStatementWithoutBody(node))); walk_statements( traverser, @@ -1419,9 +1414,7 @@ pub(crate) unsafe fn walk_block_statement<'a, Tr: Traverse<'a>>( ctx, ); ctx.pop_stack(); - if let Some(previous_scope_id) = previous_scope_id { - ctx.set_current_scope_id(previous_scope_id); - } + ctx.set_current_scope_id(previous_scope_id); traverser.exit_block_statement(&mut *node, ctx); } @@ -1920,14 +1913,13 @@ pub(crate) unsafe fn walk_switch_statement<'a, Tr: Traverse<'a>>( (node as *mut u8).add(ancestor::OFFSET_SWITCH_STATEMENT_DISCRIMINANT) as *mut Expression, ctx, ); - let mut previous_scope_id = None; - if let Some(scope_id) = (*((node as *mut u8).add(ancestor::OFFSET_SWITCH_STATEMENT_SCOPE_ID) - as *mut Cell>)) - .get() - { - previous_scope_id = Some(ctx.current_scope_id()); - ctx.set_current_scope_id(scope_id); - } + let previous_scope_id = ctx.current_scope_id(); + ctx.set_current_scope_id( + (*((node as *mut u8).add(ancestor::OFFSET_SWITCH_STATEMENT_SCOPE_ID) + as *mut Cell>)) + .get() + .unwrap(), + ); ctx.retag_stack(AncestorType::SwitchStatementCases); for item in (*((node as *mut u8).add(ancestor::OFFSET_SWITCH_STATEMENT_CASES) as *mut Vec)) @@ -1936,9 +1928,7 @@ pub(crate) unsafe fn walk_switch_statement<'a, Tr: Traverse<'a>>( walk_switch_case(traverser, item as *mut _, ctx); } ctx.pop_stack(); - if let Some(previous_scope_id) = previous_scope_id { - ctx.set_current_scope_id(previous_scope_id); - } + ctx.set_current_scope_id(previous_scope_id); traverser.exit_switch_statement(&mut *node, ctx); } @@ -2254,14 +2244,13 @@ pub(crate) unsafe fn walk_function<'a, Tr: Traverse<'a>>( ctx: &mut TraverseCtx<'a>, ) { traverser.enter_function(&mut *node, ctx); - let mut previous_scope_id = None; - if let Some(scope_id) = (*((node as *mut u8).add(ancestor::OFFSET_FUNCTION_SCOPE_ID) - as *mut Cell>)) - .get() - { - previous_scope_id = Some(ctx.current_scope_id()); - ctx.set_current_scope_id(scope_id); - } + let previous_scope_id = ctx.current_scope_id(); + ctx.set_current_scope_id( + (*((node as *mut u8).add(ancestor::OFFSET_FUNCTION_SCOPE_ID) + as *mut Cell>)) + .get() + .unwrap(), + ); ctx.push_stack(Ancestor::FunctionId(ancestor::FunctionWithoutId(node))); if let Some(field) = &mut *((node as *mut u8).add(ancestor::OFFSET_FUNCTION_ID) as *mut Option) @@ -2300,9 +2289,7 @@ pub(crate) unsafe fn walk_function<'a, Tr: Traverse<'a>>( walk_function_body(traverser, (&mut **field) as *mut _, ctx); } ctx.pop_stack(); - if let Some(previous_scope_id) = previous_scope_id { - ctx.set_current_scope_id(previous_scope_id); - } + ctx.set_current_scope_id(previous_scope_id); traverser.exit_function(&mut *node, ctx); } @@ -2383,15 +2370,13 @@ pub(crate) unsafe fn walk_arrow_function_expression<'a, Tr: Traverse<'a>>( ctx: &mut TraverseCtx<'a>, ) { traverser.enter_arrow_function_expression(&mut *node, ctx); - let mut previous_scope_id = None; - if let Some(scope_id) = (*((node as *mut u8) - .add(ancestor::OFFSET_ARROW_FUNCTION_EXPRESSION_SCOPE_ID) - as *mut Cell>)) - .get() - { - previous_scope_id = Some(ctx.current_scope_id()); - ctx.set_current_scope_id(scope_id); - } + let previous_scope_id = ctx.current_scope_id(); + ctx.set_current_scope_id( + (*((node as *mut u8).add(ancestor::OFFSET_ARROW_FUNCTION_EXPRESSION_SCOPE_ID) + as *mut Cell>)) + .get() + .unwrap(), + ); ctx.push_stack(Ancestor::ArrowFunctionExpressionTypeParameters( ancestor::ArrowFunctionExpressionWithoutTypeParameters(node), )); @@ -2423,9 +2408,7 @@ pub(crate) unsafe fn walk_arrow_function_expression<'a, Tr: Traverse<'a>>( ctx, ); ctx.pop_stack(); - if let Some(previous_scope_id) = previous_scope_id { - ctx.set_current_scope_id(previous_scope_id); - } + ctx.set_current_scope_id(previous_scope_id); traverser.exit_arrow_function_expression(&mut *node, ctx); } @@ -2465,14 +2448,12 @@ pub(crate) unsafe fn walk_class<'a, Tr: Traverse<'a>>( ctx.retag_stack(AncestorType::ClassId); walk_binding_identifier(traverser, field as *mut _, ctx); } - let mut previous_scope_id = None; - if let Some(scope_id) = (*((node as *mut u8).add(ancestor::OFFSET_CLASS_SCOPE_ID) - as *mut Cell>)) - .get() - { - previous_scope_id = Some(ctx.current_scope_id()); - ctx.set_current_scope_id(scope_id); - } + let previous_scope_id = ctx.current_scope_id(); + ctx.set_current_scope_id( + (*((node as *mut u8).add(ancestor::OFFSET_CLASS_SCOPE_ID) as *mut Cell>)) + .get() + .unwrap(), + ); if let Some(field) = &mut *((node as *mut u8).add(ancestor::OFFSET_CLASS_TYPE_PARAMETERS) as *mut Option>) { @@ -2507,9 +2488,7 @@ pub(crate) unsafe fn walk_class<'a, Tr: Traverse<'a>>( ctx, ); ctx.pop_stack(); - if let Some(previous_scope_id) = previous_scope_id { - ctx.set_current_scope_id(previous_scope_id); - } + ctx.set_current_scope_id(previous_scope_id); traverser.exit_class(&mut *node, ctx); } @@ -2641,14 +2620,13 @@ pub(crate) unsafe fn walk_static_block<'a, Tr: Traverse<'a>>( ctx: &mut TraverseCtx<'a>, ) { traverser.enter_static_block(&mut *node, ctx); - let mut previous_scope_id = None; - if let Some(scope_id) = (*((node as *mut u8).add(ancestor::OFFSET_STATIC_BLOCK_SCOPE_ID) - as *mut Cell>)) - .get() - { - previous_scope_id = Some(ctx.current_scope_id()); - ctx.set_current_scope_id(scope_id); - } + let previous_scope_id = ctx.current_scope_id(); + ctx.set_current_scope_id( + (*((node as *mut u8).add(ancestor::OFFSET_STATIC_BLOCK_SCOPE_ID) + as *mut Cell>)) + .get() + .unwrap(), + ); ctx.push_stack(Ancestor::StaticBlockBody(ancestor::StaticBlockWithoutBody(node))); walk_statements( traverser, @@ -2656,9 +2634,7 @@ pub(crate) unsafe fn walk_static_block<'a, Tr: Traverse<'a>>( ctx, ); ctx.pop_stack(); - if let Some(previous_scope_id) = previous_scope_id { - ctx.set_current_scope_id(previous_scope_id); - } + ctx.set_current_scope_id(previous_scope_id); traverser.exit_static_block(&mut *node, ctx); } @@ -3638,14 +3614,13 @@ pub(crate) unsafe fn walk_ts_enum_declaration<'a, Tr: Traverse<'a>>( (node as *mut u8).add(ancestor::OFFSET_TS_ENUM_DECLARATION_ID) as *mut BindingIdentifier, ctx, ); - let mut previous_scope_id = None; - if let Some(scope_id) = (*((node as *mut u8).add(ancestor::OFFSET_TS_ENUM_DECLARATION_SCOPE_ID) - as *mut Cell>)) - .get() - { - previous_scope_id = Some(ctx.current_scope_id()); - ctx.set_current_scope_id(scope_id); - } + let previous_scope_id = ctx.current_scope_id(); + ctx.set_current_scope_id( + (*((node as *mut u8).add(ancestor::OFFSET_TS_ENUM_DECLARATION_SCOPE_ID) + as *mut Cell>)) + .get() + .unwrap(), + ); ctx.retag_stack(AncestorType::TSEnumDeclarationMembers); for item in (*((node as *mut u8).add(ancestor::OFFSET_TS_ENUM_DECLARATION_MEMBERS) as *mut Vec)) @@ -3654,9 +3629,7 @@ pub(crate) unsafe fn walk_ts_enum_declaration<'a, Tr: Traverse<'a>>( walk_ts_enum_member(traverser, item as *mut _, ctx); } ctx.pop_stack(); - if let Some(previous_scope_id) = previous_scope_id { - ctx.set_current_scope_id(previous_scope_id); - } + ctx.set_current_scope_id(previous_scope_id); traverser.exit_ts_enum_declaration(&mut *node, ctx); } @@ -3929,14 +3902,13 @@ pub(crate) unsafe fn walk_ts_conditional_type<'a, Tr: Traverse<'a>>( ctx: &mut TraverseCtx<'a>, ) { traverser.enter_ts_conditional_type(&mut *node, ctx); - let mut previous_scope_id = None; - if let Some(scope_id) = (*((node as *mut u8).add(ancestor::OFFSET_TS_CONDITIONAL_TYPE_SCOPE_ID) - as *mut Cell>)) - .get() - { - previous_scope_id = Some(ctx.current_scope_id()); - ctx.set_current_scope_id(scope_id); - } + let previous_scope_id = ctx.current_scope_id(); + ctx.set_current_scope_id( + (*((node as *mut u8).add(ancestor::OFFSET_TS_CONDITIONAL_TYPE_SCOPE_ID) + as *mut Cell>)) + .get() + .unwrap(), + ); ctx.push_stack(Ancestor::TSConditionalTypeCheckType( ancestor::TSConditionalTypeWithoutCheckType(node), )); @@ -3964,9 +3936,7 @@ pub(crate) unsafe fn walk_ts_conditional_type<'a, Tr: Traverse<'a>>( ctx, ); ctx.pop_stack(); - if let Some(previous_scope_id) = previous_scope_id { - ctx.set_current_scope_id(previous_scope_id); - } + ctx.set_current_scope_id(previous_scope_id); traverser.exit_ts_conditional_type(&mut *node, ctx); } @@ -4488,15 +4458,13 @@ pub(crate) unsafe fn walk_ts_type_alias_declaration<'a, Tr: Traverse<'a>>( as *mut BindingIdentifier, ctx, ); - let mut previous_scope_id = None; - if let Some(scope_id) = (*((node as *mut u8) - .add(ancestor::OFFSET_TS_TYPE_ALIAS_DECLARATION_SCOPE_ID) - as *mut Cell>)) - .get() - { - previous_scope_id = Some(ctx.current_scope_id()); - ctx.set_current_scope_id(scope_id); - } + let previous_scope_id = ctx.current_scope_id(); + ctx.set_current_scope_id( + (*((node as *mut u8).add(ancestor::OFFSET_TS_TYPE_ALIAS_DECLARATION_SCOPE_ID) + as *mut Cell>)) + .get() + .unwrap(), + ); if let Some(field) = &mut *((node as *mut u8) .add(ancestor::OFFSET_TS_TYPE_ALIAS_DECLARATION_TYPE_PARAMETERS) as *mut Option>) @@ -4512,9 +4480,7 @@ pub(crate) unsafe fn walk_ts_type_alias_declaration<'a, Tr: Traverse<'a>>( ctx, ); ctx.pop_stack(); - if let Some(previous_scope_id) = previous_scope_id { - ctx.set_current_scope_id(previous_scope_id); - } + ctx.set_current_scope_id(previous_scope_id); traverser.exit_ts_type_alias_declaration(&mut *node, ctx); } @@ -4558,15 +4524,13 @@ pub(crate) unsafe fn walk_ts_interface_declaration<'a, Tr: Traverse<'a>>( as *mut BindingIdentifier, ctx, ); - let mut previous_scope_id = None; - if let Some(scope_id) = (*((node as *mut u8) - .add(ancestor::OFFSET_TS_INTERFACE_DECLARATION_SCOPE_ID) - as *mut Cell>)) - .get() - { - previous_scope_id = Some(ctx.current_scope_id()); - ctx.set_current_scope_id(scope_id); - } + let previous_scope_id = ctx.current_scope_id(); + ctx.set_current_scope_id( + (*((node as *mut u8).add(ancestor::OFFSET_TS_INTERFACE_DECLARATION_SCOPE_ID) + as *mut Cell>)) + .get() + .unwrap(), + ); if let Some(field) = &mut *((node as *mut u8) .add(ancestor::OFFSET_TS_INTERFACE_DECLARATION_EXTENDS) as *mut Option>) @@ -4591,9 +4555,7 @@ pub(crate) unsafe fn walk_ts_interface_declaration<'a, Tr: Traverse<'a>>( ctx, ); ctx.pop_stack(); - if let Some(previous_scope_id) = previous_scope_id { - ctx.set_current_scope_id(previous_scope_id); - } + ctx.set_current_scope_id(previous_scope_id); traverser.exit_ts_interface_declaration(&mut *node, ctx); } @@ -4735,14 +4697,13 @@ pub(crate) unsafe fn walk_ts_method_signature<'a, Tr: Traverse<'a>>( ctx: &mut TraverseCtx<'a>, ) { traverser.enter_ts_method_signature(&mut *node, ctx); - let mut previous_scope_id = None; - if let Some(scope_id) = (*((node as *mut u8).add(ancestor::OFFSET_TS_METHOD_SIGNATURE_SCOPE_ID) - as *mut Cell>)) - .get() - { - previous_scope_id = Some(ctx.current_scope_id()); - ctx.set_current_scope_id(scope_id); - } + let previous_scope_id = ctx.current_scope_id(); + ctx.set_current_scope_id( + (*((node as *mut u8).add(ancestor::OFFSET_TS_METHOD_SIGNATURE_SCOPE_ID) + as *mut Cell>)) + .get() + .unwrap(), + ); ctx.push_stack(Ancestor::TSMethodSignatureKey(ancestor::TSMethodSignatureWithoutKey(node))); walk_property_key( traverser, @@ -4778,9 +4739,7 @@ pub(crate) unsafe fn walk_ts_method_signature<'a, Tr: Traverse<'a>>( walk_ts_type_parameter_declaration(traverser, (&mut **field) as *mut _, ctx); } ctx.pop_stack(); - if let Some(previous_scope_id) = previous_scope_id { - ctx.set_current_scope_id(previous_scope_id); - } + ctx.set_current_scope_id(previous_scope_id); traverser.exit_ts_method_signature(&mut *node, ctx); } @@ -4790,15 +4749,13 @@ pub(crate) unsafe fn walk_ts_construct_signature_declaration<'a, Tr: Traverse<'a ctx: &mut TraverseCtx<'a>, ) { traverser.enter_ts_construct_signature_declaration(&mut *node, ctx); - let mut previous_scope_id = None; - if let Some(scope_id) = (*((node as *mut u8) - .add(ancestor::OFFSET_TS_CONSTRUCT_SIGNATURE_DECLARATION_SCOPE_ID) - as *mut Cell>)) - .get() - { - previous_scope_id = Some(ctx.current_scope_id()); - ctx.set_current_scope_id(scope_id); - } + let previous_scope_id = ctx.current_scope_id(); + ctx.set_current_scope_id( + (*((node as *mut u8).add(ancestor::OFFSET_TS_CONSTRUCT_SIGNATURE_DECLARATION_SCOPE_ID) + as *mut Cell>)) + .get() + .unwrap(), + ); ctx.push_stack(Ancestor::TSConstructSignatureDeclarationParams( ancestor::TSConstructSignatureDeclarationWithoutParams(node), )); @@ -4823,9 +4780,7 @@ pub(crate) unsafe fn walk_ts_construct_signature_declaration<'a, Tr: Traverse<'a walk_ts_type_parameter_declaration(traverser, (&mut **field) as *mut _, ctx); } ctx.pop_stack(); - if let Some(previous_scope_id) = previous_scope_id { - ctx.set_current_scope_id(previous_scope_id); - } + ctx.set_current_scope_id(previous_scope_id); traverser.exit_ts_construct_signature_declaration(&mut *node, ctx); } @@ -4927,15 +4882,13 @@ pub(crate) unsafe fn walk_ts_module_declaration<'a, Tr: Traverse<'a>>( as *mut TSModuleDeclarationName, ctx, ); - let mut previous_scope_id = None; - if let Some(scope_id) = (*((node as *mut u8) - .add(ancestor::OFFSET_TS_MODULE_DECLARATION_SCOPE_ID) - as *mut Cell>)) - .get() - { - previous_scope_id = Some(ctx.current_scope_id()); - ctx.set_current_scope_id(scope_id); - } + let previous_scope_id = ctx.current_scope_id(); + ctx.set_current_scope_id( + (*((node as *mut u8).add(ancestor::OFFSET_TS_MODULE_DECLARATION_SCOPE_ID) + as *mut Cell>)) + .get() + .unwrap(), + ); if let Some(field) = &mut *((node as *mut u8).add(ancestor::OFFSET_TS_MODULE_DECLARATION_BODY) as *mut Option) { @@ -4943,9 +4896,7 @@ pub(crate) unsafe fn walk_ts_module_declaration<'a, Tr: Traverse<'a>>( walk_ts_module_declaration_body(traverser, field as *mut _, ctx); } ctx.pop_stack(); - if let Some(previous_scope_id) = previous_scope_id { - ctx.set_current_scope_id(previous_scope_id); - } + ctx.set_current_scope_id(previous_scope_id); traverser.exit_ts_module_declaration(&mut *node, ctx); } @@ -5263,14 +5214,13 @@ pub(crate) unsafe fn walk_ts_mapped_type<'a, Tr: Traverse<'a>>( ctx: &mut TraverseCtx<'a>, ) { traverser.enter_ts_mapped_type(&mut *node, ctx); - let mut previous_scope_id = None; - if let Some(scope_id) = (*((node as *mut u8).add(ancestor::OFFSET_TS_MAPPED_TYPE_SCOPE_ID) - as *mut Cell>)) - .get() - { - previous_scope_id = Some(ctx.current_scope_id()); - ctx.set_current_scope_id(scope_id); - } + let previous_scope_id = ctx.current_scope_id(); + ctx.set_current_scope_id( + (*((node as *mut u8).add(ancestor::OFFSET_TS_MAPPED_TYPE_SCOPE_ID) + as *mut Cell>)) + .get() + .unwrap(), + ); ctx.push_stack(Ancestor::TSMappedTypeTypeParameter( ancestor::TSMappedTypeWithoutTypeParameter(node), )); @@ -5294,9 +5244,7 @@ pub(crate) unsafe fn walk_ts_mapped_type<'a, Tr: Traverse<'a>>( walk_ts_type(traverser, field as *mut _, ctx); } ctx.pop_stack(); - if let Some(previous_scope_id) = previous_scope_id { - ctx.set_current_scope_id(previous_scope_id); - } + ctx.set_current_scope_id(previous_scope_id); traverser.exit_ts_mapped_type(&mut *node, ctx); }