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 committed Feb 16, 2025
1 parent 19fdf89 commit 4346b84
Show file tree
Hide file tree
Showing 4 changed files with 1,423 additions and 5,329 deletions.
19 changes: 4 additions & 15 deletions crates/oxc_traverse/scripts/lib/ancestor.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,8 @@ 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 +32,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 +79,6 @@ export default function generateAncestorsCode(types) {

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

Expand Down Expand Up @@ -119,7 +108,7 @@ export default function generateAncestorsCode(types) {
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
19 changes: 9 additions & 10 deletions crates/oxc_traverse/scripts/lib/walk.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ export default function generateWalkFunctionsCode(types) {
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 +121,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 +204,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 +215,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 +246,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 +255,7 @@ function generateWalkForStruct(type, types) {
return `
${scopeCode}
${tagCode || retagCode}
${fieldWalkName}(traverser, ${fieldCode}, ctx);
${fieldWalkName}(traverser, from_mut(&mut ${fieldCode}), ctx);
`;
});

Expand All @@ -278,7 +277,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 4346b84

Please sign in to comment.