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
4 changes: 2 additions & 2 deletions crates/celox-sv-analyzer/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ mod expressions;
mod ff_process;
mod functions;
mod generate;
mod imports;
pub(crate) mod imports;
mod inlining;
mod instances;
mod interface_ast;
Expand All @@ -147,7 +147,7 @@ mod subroutine_owners;
pub(crate) mod timescales;
pub(crate) mod type_parameters;
mod types;
mod validation;
pub(crate) mod validation;
pub(crate) mod warnings;

use array_compatibility::selected_unpacked_shape;
Expand Down
8 changes: 4 additions & 4 deletions crates/celox-sv-analyzer/src/ast/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,10 +692,10 @@ fn parameter_literal_references(
ranges.push(item.tokens);
}
let mut references = HashSet::default();
for (_, expression) in native.expression_nodes() {
if !ranges.iter().any(|range| {
range.start <= expression.tokens.start && expression.tokens.end <= range.end
}) {
let scope = crate::source_tree::Ranges::new(ranges.iter().copied());
for id in tree.spans().expressions.within(&ranges) {
let expression = native.expression(id);
if !scope.contains(expression.tokens) {
continue;
}
if let ExprKind::Name(name) | ExprKind::Member { name, .. } = &expression.kind {
Expand Down
103 changes: 68 additions & 35 deletions crates/celox-sv-analyzer/src/ast/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ impl ScopeImports {
// Qualified expression and type names carry their package segment natively.
let native = tree.declarations();
let ranges = active_ranges(tree)?;
for expression in &native.expressions {
let spans = tree.spans();
for id in spans.expressions.within(&ranges) {
let expression = native.expression(id);
if !in_ranges(expression.tokens.start, &ranges) {
continue;
}
Expand Down Expand Up @@ -138,7 +140,8 @@ impl ScopeImports {
}
}
}
for ty in &native.types {
for id in spans.types.within(&ranges) {
let ty = native.data_type(id);
if in_ranges(ty.tokens.start, &ranges)
&& let celox_sv_syntax::DataTypeKind::Named(path) = &ty.kind
&& let [package, name] = path.as_slice()
Expand Down Expand Up @@ -353,6 +356,59 @@ fn in_ranges(offset: usize, ranges: &[celox_sv_syntax::TokenRange]) -> bool {
let next = ranges.partition_point(|range| range.start <= offset);
next > 0 && offset < ranges[next - 1].end
}
/// Whole-file facts the reference collection selects from, built once per
/// file so each scope costs what its own token ranges contain.
#[derive(Debug)]
pub(crate) struct Facts {
/// Token spans of the plain-name packages of qualified names.
qualified_bases: HashSet<(usize, usize)>,
/// First tokens of the bare-name keys of patterns.
pattern_keys: HashSet<usize>,
}

impl Facts {
fn of(tree: &SyntaxTree) -> &Self {
use celox_sv_syntax::ExprKind;
tree.spans().imports.get_or_init(|| {
let native = tree.declarations();
let name = |id| {
matches!(native.expression(id).kind, ExprKind::Name(_))
.then_some(native.expression(id).tokens)
};
let qualified_bases = native
.expressions
.iter()
.filter_map(|expression| match &expression.kind {
ExprKind::Member {
expression,
separator,
..
} if separator == "::" => name(*expression),
_ => None,
})
.map(|tokens| (tokens.start, tokens.end))
.collect();
// Bare pattern keys name members; they do not bind wildcard imports.
let pattern_keys = native
.expressions
.iter()
.flat_map(|expression| match &expression.kind {
ExprKind::Pattern { items, .. } => {
items.iter().filter_map(|item| item.key).collect::<Vec<_>>()
}
_ => Vec::new(),
})
.filter_map(name)
.map(|tokens| tokens.start)
.collect();
Self {
qualified_bases,
pattern_keys,
}
})
}
}

/// Unqualified expression and type references in the selected native scope.
pub(super) fn unqualified_names(tree: &SyntaxTree) -> HashMap<String, Vec<usize>> {
use celox_sv_syntax::{DataTypeKind, ExprKind};
Expand All @@ -361,33 +417,10 @@ pub(super) fn unqualified_names(tree: &SyntaxTree) -> HashMap<String, Vec<usize>
return HashMap::default();
};
let nested = nested_declarations(tree);
let qualified: HashSet<_> = native
.expressions
.iter()
.filter_map(|expression| match &expression.kind {
ExprKind::Member {
expression,
separator,
..
} if separator == "::" => Some(*expression),
_ => None,
})
.collect();
// Bare pattern keys name members; they do not bind wildcard imports.
let pattern_keys: HashSet<_> = native
.expressions
.iter()
.flat_map(|expression| match &expression.kind {
ExprKind::Pattern { items, .. } => {
items.iter().filter_map(|item| item.key).collect::<Vec<_>>()
}
_ => Vec::new(),
})
.filter_map(|id| {
matches!(native.expression(id).kind, ExprKind::Name(_))
.then_some(native.expression(id).tokens.start)
})
.collect();
let Facts {
qualified_bases,
pattern_keys,
} = Facts::of(tree);
let mut names: HashMap<String, Vec<usize>> = HashMap::default();
let mut add = |name: &str, offset| {
let name = normalize_identifier(name);
Expand All @@ -399,18 +432,18 @@ pub(super) fn unqualified_names(tree: &SyntaxTree) -> HashMap<String, Vec<usize>
names.entry(name).or_default().push(offset);
}
};
for expression in &native.expressions {
let spans = tree.spans();
for id in spans.expressions.within(&ranges) {
let expression = native.expression(id);
if let ExprKind::Name(name) = &expression.kind
&& !pattern_keys.contains(&expression.tokens.start)
&& !qualified.iter().any(|base| {
native.expression(*base).tokens == expression.tokens
&& matches!(&native.expression(*base).kind, ExprKind::Name(_))
})
&& !qualified_bases.contains(&(expression.tokens.start, expression.tokens.end))
{
add(&name.spelling, expression.tokens.start);
}
}
for ty in &native.types {
for id in spans.types.within(&ranges) {
let ty = native.data_type(id);
if let DataTypeKind::Named(path) = &ty.kind
&& let [name] = path.as_slice()
{
Expand Down
45 changes: 11 additions & 34 deletions crates/celox-sv-analyzer/src/ast/timescales.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ struct Unit {
pub(super) struct Timescales {
// Identical expanded tokens have identical native scope positions. Content keys
// remain valid when ParsedSource values move or their trees are dropped.
units: HashMap<String, Arc<Unit>>,
units: HashMap<crate::source_tree::SourceText, Arc<Unit>>,
definitions: HashMap<String, Scope>,
packages: HashMap<String, Scope>,
precision: Option<i32>,
Expand Down Expand Up @@ -210,12 +210,8 @@ fn install_context<'a>(
}
}

fn key(tree: &SyntaxTree) -> String {
tree.declarations()
.tokens
.iter()
.map(|token| token.text.as_str())
.collect()
fn key(tree: &SyntaxTree) -> crate::source_tree::SourceText {
tree.source_text().clone()
}

fn exponent(number: &str, suffix: &str) -> Converted<i32> {
Expand Down Expand Up @@ -384,34 +380,15 @@ pub(crate) fn generated_source_declarations(
pub(super) fn validate_queries(
tree: &AnalysisContext<'_>,
ranges: &[celox_sv_syntax::TokenRange],
excluded: &[celox_sv_syntax::TokenRange],
excluded: &crate::source_tree::Ranges,
) -> Converted<()> {
use celox_sv_syntax::{ExprKind, StatementKind};
let native = tree.declarations();
let callees: HashSet<_> = native
.expression_nodes()
.filter_map(|(_, node)| match node.kind {
ExprKind::Call { expression, .. } => Some(expression),
_ => None,
})
.collect();
let statements: HashSet<_> = native
.statements
.iter()
.filter_map(|statement| match statement.kind {
StatementKind::Expression(id) => Some(id),
_ => None,
})
.collect();
for (id, expression) in native.expression_nodes() {
let at = expression.tokens.start;
if !ranges
.iter()
.any(|range| range.start <= at && at < range.end)
|| excluded
.iter()
.any(|range| range.start <= at && at < range.end)
{
let facts = validation::Facts::of(tree);
let (callees, statements) = (&facts.callees, &facts.expression_statements);
let scope = crate::source_tree::Ranges::new(ranges.iter().copied());
for id in tree.spans().expressions.within(ranges) {
let at = native.expression(id).tokens.start;
if !scope.covers(at) || excluded.covers(at) {
continue;
}
if callees.contains(&id) {
Expand Down Expand Up @@ -743,7 +720,7 @@ impl Timescales {
.insert(scopes[i].name.clone(), scopes[i].clone());
}
design.units.insert(
key(tree).to_string(),
key(tree),
Arc::new(Unit {
scale: unit,
scopes,
Expand Down
7 changes: 6 additions & 1 deletion crates/celox-sv-analyzer/src/ast/type_binding/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,12 @@ pub(in crate::ast) fn packed_ranges(
// historical primitive/alias kernel includes nested datatype dimensions.
// Aggregate fields use their own independently scheduled type ranges.
let mut all = dimensions.to_vec();
for data in &tree.declarations().types {
let bounds = dimensions
.iter()
.map(|dimension| dimension.tokens)
.collect::<Vec<_>>();
for id in tree.spans().types.within(&bounds) {
let data = tree.declarations().data_type(id);
if dimensions.iter().any(|bound| {
bound.tokens.start <= data.tokens.start && data.tokens.end <= bound.tokens.end
}) {
Expand Down
Loading
Loading