use std::collections::HashMap;
use crate::ast::{Stmt, UseImport, UseImportKind};
use super::{
AnalysisDiagnostic, Analyzer, Definition, SymbolContainer, SymbolKind, SymbolOccurrence,
};
pub(super) type ModuleExport = super::ExternalModuleExport;
pub(super) fn analyze(analyzer: &mut Analyzer, imports: &[UseImport]) {
for import in imports {
if let Some(error) = analyzer.declaration_types.script_access(
&analyzer.module_path,
&import.path,
import.span,
) {
analyzer.result.diagnostics.push(error);
continue;
}
let exported = imported_export(analyzer, import);
let path = import.path.join("::");
let local = analyzer
.declaration_types
.direct_module_let(&path, &analyzer.module_path);
let kind = exported
.as_ref()
.map(|export| export.kind)
.or_else(|| local.map(|_| SymbolKind::Variable));
if import.kind == UseImportKind::Single && kind.is_none() {
if let Some(private) = analyzer.declaration_types.inaccessible_import_path(
&path,
&analyzer.module_path,
None,
) {
analyzer.resolution_error(
format!("import path `{private}` is private"),
import.name_span,
);
} else {
analyzer
.resolution_error(format!("cannot resolve import `{path}`"), import.name_span);
}
continue;
}
for (index, (segment, segment_span)) in
import.path.iter().zip(&import.path_spans).enumerate()
{
let is_imported_item =
index + 1 == import.path.len() && import.kind == UseImportKind::Single;
if index == 0 && !is_imported_item {
analyzer.reference(segment, *segment_span);
} else {
analyzer.result.symbols.push(SymbolOccurrence {
is_derive_generated: analyzer.inside_derive,
external_function: is_imported_item
.then(|| {
exported
.as_ref()
.and_then(|export| export.external_function.clone())
})
.flatten(),
name: segment.clone(),
span: *segment_span,
definition_span: is_imported_item
.then(|| exported.as_ref().and_then(|export| export.span))
.flatten(),
symbol_id: None,
definition_id: is_imported_item
.then(|| exported.as_ref().and_then(|export| export.definition_id))
.flatten(),
kind: if is_imported_item {
kind.expect("resolved single import")
} else {
SymbolKind::Module
},
is_definition: false,
inferred_type: is_imported_item
.then(|| {
exported
.as_ref()
.and_then(|export| export.inferred_type.clone())
})
.flatten(),
detail: is_imported_item
.then(|| exported.as_ref().and_then(|export| export.detail.clone()))
.flatten(),
container: is_imported_item
.then(|| {
exported
.as_ref()
.map(|export| SymbolContainer::Module(export.module_path.clone()))
})
.flatten(),
});
}
}
let Some(name) = import.binding_name() else {
import_glob(analyzer, import);
continue;
};
let name_span = import.alias_span.unwrap_or(import.name_span);
analyzer.define(name, name_span, kind.expect("resolved single import"));
if let Some(exported) = exported {
if let Some(detail) = exported.detail.clone() {
analyzer.set_last_detail(detail);
}
analyzer.set_last_container(SymbolContainer::Module(exported.module_path.clone()));
let definition = analyzer
.scopes
.last_mut()
.expect("scope")
.get_mut(name)
.expect("import binding");
definition.span = exported.span;
definition.canonical_path = Some(exported.canonical_path.clone());
definition.external_function = exported.external_function.clone();
definition.id = exported.definition_id;
let symbol = analyzer
.result
.symbols
.last_mut()
.expect("import occurrence");
symbol.definition_id = exported.definition_id;
symbol.definition_span = exported.span;
symbol.external_function = exported.external_function;
symbol.inferred_type = exported.inferred_type;
}
}
}
fn imported_export(analyzer: &Analyzer, import: &UseImport) -> Option<ModuleExport> {
path_export(
&analyzer.module_exports,
&analyzer.module_path,
&import.path,
)
}
pub(super) fn path_export(
exports: &HashMap<String, Vec<ModuleExport>>,
prefix: &[String],
path: &[String],
) -> Option<ModuleExport> {
crate::exports::resolve_export(exports, prefix, path).cloned()
}
fn import_glob(analyzer: &mut Analyzer, import: &UseImport) {
let module = crate::exports::resolve_module_path(
&analyzer.module_exports,
&analyzer.module_path,
&import.path,
);
let exports = module
.and_then(|module| analyzer.module_exports.get(&module))
.cloned();
let Some(exports) = exports else {
analyzer.resolution_error(
format!(
"cannot resolve wildcard import `{}::*`",
import.path.join("::")
),
import.span,
);
return;
};
for export in exports {
if !export.visibility.glob_visible_from(&analyzer.module_path)
&& !(export.kind == SymbolKind::Variant
&& export.visibility.visible_from(&analyzer.module_path))
{
continue;
}
if analyzer
.scopes
.last()
.and_then(|scope| scope.get(&export.name))
.is_some_and(|existing| {
existing.kind == export.kind
&& (export.external_function.is_some()
&& existing.external_function == export.external_function
|| export.span.is_some() && existing.span == export.span
|| export.span.is_none()
&& existing.span.is_none()
&& existing.canonical_path.as_ref() == Some(&export.canonical_path))
})
{
continue;
}
if analyzer
.scopes
.last()
.is_some_and(|scope| scope.contains_key(&export.name))
{
analyzer.result.diagnostics.push(AnalysisDiagnostic::error(
format!("`{}` is already defined in this scope", export.name),
import.span,
));
continue;
}
analyzer.scopes.last_mut().expect("scope exists").insert(
export.name,
Definition {
canonical_path: Some(export.canonical_path),
external_function: export.external_function,
span: export.span,
id: export.definition_id,
kind: export.kind,
container: Some(SymbolContainer::Module(export.module_path)),
},
);
}
}
pub(super) fn collect_module_exports(
statements: &[Stmt],
module_path: &[String],
packages: &crate::exports::PackageScopes,
) -> HashMap<String, Vec<ModuleExport>> {
let mut exports = HashMap::new();
crate::exports::collect_package_statements(
statements,
module_path,
None,
&mut exports,
packages,
);
exports
}