Skip to content

Commit

Permalink
u
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 19, 2024
1 parent b12bcfd commit 556c1da
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 34 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_mangler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl Mangler {
// rename the variables
for (symbol_to_rename, new_name) in symbols_to_rename_with_new_names {
for &symbol_id in &symbol_to_rename.symbol_ids {
symbol_table.set_name(symbol_id, new_name.clone());
symbol_table.set_name(symbol_id, new_name.as_str());
}
}
}
Expand Down
14 changes: 5 additions & 9 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,14 +399,10 @@ impl<'a> SemanticBuilder<'a> {
return symbol_id;
}

let symbol_id =
self.symbols.create_symbol(span, name, includes, scope_id, self.current_node_id);

let name = CompactStr::new(name);
let symbol_id = self.symbols.create_symbol(
span,
name.clone(),
includes,
scope_id,
self.current_node_id,
);
self.scope.add_binding(scope_id, name, symbol_id);
symbol_id
}
Expand Down Expand Up @@ -466,14 +462,14 @@ impl<'a> SemanticBuilder<'a> {
scope_id: ScopeId,
includes: SymbolFlags,
) -> SymbolId {
let name = CompactStr::new(name);
let symbol_id = self.symbols.create_symbol(
span,
name.clone(),
name,
includes,
self.current_scope_id,
self.current_node_id,
);
let name = CompactStr::new(name);
self.scope.get_bindings_mut(scope_id).insert(name, symbol_id);
symbol_id
}
Expand Down
34 changes: 19 additions & 15 deletions crates/oxc_semantic/src/symbol.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::mem;

