Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/celox-frontend-sv/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,8 @@ fn lower_package_states(
ff_parts: bool,
warnings: &mut Vec<crate::warning::PendingWarning>,
) -> Result<Vec<(String, ModuleId, LoweredSvModule)>, sv::AnalyzerError> {
let mut states = packages.state_modules().collect::<Vec<_>>();
let states = packages.specialized_state_modules();
let mut states = states.iter().collect::<Vec<_>>();
states.sort_by(|left, right| left.name().cmp(right.name()));
states
.into_iter()
Expand Down
48 changes: 48 additions & 0 deletions crates/celox-sv-analyzer/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ use crate::source_tree::SourceTree as SyntaxTree;
#[derive(Default)]
pub(super) struct AnalysisState {
pub(crate) const_functions: const_functions::State,
foreign: std::rc::Rc<classes::foreign::Registry>,
foreign_arguments: RefCell<Vec<(String, classes::specializations::BoundArguments)>>,
foreign_names: RefCell<Vec<(String, classes::specializations::BoundArguments, String)>>,
foreign_error: RefCell<Option<AnalyzerError>>,
scope: scope::State,
interfaces: interface_ast::State,
dimensions: dimensions::State,
Expand Down Expand Up @@ -93,6 +97,7 @@ impl<'a> AnalysisContext<'a> {
syntax_tree,
state: std::rc::Rc::new(AnalysisState {
next_call_site: packages.call_sites.clone(),
foreign: packages.foreign.clone(),
..AnalysisState::default()
}),
parameter_overrides: Cow::Owned(HashMap::default()),
Expand Down Expand Up @@ -696,6 +701,49 @@ impl Module {
imported: Arc<scope::ScopeSymbols>,
export_symbols: bool,
header_ports: Option<&port_owners::Selection<'_>>,
) -> Result<Self, AnalyzerError> {
let registry = syntax_tree.state.foreign.clone();
for _ in 0..1024 {
let revision = registry.revision();
let mut symbols = (*imported).clone();
registry.extend(syntax_tree, &mut symbols);
let declarations_only = symbols.declarations_only;
let result = Self::from_selected_scope_once(
syntax_tree,
override_module_name,
parameter_overrides,
interfaces,
Arc::new(symbols),
export_symbols || registry.has_templates(),
header_ports,
);
if let Ok(module) = &result
&& !declarations_only
&& let Some(symbols) = &module.symbols
{
registry.finish(syntax_tree, symbols)?;
}
if registry.revision() == revision {
return result;
}
syntax_tree.state.classes.borrow_mut().clear();
syntax_tree.state.class_specializations.borrow_mut().clear();
syntax_tree.state.foreign_error.borrow_mut().take();
syntax_tree.state.foreign_names.borrow_mut().clear();
}
Err(unsupported(
"foreign class specialization discovery did not converge",
))
}

fn from_selected_scope_once(
syntax_tree: &AnalysisContext<'_>,
override_module_name: &str,
parameter_overrides: &HashMap<String, ConstExpr>,
interfaces: &InterfaceLookup<'_>,
imported: Arc<scope::ScopeSymbols>,
export_symbols: bool,
header_ports: Option<&port_owners::Selection<'_>>,
) -> Result<Self, AnalyzerError> {
// The scope makes its class specializations until its class methods
// have signatures.
Expand Down
78 changes: 41 additions & 37 deletions crates/celox-sv-analyzer/src/ast/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::*;
use crate::system_functions::{HandleProperty, ObjectField, ObjectOp};

pub(super) mod constraints;
pub(super) mod foreign;
pub(super) mod specializations;

/// Bit width of a handle, see `celox_design::HANDLE_WIDTH`.
Expand Down Expand Up @@ -371,7 +372,12 @@ pub(super) fn declared_name(
instances::normalize_identifier(&unit.name.spelling)
),
Some(_) => name,
None => format!("{}::{name}", packages::UNIT),
None => {
use std::hash::{Hash, Hasher};
let mut hasher = fxhash::FxHasher::default();
tree.compilation_path().hash(&mut hasher);
format!("{}::{name}@{:016x}", packages::UNIT, hasher.finish())
}
}
}

Expand Down Expand Up @@ -435,9 +441,15 @@ pub(super) fn scope_class_types(tree: &AnalysisContext<'_>) -> Vec<(String, Type
/// declares (IEEE 1800-2023 3.12.1). Classes of packages are seen through
/// imports, as types.
pub(super) fn resolve(tree: &AnalysisContext<'_>, name: &str) -> Option<String> {
if tree.state.foreign.contains(name) {
return Some(name.to_owned());
}
let native = tree.declarations();
let scope = tree.root_unit().ok().map(|unit| unit.tokens);
let mut unit_class = None;
let package_scope = tree
.root_unit()
.is_ok_and(|unit| unit.kind == celox_sv_syntax::UnitKind::Package);
for class in &native.classes {
if instances::normalize_identifier(&class.name.spelling) != name {
continue;
Expand All @@ -446,25 +458,35 @@ pub(super) fn resolve(tree: &AnalysisContext<'_>, name: &str) -> Option<String>
if enclosing == scope {
return Some(declared_name(tree, class));
}
if enclosing.is_none() {
if enclosing.is_none() && !package_scope {
unit_class = Some(declared_name(tree, class));
}
}
// A class an import makes visible (IEEE 1800-2023 26.3).
let imported = scope::imported(&tree.state);
if let Some(class) = imported.aliases.get(name)
&& imported.classes.iter().any(|known| &known.name == class)
{
return Some(class.clone());
if let Some(alias) = imported.aliases.get(name) {
let class = imported
.type_aliases
.get(alias)
.and_then(Type::class)
.unwrap_or(alias);
if imported.classes.iter().any(|known| known.name == class)
|| tree.state.foreign.contains(class)
{
return Some(class.to_owned());
}
}
unit_class
.or_else(|| {
let class = format!("{}::{name}", packages::UNIT);
if package_scope {
return None;
}
let alias = format!("{}::{name}", packages::UNIT);
imported
.classes
.iter()
.any(|known| known.name == class)
.then_some(class)
.type_aliases
.get(&alias)
.and_then(Type::class)
.map(str::to_owned)
})
.or_else(|| (name == "process").then(|| super::processes::CLASS.to_owned()))
}
Expand Down Expand Up @@ -495,6 +517,7 @@ pub(super) fn is_interface(tree: &AnalysisContext<'_>, name: &str) -> bool {
/// 1800-2023 8.25).
pub(super) fn is_parameterized(tree: &AnalysisContext<'_>, name: &str) -> bool {
declaration(tree, name).is_some_and(|class| class.parameter_ports.is_some())
|| tree.state.foreign.contains(name)
}

/// The classes and interface classes that body item `item` declares: of a
Expand Down Expand Up @@ -912,7 +935,7 @@ pub(super) fn class(
}
let class = Rc::new(Class {
name: name.to_owned(),
id: class_id(tree, name),
id: class_id(name),
constraints,
has_constraints: declaration.has_constraints
|| base.as_ref().is_some_and(|base| base.has_constraints),
Expand Down Expand Up @@ -1033,7 +1056,7 @@ fn interface_class(
}
let class = Rc::new(Class {
name: name.to_owned(),
id: class_id(tree, name),
id: class_id(name),
base: None,
interfaces,
is_abstract: true,
Expand Down Expand Up @@ -1176,30 +1199,11 @@ fn field(

/// A run-time identifier of class `name`: an FNV-1a hash, never 0, which
/// `null` reads as.
fn class_id(tree: &AnalysisContext<'_>, name: &str) -> u32 {
// Classes of the compilation units of two sources may share a name; the
// path of the source tells them apart (IEEE 1800-2023 3.12.1).
let source = name
.starts_with(&format!("{}::", packages::UNIT))
.then(|| {
let native = tree.declarations();
native.tokens.first().map(|token| {
native
.sources
.file(token.origin.spelling.file)
.path
.display()
.to_string()
})
})
.flatten()
.unwrap_or_default();
let hash = name
.bytes()
.chain(source.bytes())
.fold(0x811c_9dc5u32, |hash, byte| {
(hash ^ u32::from(byte)).wrapping_mul(0x0100_0193)
});
fn class_id(name: &str) -> u32 {
// Compilation-unit declaration names already contain their source identity.
let hash = name.bytes().fold(0x811c_9dc5u32, |hash, byte| {
(hash ^ u32::from(byte)).wrapping_mul(0x0100_0193)
});
hash.max(1)
}

Expand Down
Loading
Loading