Skip to content

Commit

Permalink
feat: add referenced_symbols to StmtInfo (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyf0 authored Oct 18, 2023
1 parent f530305 commit 465e6e7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
11 changes: 8 additions & 3 deletions crates/rolldown/src/bundler/module/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ impl NormalModule {
self.stmt_infos.push(StmtInfo {
stmt_idx: self.ast.program().body.len(),
declared_symbols: vec![self.namespace_symbol.0.symbol],
..Default::default()
});
}
}
Expand Down Expand Up @@ -294,6 +295,7 @@ impl NormalModule {
self.stmt_infos.push(StmtInfo {
stmt_idx: self.ast.program().body.len(),
declared_symbols: vec![symbol],
..Default::default()
});
(self.id, symbol).into()
});
Expand All @@ -310,9 +312,11 @@ impl NormalModule {
.into();
let symbol = symbols.tables[self.id].create_symbol(name);
self.wrap_symbol = Some((self.id, symbol).into());
self
.stmt_infos
.push(StmtInfo { stmt_idx: self.ast.program().body.len(), declared_symbols: vec![symbol] });
self.stmt_infos.push(StmtInfo {
stmt_idx: self.ast.program().body.len(),
declared_symbols: vec![symbol],
..Default::default()
});
self.initialize_namespace();
}
}
Expand All @@ -335,6 +339,7 @@ impl NormalModule {
// The deconflict for runtime symbols would be handled in the deconflict on cross-chunk-imported
// symbols
declared_symbols: vec![local_symbol],
..Default::default()
});
}
}
25 changes: 19 additions & 6 deletions crates/rolldown/src/bundler/visitors/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,17 @@ impl<'a> Scanner<'a> {
}
}
}
fn get_symbol_id_from_identifier_reference(&self, id_ref: &IdentifierReference) -> SymbolId {
let ref_id = id_ref.reference_id.get().unwrap();

// If the reference is a global variable, `None` will be returned.
fn resolve_symbol_from_reference(&self, id_ref: &IdentifierReference) -> Option<SymbolId> {
let ref_id = id_ref.reference_id.get().expect("must have reference id");
let refer = self.symbol_table.get_reference(ref_id);
refer.symbol_id().unwrap()
refer.symbol_id()
}
fn scan_export_default_decl(&mut self, decl: &ExportDefaultDeclaration) {
let local = match &decl.declaration {
oxc::ast::ast::ExportDefaultDeclarationKind::Expression(exp) => match exp {
oxc::ast::ast::Expression::Identifier(id_ref) => {
Some(self.get_symbol_id_from_identifier_reference(id_ref))
}
oxc::ast::ast::Expression::Identifier(id_ref) => self.resolve_symbol_from_reference(id_ref),
_ => None,
},
oxc::ast::ast::ExportDefaultDeclarationKind::FunctionDeclaration(fn_decl) => {
Expand Down Expand Up @@ -262,6 +262,10 @@ impl<'a> Scanner<'a> {
_ => {}
}
}

pub fn add_referenced_symbol(&mut self, id: SymbolId) {
self.current_stmt_info.referenced_symbols.push((self.idx, id).into());
}
}

impl<'ast, 'p> VisitMut<'ast, 'p> for Scanner<'ast> {
Expand All @@ -282,6 +286,15 @@ impl<'ast, 'p> VisitMut<'ast, 'p> for Scanner<'ast> {
}

fn visit_identifier_reference(&mut self, ident: &'p mut IdentifierReference) {
let symbol_id = self.resolve_symbol_from_reference(ident);
match symbol_id {
Some(symbol_id)
if self.scope.root_scope_id() == self.symbol_table.get_scope_id(symbol_id) =>
{
self.add_referenced_symbol(symbol_id);
}
_ => {}
}
if ident.name == "module" || ident.name == "exports" {
if let Some(refs) = self.scope.root_unresolved_references().get(&ident.name) {
if refs.iter().any(|r| (*r).eq(&ident.reference_id.get().unwrap())) {
Expand Down
6 changes: 6 additions & 0 deletions crates/rolldown_common/src/stmt_info.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use oxc::semantic::SymbolId;

use crate::SymbolRef;

index_vec::define_index_type! {
pub struct StmtInfoId = u32;
}
Expand All @@ -9,4 +11,8 @@ pub struct StmtInfo {
pub stmt_idx: usize,
// currently, we only store top level symbols
pub declared_symbols: Vec<SymbolId>,
// We will add symbols of other modules to `referenced_symbols`, so we need `SymbolRef`
// here instead of `SymbolId`.
/// Top level symbols referenced by this statement.
pub referenced_symbols: Vec<SymbolRef>,
}

0 comments on commit 465e6e7

Please sign in to comment.