Skip to content

Commit

Permalink
refactor(traverse): simplify pointer manipulation and improve readabi…
Browse files Browse the repository at this point in the history
…lity
  • Loading branch information
Dunqing authored and overlookmotel committed Feb 18, 2025
1 parent d3142b2 commit c7945c2
Show file tree
Hide file tree
Showing 4 changed files with 1,424 additions and 5,344 deletions.
20 changes: 3 additions & 17 deletions crates/oxc_traverse/scripts/lib/ancestor.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,7 @@ export default function generateAncestorsCode(types) {
for (const type of Object.values(types)) {
if (type.kind === 'enum') continue;

const typeSnakeName = camelToSnake(type.name),
typeScreamingName = typeSnakeName.toUpperCase();
let offsetCode = '';
for (const field of type.fields) {
const offsetVarName = `OFFSET_${typeScreamingName}_${field.name.toUpperCase()}`;
field.offsetVarName = offsetVarName;
offsetCode += `pub(crate) const ${offsetVarName}: usize = ` +
`offset_of!(${type.name}, ${field.rawName});\n`;
}
const typeSnakeName = camelToSnake(type.name);

const variantNames = [];
let thisAncestorTypes = '';
Expand All @@ -39,10 +31,7 @@ export default function generateAncestorsCode(types) {
#[inline]
pub fn ${otherField.rawName}(self) -> &'t ${otherField.rawTypeName} {
unsafe {
&*(
(self.0 as *const u8).add(${otherField.offsetVarName})
as *const ${otherField.rawTypeName}
)
&*from_ref(&(*self.0).${otherField.rawName})
}
}
`;
Expand Down Expand Up @@ -89,7 +78,6 @@ export default function generateAncestorsCode(types) {

if (variantNames.length > 0) {
ancestorTypes += `
${offsetCode}
${thisAncestorTypes}
`;

Expand All @@ -113,13 +101,11 @@ export default function generateAncestorsCode(types) {

return `
#![expect(
clippy::ptr_as_ptr,
clippy::undocumented_unsafe_blocks,
clippy::cast_ptr_alignment,
clippy::needless_lifetimes
)]
use std::{cell::Cell, marker::PhantomData, mem::offset_of};
use std::{cell::Cell, marker::PhantomData, ptr::from_ref};
use oxc_allocator::{Address, Box, GetAddress, Vec};
use oxc_ast::ast::*;
Expand Down
20 changes: 9 additions & 11 deletions crates/oxc_traverse/scripts/lib/walk.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ export default function generateWalkFunctionsCode(types) {
clippy::semicolon_if_nothing_returned,
clippy::ptr_as_ptr,
clippy::ref_as_ptr,
clippy::cast_ptr_alignment
)]
use std::{cell::Cell, marker::PhantomData};
use std::{marker::PhantomData, ptr::from_mut};
use oxc_allocator::Vec;
use oxc_ast::ast::*;
use oxc_syntax::scope::ScopeId;
use crate::{ancestor::{self, AncestorType}, Ancestor, Traverse, TraverseCtx};
Expand Down Expand Up @@ -122,7 +120,7 @@ function generateWalkForStruct(type, types) {

enterScopeCode = `
let previous_scope_id = ctx.current_scope_id();
let current_scope_id = (*(${makeFieldCode(scopeIdField)})).get().unwrap();
let current_scope_id = ${makeFieldCode(scopeIdField)}.get().unwrap();
ctx.set_current_scope_id(current_scope_id);
`;

Expand Down Expand Up @@ -205,7 +203,7 @@ function generateWalkForStruct(type, types) {
return `
${scopeCode}
${tagCode}
if let Some(field) = &mut *(${fieldCode}) {
if let Some(field) = &mut ${fieldCode} {
${retagCode}
${walkCode}
}
Expand All @@ -216,18 +214,18 @@ function generateWalkForStruct(type, types) {
let walkVecCode;
if (field.wrappers.length === 1 && field.innerTypeName === 'Statement') {
// Special case for `Vec<Statement>`
walkVecCode = `walk_statements(traverser, ${fieldCode}, ctx);`;
walkVecCode = `walk_statements(traverser, from_mut(&mut ${fieldCode}), ctx);`;
} else {
let walkCode = `${fieldWalkName}(traverser, item as *mut _, ctx);`,
iteratorCode = '';
if (field.wrappers.length === 2 && field.wrappers[1] === 'Option') {
iteratorCode = `(*(${fieldCode})).iter_mut().flatten()`;
iteratorCode = `${fieldCode}.iter_mut().flatten()`;
} else {
assert(
field.wrappers.length === 1,
`Cannot handle struct field with type ${field.type}`,
);
iteratorCode = `&mut *(${fieldCode})`;
iteratorCode = `&mut ${fieldCode}`;
}
walkVecCode = `
for item in ${iteratorCode} {
Expand All @@ -247,7 +245,7 @@ function generateWalkForStruct(type, types) {
return `
${scopeCode}
${tagCode || retagCode}
${fieldWalkName}(traverser, (&mut **(${fieldCode})) as *mut _, ctx);
${fieldWalkName}(traverser, from_mut(&mut *(${fieldCode})), ctx);
`;
}

Expand All @@ -256,7 +254,7 @@ function generateWalkForStruct(type, types) {
return `
${scopeCode}
${tagCode || retagCode}
${fieldWalkName}(traverser, ${fieldCode}, ctx);
${fieldWalkName}(traverser, from_mut(&mut ${fieldCode}), ctx);
`;
});

Expand All @@ -278,7 +276,7 @@ function generateWalkForStruct(type, types) {
}

function makeFieldCode(field) {
return `(node as *mut u8).add(ancestor::${field.offsetVarName}) as *mut ${field.typeName}`;
return `(*node).${field.name}`;
}

/**
Expand Down
Loading

0 comments on commit c7945c2

Please sign in to comment.