use oxc_allocator::{Allocator, Vec as ArenaVec};
use oxc_allocator::{Allocator, FromIn, Vec as ArenaVec};
use oxc_ast::ast::{Expression, IdentifierReference};
use oxc_index::{Idx, IndexVec};
use oxc_span::{CompactStr, Span};
use oxc_span::{Atom, Span};
use oxc_syntax::{
node::NodeId,
reference::ReferenceId,
Expand All @@ -22,7 +22,6 @@ use crate::reference::Reference;
/// That ID indexes into `redeclarations` where the actual `Vec<Span>` is stored.
pub struct SymbolTable {
pub(crate) spans: IndexVec<SymbolId, Span>,
pub(crate) names: IndexVec<SymbolId, CompactStr>,
pub(crate) flags: IndexVec<SymbolId, SymbolFlags>,
pub(crate) scope_ids: IndexVec<SymbolId, ScopeId>,
/// Pointer to the AST Node where this symbol is declared
Expand All @@ -39,13 +38,13 @@ impl Default for SymbolTable {
let allocator = Allocator::default();
Self {
spans: IndexVec::new(),
names: IndexVec::new(),
flags: IndexVec::new(),
scope_ids: IndexVec::new(),
declarations: IndexVec::new(),
redeclarations: IndexVec::new(),
references: IndexVec::new(),
inner: SymbolTableCell::new(allocator, |allocator| SymbolTableInner {
names: ArenaVec::new_in(allocator),
resolved_references: ArenaVec::new_in(allocator),
redeclaration_spans: ArenaVec::new_in(allocator),
}),
Expand All @@ -61,9 +60,10 @@ self_cell::self_cell!(
}
);

struct SymbolTableInner<'a> {
resolved_references: ArenaVec<'a, ArenaVec<'a, ReferenceId>>,
redeclaration_spans: ArenaVec<'a, ArenaVec<'a, Span>>,
struct SymbolTableInner<'cell> {
names: ArenaVec<'cell, Atom<'cell>>,
resolved_references: ArenaVec<'cell, ArenaVec<'cell, ReferenceId>>,
redeclaration_spans: ArenaVec<'cell, ArenaVec<'cell, Span>>,
}

impl SymbolTable {
Expand All @@ -79,8 +79,8 @@ impl SymbolTable {
self.spans.is_empty()
}

pub fn names(&self) -> impl Iterator<Item = &CompactStr> + '_ {
self.names.iter()
pub fn names(&self) -> impl Iterator<Item = &str> + '_ {
self.inner.borrow_dependent().names.iter().map(|name| name.as_str())
}

pub fn resolved_references(&self) -> impl Iterator<Item = &ArenaVec<'_, ReferenceId>> + '_ {
Expand Down Expand Up @@ -124,15 +124,19 @@ impl SymbolTable {
/// Get the identifier name a symbol is bound to.
#[inline]
pub fn get_name(&self, symbol_id: SymbolId) -> &str {
&self.names[symbol_id]
&self.inner.borrow_dependent().names[symbol_id.index()]
}

/// Rename a symbol.
///
/// Returns the old name.
#[inline]
pub fn set_name(&mut self, symbol_id: SymbolId, name: CompactStr) -> CompactStr {
mem::replace(&mut self.names[symbol_id], name)
pub fn set_name(&mut self, symbol_id: SymbolId, name: &str) -> &str {
self.inner
.with_dependent_mut(|allocator, inner| {
mem::replace(&mut inner.names[symbol_id.index()], Atom::from_in(name, allocator))
})
.as_str()
}

/// Get the [`SymbolFlags`] for a symbol, which describe how the symbol is declared.
Expand Down Expand Up @@ -192,17 +196,17 @@ impl SymbolTable {
pub fn create_symbol(
&mut self,
span: Span,
name: CompactStr,
name: &str,
flags: SymbolFlags,
scope_id: ScopeId,
node_id: NodeId,
) -> SymbolId {
self.spans.push(span);
self.names.push(name);
self.flags.push(flags);
self.scope_ids.push(scope_id);
self.declarations.push(node_id);
self.inner.with_dependent_mut(|allocator, inner| {
inner.names.push(Atom::from_in(name, allocator));
inner.resolved_references.push(ArenaVec::new_in(allocator));
});
self.redeclarations.push(None)
Expand Down Expand Up @@ -303,11 +307,11 @@ impl SymbolTable {

pub fn reserve(&mut self, additional_symbols: usize, additional_references: usize) {
self.spans.reserve(additional_symbols);
self.names.reserve(additional_symbols);
self.flags.reserve(additional_symbols);
self.scope_ids.reserve(additional_symbols);
self.declarations.reserve(additional_symbols);
self.inner.with_dependent_mut(|_allocator, inner| {
inner.names.reserve(additional_symbols);
inner.resolved_references.reserve(additional_symbols);
});
self.references.reserve(additional_references);
Expand Down
22 changes: 13 additions & 9 deletions crates/oxc_traverse/src/context/scoping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl TraverseScoping {
flags: SymbolFlags,
) -> SymbolId {
let symbol_id =
self.symbols.create_symbol(SPAN, name.clone(), flags, scope_id, NodeId::DUMMY);
self.symbols.create_symbol(SPAN, name.as_str(), flags, scope_id, NodeId::DUMMY);
self.scopes.add_binding(scope_id, name, symbol_id);

symbol_id
Expand Down Expand Up @@ -374,7 +374,7 @@ impl TraverseScoping {
/// Panics in debug mode if either of the above are not satisfied.
pub fn rename_symbol(&mut self, symbol_id: SymbolId, scope_id: ScopeId, new_name: CompactStr) {
// Rename symbol
let old_name = self.symbols.set_name(symbol_id, new_name.clone());
let old_name = self.symbols.set_name(symbol_id, new_name.as_str());
// Rename binding
self.scopes.rename_binding(scope_id, symbol_id, &old_name, new_name);
}
Expand Down Expand Up @@ -422,14 +422,18 @@ impl TraverseScoping {
self.scopes
.root_unresolved_references()
.keys()
.map(CompactStr::as_str)
.chain(self.symbols.names())
.filter_map(|name| {
if name.as_bytes().first() == Some(&b'_') {
Some(name.clone())
} else {
None
}
})
.filter_map(
|name| {
if name.as_bytes().first() == Some(&b'_') {
Some(name)
} else {
None
}
},
)
.map(CompactStr::from)
.collect()
}
}
Expand Down

0 comments on commit 556c1da

Please sign in to comment.