Skip to content

Commit

Permalink
feat: remove VirtualStmtInfo (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyf0 committed Oct 26, 2023
1 parent 9c90067 commit 344be1a
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 33 deletions.
2 changes: 1 addition & 1 deletion crates/rolldown/src/bundler/chunk/de_conflict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl Chunk {
.flat_map(|part| part.declared_symbols.iter().copied())
.chain(
graph.linker_modules[module.id]
.virtual_stmt_infos
.facade_stmt_infos
.iter()
.flat_map(|part| part.declared_symbols.iter().copied()),
)
Expand Down
4 changes: 2 additions & 2 deletions crates/rolldown/src/bundler/graph/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use index_vec::IndexVec;
use oxc::span::Atom;
use rolldown_common::{
ExportsKind, ImportKind, LocalOrReExport, ModuleId, ResolvedExport, ResolvedExportRuntime,
SymbolRef, VirtualStmtInfo,
StmtInfo, SymbolRef,
};
use rustc_hash::FxHashMap;

Expand All @@ -21,7 +21,7 @@ use crate::bundler::{
pub struct LinkerModule {
// The symbol for wrapped module
pub wrap_symbol: Option<SymbolRef>,
pub virtual_stmt_infos: Vec<VirtualStmtInfo>,
pub facade_stmt_infos: Vec<StmtInfo>,
pub resolved_exports: FxHashMap<Atom, ResolvedExport>,
pub resolved_star_exports: Vec<ModuleId>,
pub is_symbol_for_namespace_referenced: bool,
Expand Down
4 changes: 2 additions & 2 deletions crates/rolldown/src/bundler/module/module_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use oxc::{
};
use rolldown_common::{
ExportsKind, ImportRecord, ImportRecordId, LocalOrReExport, ModuleId, ModuleType, NamedImport,
ResourceId, StmtInfo, StmtInfoId, SymbolRef,
ResourceId, StmtInfo, SymbolRef,
};
use rolldown_oxc::OxcProgram;
use rustc_hash::FxHashMap;
Expand All @@ -21,7 +21,7 @@ pub struct NormalModuleBuilder {
pub ast: Option<OxcProgram>,
pub named_imports: Option<FxHashMap<SymbolId, NamedImport>>,
pub named_exports: Option<FxHashMap<Atom, LocalOrReExport>>,
pub stmt_infos: Option<IndexVec<StmtInfoId, StmtInfo>>,
pub stmt_infos: Option<Vec<StmtInfo>>,
pub import_records: Option<IndexVec<ImportRecordId, ImportRecord>>,
pub imports: Option<FxHashMap<Span, ImportRecordId>>,
pub star_exports: Option<Vec<ImportRecordId>>,
Expand Down
20 changes: 9 additions & 11 deletions crates/rolldown/src/bundler/module/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use oxc::{
};
use rolldown_common::{
ExportsKind, ImportRecord, ImportRecordId, LocalOrReExport, ModuleId, ModuleType, NamedImport,
ResourceId, StmtInfo, StmtInfoId, SymbolRef, VirtualStmtInfo,
ResourceId, StmtInfo, SymbolRef,
};
use rolldown_oxc::OxcProgram;
use rustc_hash::{FxHashMap, FxHashSet};
Expand All @@ -33,7 +33,7 @@ pub struct NormalModule {
pub ast: OxcProgram,
pub named_imports: FxHashMap<SymbolId, NamedImport>,
pub named_exports: FxHashMap<Atom, LocalOrReExport>,
pub stmt_infos: IndexVec<StmtInfoId, StmtInfo>,
pub stmt_infos: Vec<StmtInfo>,
pub import_records: IndexVec<ImportRecordId, ImportRecord>,
pub imports: FxHashMap<Span, ImportRecordId>,
// [[StarExportEntries]] in https://tc39.es/ecma262/#sec-source-text-module-records
Expand Down Expand Up @@ -102,10 +102,9 @@ impl NormalModule {
pub fn initialize_namespace(&self, self_linker_module: &mut LinkerModule) {
if !self_linker_module.is_symbol_for_namespace_referenced {
self_linker_module.is_symbol_for_namespace_referenced = true;
self_linker_module.virtual_stmt_infos.push(VirtualStmtInfo {
declared_symbols: vec![self.namespace_symbol],
..Default::default()
});
self_linker_module
.facade_stmt_infos
.push(StmtInfo { declared_symbols: vec![self.namespace_symbol], ..Default::default() });
}
}

Expand Down Expand Up @@ -335,10 +334,9 @@ impl NormalModule {
.into();
let symbol = symbols.create_symbol(self.id, name).symbol;
self_linker_module.wrap_symbol = Some((self.id, symbol).into());
self_linker_module.virtual_stmt_infos.push(VirtualStmtInfo {
declared_symbols: vec![(self.id, symbol).into()],
..Default::default()
});
self_linker_module
.facade_stmt_infos
.push(StmtInfo { declared_symbols: vec![(self.id, symbol).into()], ..Default::default() });
self.initialize_namespace(self_linker_module);
}
}
Expand All @@ -362,7 +360,7 @@ impl NormalModule {
symbols: &mut Symbols,
) -> SymbolRef {
let local_symbol_ref = symbols.create_symbol(self.id, name);
self_linker_module.virtual_stmt_infos.push(VirtualStmtInfo {
self_linker_module.facade_stmt_infos.push(StmtInfo {
// FIXME: should store the symbol in `used_symbols` instead of `declared_symbols`.
// The deconflict for runtime symbols would be handled in the deconflict on cross-chunk-imported
// symbols
Expand Down
8 changes: 4 additions & 4 deletions crates/rolldown/src/bundler/visitors/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use oxc::{
};
use rolldown_common::{
ExportsKind, ImportKind, ImportRecord, ImportRecordId, LocalExport, LocalOrReExport, ModuleId,
ModuleType, NamedImport, ReExport, StmtInfo, StmtInfoId,
ModuleType, NamedImport, ReExport, StmtInfo,
};
use rolldown_oxc::BindingIdentifierExt;
use rustc_hash::FxHashMap;
Expand All @@ -21,7 +21,7 @@ use rustc_hash::FxHashMap;
pub struct ScanResult {
pub named_imports: FxHashMap<SymbolId, NamedImport>,
pub named_exports: FxHashMap<Atom, LocalOrReExport>,
pub stmt_infos: IndexVec<StmtInfoId, StmtInfo>,
pub stmt_infos: Vec<StmtInfo>,
pub import_records: IndexVec<ImportRecordId, ImportRecord>,
pub star_exports: Vec<ImportRecordId>,
pub export_default_symbol_id: Option<SymbolId>,
Expand Down Expand Up @@ -297,9 +297,9 @@ impl<'a> Scanner<'a> {

impl<'ast, 'p> VisitMut<'ast, 'p> for Scanner<'ast> {
fn visit_program(&mut self, program: &'p mut oxc::ast::ast::Program<'ast>) {
self.result.stmt_infos = IndexVec::with_capacity(program.body.len());
self.result.stmt_infos = Vec::with_capacity(program.body.len());
for (idx, stmt) in program.body.iter_mut().enumerate() {
self.current_stmt_info.stmt_idx = idx;
self.current_stmt_info.stmt_idx = Some(idx);
self.visit_statement(stmt);
self.result.stmt_infos.push(std::mem::take(&mut self.current_stmt_info));
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rolldown_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ pub use crate::{
named_import::NamedImport,
raw_path::RawPath,
resolved_export::{ResolvedExport, ResolvedExportRuntime},
stmt_info::{StmtInfo, StmtInfoId, VirtualStmtInfo},
stmt_info::StmtInfo,
symbol_ref::SymbolRef,
};
18 changes: 6 additions & 12 deletions crates/rolldown_common/src/stmt_info.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
use crate::SymbolRef;

index_vec::define_index_type! {
pub struct StmtInfoId = u32;
}

#[derive(Default, Debug)]
pub struct StmtInfo {
pub stmt_idx: usize,
/// The index of this statement in the module body.
///
/// We will create some facade statements while bundling, and the facade statements
/// don't have a corresponding statement in the original module body, which means
/// `stmt_idx` will be `None`.
pub stmt_idx: Option<usize>,
// currently, we only store top level symbols
pub declared_symbols: Vec<SymbolRef>,
// 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>,
}

// Because we want declare symbols at linker, it shouldn't mutate the original `StmtInfo`.
#[derive(Default, Debug)]
pub struct VirtualStmtInfo {
pub declared_symbols: Vec<SymbolRef>,
pub referenced_symbols: Vec<SymbolRef>,
}

0 comments on commit 344be1a

Please sign in to comment.