TODO section 26; closes docs/topo.md 2.9-15 and 2.9-19; coverage_gaps
M4/M5/M6 corrected against a fresh opentitan census.
A bind directive's target is a path of segments with optional indices
(bind top.gen[0].u_core chk u_chk (...)); a lone target binds as a
module reference, an instance path records nothing rather than a guess.
Each connection's port half resolves against the module being bound in
and its expression half in the target module's scope first, the
workspace's member path when the target lives elsewhere, the writing
scope last (Target::BindScope, resolve::in_bind_target - the
inline-constraint shape). FACTS_FORMAT 14 -> 15: binds now post
references and an instantiation edge.
Expression-position spaced macro calls (assign x = `WIDTH (a + b);)
keep the author's gap, read from the source as glued_null_item reads
its gap; verify.rs untouched, both pins stand. A conditional directive
inside a connection value becomes ExprKind::Verbatim, byte-exact
through raw_inline as Doc::Inline, with the closing paren's line and
the list's break read from the source - so the instantiation keeps its
edge, lens and port references instead of collapsing to opaque bytes
(opentitan: the two padring sites in chip_earlgrey_{asic,cw340}.sv,
+2 edges, everything else byte-identical to 0.31.0).
lucisv.instantiate [uri, position, module] renders the inline ghost's
plan as plain text through workspace/applyEdit for clients without
inline completion; the Neovim README documents it.
QA: 3186 tests / 47 binaries, corpus 814/820, independent binary diff
over corpus and 2 645 opentitan files: only the two named files differ.
docs/features/bind-semantics/README.md.
Co-Authored-By: Claude Fable 5 <[email protected]>
2227 lines
97 KiB
Rust
2227 lines
97 KiB
Rust
//! Pass one: walk the tree, open scopes, bind declarations, and record every
|
|
//! identifier *use* as a pending reference. Nothing is resolved here — that is
|
|
//! `resolve.rs`, and the split is what makes forward references work.
|
|
|
|
use std::collections::HashMap;
|
|
use vlog_parser::ast::*;
|
|
use vlog_parser::span::TextRange;
|
|
use vlog_parser::token::is_keyword;
|
|
|
|
use crate::model::{Import, Scope, ScopeId, ScopeKind, Symbol, SymbolId, SymbolKind};
|
|
use crate::scan::{name_after_kw, NameScanner};
|
|
|
|
/// A recorded use, waiting for pass two to say what it names.
|
|
pub(crate) struct Pending {
|
|
pub name: String,
|
|
pub name_span: TextRange,
|
|
pub scope: ScopeId,
|
|
pub target: Target,
|
|
}
|
|
|
|
/// Where pass two must look for a pending use.
|
|
pub(crate) enum Target {
|
|
/// The lexical chain from `scope` outward, imports included.
|
|
Lexical,
|
|
/// A name written where a **type** goes — the user-defined type of a
|
|
/// declaration, a port, a parameter, a return type, a `virtual` interface
|
|
/// property. Looked up exactly like [`Target::Lexical`], but a miss also
|
|
/// carries the bare name to the workspace, because that is how a
|
|
/// cross-file `interface`, `class` or `typedef` is spelled. Every type
|
|
/// position reaches this through [`Binder::bind_data_type`]; an
|
|
/// identifier in a *value* position never does.
|
|
Type,
|
|
/// A package name: the `p` of `import p::x`. Only a package can be
|
|
/// imported, so this stays narrower than [`Target::Unit`].
|
|
Package,
|
|
/// A module name: the type of an instantiation.
|
|
Module,
|
|
/// A unit name — package or class (or a `typedef class` forward
|
|
/// declaration): the head of `p::x`, an `extends`/`implements` base, the
|
|
/// `cls` of an out-of-body method definition.
|
|
Unit,
|
|
/// The `x` of `p::x` (and an `extends base #(.x(...))` override): looked
|
|
/// up inside the named unit's scope only, never in the lexical chain.
|
|
UnitMember(String),
|
|
/// The `.port` of a connection to the named module.
|
|
Port(String),
|
|
/// A name that may only be looked up in the scope it is **written in**,
|
|
/// never outward: a modport's port list (IEEE 1800 §25.5 — every name it
|
|
/// governs is declared in that very interface) and a clocking block's
|
|
/// signal list (§14.4). Walking outward here would be a wrong answer
|
|
/// waiting to happen: `modport mp (input other_if)` must not find a
|
|
/// file-scope `other_if` because the interface itself declares no such
|
|
/// signal. A miss records **nothing**.
|
|
InScope,
|
|
/// The `mp` of a glued interface type name — `bus_if.mp bus`,
|
|
/// `virtual bus_if.mp vif` (IEEE 1800 §25.4/§25.9). `unit` is the
|
|
/// interface half, which [`Binder::bind_data_type`] binds beside this as
|
|
/// an ordinary [`Target::Type`]; the tail is a *member* of it, looked up
|
|
/// in the interface's own scope when this file declares it and handed to
|
|
/// the workspace as `unit::name` when it does not. A tail an interface
|
|
/// this file declares whole does not bind records nothing, on the same
|
|
/// rule [`Target::MemberOf`] follows.
|
|
TypeMember { unit: String },
|
|
/// The `member` of `head.mids….member`, where `head` is a plain
|
|
/// identifier this file may declare: looked up in the scope of the head's
|
|
/// **declared type**, then up that type's `extends` chain, one hop per
|
|
/// name in `mids`.
|
|
///
|
|
/// Carries the *spellings* rather than the inner tails' resolutions,
|
|
/// because the type of a link is only known once the link before it is
|
|
/// resolved, which is pass two's job — and because a spelling is all the
|
|
/// workspace layer gets either way: a chain that leaves this file becomes
|
|
/// one composed path (`pkt_t::hdr::addr`), read by the same
|
|
/// `follow_one` every other path goes through.
|
|
MemberOf { head: String, mids: Vec<String> },
|
|
/// The `x` of `obj.randomize() with { x < 5; }` — an unqualified name in
|
|
/// an inline constraint block (IEEE 1800 §18.7). It is a member of `obj`
|
|
/// **first**, on exactly the terms [`Target::MemberOf`] chases one, and
|
|
/// the scope the block is *written* in second, which is where §18.7 says
|
|
/// the name resolves next. The fallback is only taken when the object's
|
|
/// own class settles the miss — a candidate for another file is left
|
|
/// alone, because it may still be the member.
|
|
InlineConstraint { head: String, mids: Vec<String> },
|
|
/// A bare name in the **expression** half of a `bind` directive's
|
|
/// connection: `bind top chk u (.x(sig));`. The connection is elaborated
|
|
/// inside the *target* scope (IEEE 1800 §23.11), so `target`'s own scope
|
|
/// answers first and the scope the directive is written in second — the
|
|
/// [`Target::InlineConstraint`] shape, with the same refusal to guess.
|
|
BindScope { target: String },
|
|
/// The `x` of `this.x`: the enclosing class's scope only.
|
|
ThisMember,
|
|
/// The `x` of `super.x`: always the base class's business.
|
|
SuperMember,
|
|
/// Exactly this declaration, with no lookup at all: an end label
|
|
/// (`endmodule : m`, `endfunction : f`, `end : blk`) names the construct
|
|
/// it closes and nothing else.
|
|
Declaration(SymbolId),
|
|
}
|
|
|
|
/// An out-of-body method definition (`function void cls::method(...)`), held
|
|
/// until the whole file is bound so `cls` can be looked up wherever it is
|
|
/// declared — before or after the definition.
|
|
enum Deferred<'a> {
|
|
Function(&'a FunctionDecl, TextRange, ScopeId),
|
|
Task(&'a TaskDecl, TextRange, ScopeId),
|
|
/// `constraint cls::c { ... }` — the constraint sibling, held for the
|
|
/// same reason and finished through the same stand-in scope.
|
|
Constraint(&'a ConstraintDecl, TextRange, ScopeId),
|
|
}
|
|
|
|
pub(crate) struct Binder<'a> {
|
|
src: &'a str,
|
|
pub(crate) scopes: Vec<Scope>,
|
|
pub(crate) symbols: Vec<Symbol>,
|
|
pub(crate) pending: Vec<Pending>,
|
|
deferred: Vec<Deferred<'a>>,
|
|
/// While a `randomize() with { ... }` block is being bound: the head of
|
|
/// the object being randomized, so a bare name inside the block is
|
|
/// recorded as a member of it. See [`Binder::bind_with_constraint`].
|
|
with_head: Option<(String, Vec<String>)>,
|
|
/// While a `bind` directive's connections are being bound: the target
|
|
/// scope they are elaborated in, so a bare name in a connection's value
|
|
/// is looked up there first. `None` for a hierarchical instance path,
|
|
/// which this crate does not resolve. See [`Binder::bind_bind`].
|
|
bind_target: Option<String>,
|
|
}
|
|
|
|
impl<'a> Binder<'a> {
|
|
pub(crate) fn new(src: &'a str, file_span: TextRange) -> Self {
|
|
let file = Scope {
|
|
kind: ScopeKind::File,
|
|
parent: None,
|
|
owner: None,
|
|
span: file_span,
|
|
symbols: Vec::new(),
|
|
children: Vec::new(),
|
|
imports: Vec::new(),
|
|
extends: None,
|
|
names: HashMap::new(),
|
|
};
|
|
Binder {
|
|
src,
|
|
scopes: vec![file],
|
|
symbols: Vec::new(),
|
|
pending: Vec::new(),
|
|
deferred: Vec::new(),
|
|
with_head: None,
|
|
bind_target: None,
|
|
}
|
|
}
|
|
|
|
pub(crate) fn file_scope(&self) -> ScopeId {
|
|
ScopeId(0)
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// Tables
|
|
// ------------------------------------------------------------------
|
|
|
|
fn new_scope(&mut self, kind: ScopeKind, parent: ScopeId, span: TextRange) -> ScopeId {
|
|
let id = ScopeId(self.scopes.len() as u32);
|
|
self.scopes.push(Scope {
|
|
kind,
|
|
parent: Some(parent),
|
|
owner: None,
|
|
span,
|
|
symbols: Vec::new(),
|
|
children: Vec::new(),
|
|
imports: Vec::new(),
|
|
extends: None,
|
|
names: HashMap::new(),
|
|
});
|
|
self.scopes[parent.0 as usize].children.push(id);
|
|
id
|
|
}
|
|
|
|
/// Bind `name` in `scope`. A name already bound there becomes a reference
|
|
/// to the first declaration instead of a second symbol — which is how a
|
|
/// non-ANSI port's body declaration attaches to its header port, and how
|
|
/// an out-of-body method definition attaches to its `extern` prototype.
|
|
fn declare(
|
|
&mut self,
|
|
scope: ScopeId,
|
|
name: &str,
|
|
kind: SymbolKind,
|
|
name_span: TextRange,
|
|
decl_span: TextRange,
|
|
) -> Option<SymbolId> {
|
|
if name.is_empty() {
|
|
return None;
|
|
}
|
|
if self.scopes[scope.0 as usize].names.contains_key(name) {
|
|
self.use_name(scope, name, name_span, Target::Lexical);
|
|
return None;
|
|
}
|
|
let id = SymbolId(self.symbols.len() as u32);
|
|
self.symbols.push(Symbol {
|
|
name: name.to_string(),
|
|
kind,
|
|
name_span,
|
|
decl_span,
|
|
detail_span: None,
|
|
type_name: None,
|
|
scope,
|
|
child_scope: None,
|
|
});
|
|
let s = &mut self.scopes[scope.0 as usize];
|
|
s.symbols.push(id);
|
|
s.names.insert(name.to_string(), id);
|
|
Some(id)
|
|
}
|
|
|
|
/// [`Binder::declare`] for a declaration written with a type, recording
|
|
/// that type's name on the symbol — what `handle.member` is looked up
|
|
/// with. A redeclaration binds nothing and records nothing here either.
|
|
///
|
|
/// `ty` is `None` where the declared name is not itself a value of the
|
|
/// type: an **unpacked** declarator (`c1 q[$]`, `item_t pool[4]`) names a
|
|
/// container, and `q.size()` is a built-in array method, not a member of
|
|
/// `c1` — the element is reached through an index, which is a head this
|
|
/// crate does not type (TODO.md §15, round 2).
|
|
fn declare_typed(
|
|
&mut self,
|
|
scope: ScopeId,
|
|
name: &str,
|
|
kind: SymbolKind,
|
|
name_span: TextRange,
|
|
decl_span: TextRange,
|
|
ty: Option<&DataType>,
|
|
) -> Option<SymbolId> {
|
|
let id = self.declare(scope, name, kind, name_span, decl_span);
|
|
if let Some(id) = id {
|
|
self.symbols[id.0 as usize].type_name = ty.and_then(type_name_of);
|
|
}
|
|
id
|
|
}
|
|
|
|
/// Declare `name` and open the scope it owns.
|
|
fn declare_with_scope(
|
|
&mut self,
|
|
scope: ScopeId,
|
|
name: &str,
|
|
kind: SymbolKind,
|
|
name_span: TextRange,
|
|
decl_span: TextRange,
|
|
child_kind: ScopeKind,
|
|
) -> ScopeId {
|
|
let sym = self.declare(scope, name, kind, name_span, decl_span);
|
|
let child = self.new_scope(child_kind, scope, decl_span);
|
|
if let Some(sym) = sym {
|
|
self.symbols[sym.0 as usize].child_scope = Some(child);
|
|
self.scopes[child.0 as usize].owner = Some(sym);
|
|
}
|
|
child
|
|
}
|
|
|
|
/// Record a use — unless it is not a name this crate can say anything
|
|
/// about: a system task (`$display`), a macro standing where a name
|
|
/// belongs (`` `PICORV32_REGS regs (...) ``), a keyword the parser kept
|
|
/// in an identifier slot, or one of the four class words the lexer leaves
|
|
/// as identifiers (`this`/`super` are modelled through their member
|
|
/// accesses, `null` is a literal, and a bare `new` names the constructor
|
|
/// of the *assigned* class, which needs types this crate does not have).
|
|
fn use_name(&mut self, scope: ScopeId, name: &str, name_span: TextRange, target: Target) {
|
|
if matches!(name, "this" | "super" | "null" | "new") {
|
|
return;
|
|
}
|
|
self.use_member(scope, name, name_span, target);
|
|
}
|
|
|
|
/// Like [`Binder::use_name`], but keeps `new`: the tail of `super.new(...)`
|
|
/// really does name the base class's constructor.
|
|
fn use_member(&mut self, scope: ScopeId, name: &str, name_span: TextRange, target: Target) {
|
|
if name.is_empty() || name.starts_with('$') || name.starts_with('`') || is_keyword(name) {
|
|
return;
|
|
}
|
|
// Inside `obj.randomize() with { ... }` a bare name is a member of
|
|
// `obj` before it is anything else (IEEE 1800 §18.7), so the one
|
|
// funnel every use goes through redirects it there. A name written
|
|
// with a head of its own already carries the right target and is left
|
|
// alone.
|
|
let target = match (&self.with_head, &self.bind_target, &target) {
|
|
(Some((head, mids)), _, Target::Lexical) => {
|
|
Target::InlineConstraint { head: head.clone(), mids: mids.clone() }
|
|
}
|
|
// Same funnel, same rule, for a `bind`'s connection expressions:
|
|
// a bare name there is the target scope's before it is anything
|
|
// else. A name written with a head of its own already carries the
|
|
// right target and is left alone.
|
|
(None, Some(scope), Target::Lexical) => Target::BindScope { target: scope.clone() },
|
|
_ => target,
|
|
};
|
|
self.pending.push(Pending { name: name.to_string(), name_span, scope, target });
|
|
}
|
|
|
|
/// `endmodule : m`, `endfunction : f`, `end : blk`: an end label names the
|
|
/// declaration it closes, so it is recorded as a use of exactly that
|
|
/// symbol — never looked up, so it can never attach to a same-named
|
|
/// declaration somewhere else. A label that does not spell the declared
|
|
/// name is an error the linter owns, and records nothing.
|
|
fn use_end_label(
|
|
&mut self,
|
|
scope: ScopeId,
|
|
body: ScopeId,
|
|
label: Option<&String>,
|
|
span: Option<TextRange>,
|
|
) {
|
|
let (Some(label), Some(span)) = (label, span) else {
|
|
return;
|
|
};
|
|
// `None` when the declaration was a redeclaration and bound no symbol
|
|
// of its own; the label is then left unrecorded, as it was before.
|
|
let Some(sym) = self.scopes[body.0 as usize].owner else {
|
|
return;
|
|
};
|
|
if self.symbols[sym.0 as usize].name != *label {
|
|
return;
|
|
}
|
|
let span = self.exact_or_empty(span, label);
|
|
if span.is_empty() {
|
|
return;
|
|
}
|
|
self.use_member(scope, label, span, Target::Declaration(sym));
|
|
}
|
|
|
|
/// `span` when it slices to exactly `name`, else an empty range at its
|
|
/// start. Guards the ranges this crate derives instead of receiving —
|
|
/// a source spelling `pkg :: mod` must cost a span, never mislocate one.
|
|
fn exact_or_empty(&self, span: TextRange, name: &str) -> TextRange {
|
|
let range = span.as_range();
|
|
if range.end <= self.src.len() && self.src.get(range.clone()).is_some_and(|s| s == name) {
|
|
span
|
|
} else {
|
|
TextRange::empty(span.start)
|
|
}
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// Items
|
|
// ------------------------------------------------------------------
|
|
|
|
pub(crate) fn bind_items(&mut self, items: &'a [Item], scope: ScopeId) {
|
|
for item in items {
|
|
self.bind_item(item, scope);
|
|
}
|
|
}
|
|
|
|
/// A file that is the bare middle of some body, bound as that body would
|
|
/// have bound it (`docs/features/fragment-mode/README.md`).
|
|
///
|
|
/// A statement list gets a [`ScopeKind::Block`] of its own, because that is
|
|
/// what its declarations are scoped by wherever the fragment is included —
|
|
/// a `begin ... end`, an `always` body, a task — and because a local of the
|
|
/// snippet has no business being a file-scope name. Enum members and cover
|
|
/// items bind in the file scope itself: an enum label belongs to the scope
|
|
/// *holding* the enum (IEEE 1800 §6.19, the rule
|
|
/// [`Binder::bind_enum_body`] already follows), and a labelled coverpoint
|
|
/// is the one name a cover-item fragment declares.
|
|
pub(crate) fn bind_fragment(
|
|
&mut self,
|
|
fragment: &'a Fragment,
|
|
scope: ScopeId,
|
|
span: TextRange,
|
|
) {
|
|
match fragment {
|
|
Fragment::Statements(stmts) => {
|
|
let inner = self.new_scope(ScopeKind::Block, scope, span);
|
|
self.bind_stmts(stmts, inner);
|
|
}
|
|
Fragment::EnumMembers { members, .. } => self.bind_enum_members(members, scope),
|
|
Fragment::CoverItems(items) => {
|
|
for item in items {
|
|
self.bind_cover_item(item, scope);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn bind_item(&mut self, item: &'a Item, scope: ScopeId) {
|
|
match &item.kind {
|
|
ItemKind::Module(m) => self.bind_module(m, item.span, scope),
|
|
ItemKind::Package { name, name_span, items, end_label, end_label_span, .. } => {
|
|
let inner = self.declare_with_scope(
|
|
scope,
|
|
name,
|
|
SymbolKind::Package,
|
|
*name_span,
|
|
item.span,
|
|
ScopeKind::Package,
|
|
);
|
|
self.bind_items(items, inner);
|
|
self.use_end_label(scope, inner, end_label.as_ref(), *end_label_span);
|
|
}
|
|
ItemKind::Import(paths) => {
|
|
for path in paths {
|
|
self.bind_import_path(path, scope);
|
|
}
|
|
}
|
|
// A `let` is a compile-time function: its ports are visible only
|
|
// inside its own body expression.
|
|
ItemKind::Let(l) => {
|
|
let inner = self.declare_with_scope(
|
|
scope,
|
|
&l.name,
|
|
SymbolKind::Function,
|
|
l.name_span,
|
|
item.span,
|
|
ScopeKind::Function,
|
|
);
|
|
self.bind_tf_args(&l.ports, inner);
|
|
self.bind_expr(&l.value, inner);
|
|
}
|
|
ItemKind::Typedef(t) => self.bind_typedef(t, item.span, scope),
|
|
ItemKind::ParamDecl(p) => {
|
|
self.bind_declarators(&p.declarators, param_kind(&p.kw), &p.ty, item.span, scope)
|
|
}
|
|
ItemKind::DataDecl(d) => self.bind_data_decl(d, item.span, scope),
|
|
ItemKind::PortDecl(p) => self.bind_port_decl(p, item.span, scope),
|
|
ItemKind::ContAssign(c) => {
|
|
self.bind_opt_expr(c.delay.as_ref(), scope);
|
|
for a in &c.assigns {
|
|
self.bind_expr(&a.lhs, scope);
|
|
self.bind_expr(&a.rhs, scope);
|
|
}
|
|
}
|
|
// The arguments name real parameters (`$error("...", W)`), so they
|
|
// resolve like any other expression.
|
|
ItemKind::ElabTask(e) => self.bind_expr(e, scope),
|
|
ItemKind::Always { stmt, .. } => self.bind_stmt(stmt, scope),
|
|
ItemKind::Initial(s) | ItemKind::Final(s) => self.bind_stmt(s, scope),
|
|
ItemKind::Instantiation(i) => self.bind_instantiation(i, scope),
|
|
ItemKind::Bind(b) => self.bind_bind(b, scope),
|
|
ItemKind::GateInstantiation(g) => {
|
|
for d in &g.delay {
|
|
self.bind_expr(d, scope);
|
|
}
|
|
for inst in &g.instances {
|
|
if let (Some(name), Some(ns)) = (&inst.name, inst.name_span) {
|
|
self.declare(scope, name, SymbolKind::Instance, ns, inst.span);
|
|
}
|
|
for a in &inst.args {
|
|
self.bind_expr(&a.expr, scope);
|
|
}
|
|
}
|
|
}
|
|
ItemKind::Genvar(names) => {
|
|
for name in names {
|
|
self.declare(scope, &name.text, SymbolKind::Genvar, name.span, item.span);
|
|
}
|
|
}
|
|
ItemKind::GenerateRegion { items, .. } => {
|
|
let inner = self.new_scope(ScopeKind::Generate, scope, item.span);
|
|
self.bind_items(items, inner);
|
|
}
|
|
ItemKind::GenIf(g) => self.bind_gen_if(g, scope),
|
|
ItemKind::GenFor(g) => self.bind_gen_for(g, item.span, scope),
|
|
ItemKind::GenCase(g) => {
|
|
self.bind_expr(&g.expr, scope);
|
|
for it in &g.items {
|
|
for l in &it.labels {
|
|
self.bind_expr(&l.expr, scope);
|
|
}
|
|
self.bind_gen_block(&it.body, scope);
|
|
}
|
|
}
|
|
ItemKind::Constraint(c) => self.bind_constraint(c, item.span, scope),
|
|
ItemKind::Covergroup(c) => self.bind_covergroup(c, item.span, scope),
|
|
ItemKind::Function(f) => self.bind_function(f, item.span, scope),
|
|
ItemKind::Task(t) => self.bind_task(t, item.span, scope),
|
|
ItemKind::Class(c) => self.bind_class(c, item.span, scope),
|
|
ItemKind::Modport(decls) => {
|
|
for d in decls {
|
|
self.bind_modport(d, scope);
|
|
}
|
|
}
|
|
ItemKind::Clocking(c) => self.bind_clocking(c, item.span, scope),
|
|
ItemKind::Defparam(pairs) => {
|
|
for (lhs, rhs) in pairs {
|
|
self.bind_expr(lhs, scope);
|
|
self.bind_expr(rhs, scope);
|
|
}
|
|
}
|
|
// Every term of an `alias` names an existing net — the item
|
|
// declares nothing — so all of them are references.
|
|
ItemKind::Alias(nets) => {
|
|
for net in nets {
|
|
self.bind_expr(net, scope);
|
|
}
|
|
}
|
|
// Opaque leaves: verbatim text this crate makes no claims about.
|
|
// `Verbatim` is a constraint or covergroup body — no symbol is
|
|
// bound for its name and nothing inside it is scanned, the same
|
|
// contract macro invocations have. `Export` is a leaf too: a DPI
|
|
// export names a function declared elsewhere in the same scope,
|
|
// and a package re-export does not bring the name into this scope
|
|
// (an `import` alongside it does that). Resolving either one is a
|
|
// vlog_semantic decision, not the parser's — see
|
|
// `docs/features/dpi-export/README.md` Handshakes.
|
|
ItemKind::Export(_)
|
|
| ItemKind::Directive(_)
|
|
| ItemKind::MacroItem(_)
|
|
| ItemKind::Timeunit(_)
|
|
| ItemKind::Error(_)
|
|
// A null item (`;`) declares nothing and references nothing.
|
|
| ItemKind::Empty
|
|
| ItemKind::Verbatim(_) => {}
|
|
}
|
|
}
|
|
|
|
fn bind_module(&mut self, m: &'a Module, span: TextRange, scope: ScopeId) {
|
|
// One AST node, three keywords. `interface` gets its own kind — its
|
|
// signals are its surface and another file may reach them, which is
|
|
// exactly what a module never grants. `program` stays a `Module`: it is
|
|
// a module body with a different closing keyword and no export rule of
|
|
// its own to write (`docs/features/interface-semantics/README.md`).
|
|
let kind = match m.kw.as_str() {
|
|
"interface" => SymbolKind::Interface,
|
|
_ => SymbolKind::Module,
|
|
};
|
|
let inner =
|
|
self.declare_with_scope(scope, &m.name, kind, m.name_span, span, ScopeKind::Module);
|
|
for import in &m.imports {
|
|
for path in &import.paths {
|
|
self.bind_import_path(path, inner);
|
|
}
|
|
}
|
|
self.bind_param_ports(m.param_ports.iter().flatten(), inner);
|
|
match &m.ports {
|
|
PortList::Ansi(ports) => {
|
|
for p in ports {
|
|
self.bind_data_type(
|
|
&p.ty,
|
|
TextRange::new(p.span.start, p.name_span.start),
|
|
inner,
|
|
);
|
|
self.declare_typed(
|
|
inner,
|
|
&p.name,
|
|
SymbolKind::Port,
|
|
p.name_span,
|
|
p.span,
|
|
p.unpacked.is_empty().then_some(&p.ty),
|
|
);
|
|
self.bind_dims(&p.unpacked, inner);
|
|
self.bind_opt_expr(p.default.as_ref(), inner);
|
|
}
|
|
}
|
|
PortList::NonAnsi(ports) => {
|
|
for p in ports {
|
|
self.declare(inner, &p.name, SymbolKind::Port, p.span, p.span);
|
|
}
|
|
}
|
|
PortList::None | PortList::Empty => {}
|
|
}
|
|
self.bind_items(&m.items, inner);
|
|
self.use_end_label(scope, inner, m.end_label.as_ref(), m.end_label_span);
|
|
}
|
|
|
|
/// A `#( ... )` header parameter list — module and class headers share it.
|
|
///
|
|
/// The keyword decides the kind, and it is **sticky**: a continuation
|
|
/// entry writes none (`#(parameter A = 1, B = 2)`) and inherits the one
|
|
/// before it, so `#(parameter int Depth = 4, localparam int DepthW = 3,
|
|
/// DepthX = 4)` declares one `Param` and two `LocalParam`s. Exactly the
|
|
/// rule `vlog_refactor::instantiate::params` already applies to decide
|
|
/// what an instantiation may override — the two now agree because they
|
|
/// read the same field the same way, where before this one read nothing
|
|
/// and called everything a `Param`.
|
|
fn bind_param_ports(&mut self, ports: impl Iterator<Item = &'a ParamPort>, scope: ScopeId) {
|
|
let mut kw = String::new();
|
|
for p in ports {
|
|
if let Some(written) = &p.kw {
|
|
kw.clone_from(written);
|
|
}
|
|
self.bind_data_type(&p.ty, TextRange::new(p.span.start, p.name_span.start), scope);
|
|
let ty = p.unpacked.is_empty().then_some(&p.ty);
|
|
self.declare_typed(scope, &p.name, param_kind(&kw), p.name_span, p.span, ty);
|
|
self.bind_dims(&p.unpacked, scope);
|
|
self.bind_opt_expr(p.default.as_ref(), scope);
|
|
}
|
|
}
|
|
|
|
/// Record one `pkg::item` / `pkg::*` path as an import of `scope`.
|
|
fn bind_import_path(&mut self, path: &ImportPath, scope: ScopeId) {
|
|
let item = if path.item.text == "*" {
|
|
None
|
|
} else {
|
|
self.use_name(
|
|
scope,
|
|
&path.item.text,
|
|
path.item.span,
|
|
Target::UnitMember(path.pkg.text.clone()),
|
|
);
|
|
Some(path.item.text.clone())
|
|
};
|
|
self.use_name(scope, &path.pkg.text, path.pkg.span, Target::Package);
|
|
self.scopes[scope.0 as usize].imports.push(Import {
|
|
package: path.pkg.text.clone(),
|
|
item,
|
|
scope: None,
|
|
name_span: path.pkg.span,
|
|
});
|
|
}
|
|
|
|
fn bind_typedef(&mut self, t: &Typedef, span: TextRange, scope: ScopeId) {
|
|
let ns = t.name_span;
|
|
match &t.kind {
|
|
// The aliased type is recorded on the alias, so `typedef c1 my_t;
|
|
// my_t h; h.x` can follow the alias to the class (round 2). An
|
|
// alias **with unpacked dimensions** (`typedef c1 q_t[4];`) names
|
|
// a container, not a value of the type, and is left untyped on
|
|
// the same rule every other unpacked declarator follows.
|
|
TypedefKind::Alias(ty) => {
|
|
self.bind_data_type(ty, TextRange::new(span.start, ns.start), scope);
|
|
let ty = t.unpacked.is_empty().then_some(ty);
|
|
self.declare_typed(scope, &t.name, SymbolKind::Typedef, ns, span, ty);
|
|
}
|
|
TypedefKind::Enum(e) => {
|
|
self.declare(scope, &t.name, SymbolKind::Typedef, ns, span);
|
|
self.bind_enum_body(e, TextRange::new(span.start, ns.start), scope);
|
|
}
|
|
TypedefKind::Struct(s) => {
|
|
let inner = self.declare_with_scope(
|
|
scope,
|
|
&t.name,
|
|
SymbolKind::Typedef,
|
|
ns,
|
|
span,
|
|
ScopeKind::Struct,
|
|
);
|
|
self.bind_struct_uses(s, scope);
|
|
self.bind_struct_members(s, inner);
|
|
}
|
|
// `typedef class c;` promises a class declared elsewhere; the
|
|
// name is declared here so a reference to it resolves to the
|
|
// forward declaration.
|
|
TypedefKind::ForwardClass => {
|
|
self.declare(scope, &t.name, SymbolKind::Typedef, ns, span);
|
|
}
|
|
}
|
|
self.bind_dims(&t.unpacked, scope);
|
|
}
|
|
|
|
/// An `enum` body: its base type, and its member names — which IEEE 1800
|
|
/// §6.19 binds in the scope **holding** the enum, not in the enum itself,
|
|
/// which is why `IDLE` is written bare. One function for all three
|
|
/// spellings that can write one: `typedef enum {…} e_t;`, an anonymous
|
|
/// `enum {…} x;`, and an anonymous enum standing as a struct member's type.
|
|
fn bind_enum_body(&mut self, e: &EnumType, window: TextRange, scope: ScopeId) {
|
|
if let Some(base) = &e.base {
|
|
self.bind_data_type(base, window, scope);
|
|
}
|
|
self.bind_enum_members(&e.members, scope);
|
|
}
|
|
|
|
/// The member names of an enum, declared in `scope` — the scope holding
|
|
/// the enum, per IEEE 1800 §6.19. Shared with an enum-member fragment,
|
|
/// whose holding scope is the file.
|
|
fn bind_enum_members(&mut self, members: &[EnumMember], scope: ScopeId) {
|
|
for m in members {
|
|
self.declare(scope, &m.name, SymbolKind::EnumVariant, m.name_span, m.span);
|
|
self.bind_opt_expr(m.value.as_ref(), scope);
|
|
}
|
|
}
|
|
|
|
/// Every *use* written inside a `struct`/`union` body — each member's type
|
|
/// name, its dimensions, its initializer — recorded in the scope the body
|
|
/// is written in, because that is where those names are looked up from.
|
|
///
|
|
/// Split from [`Binder::bind_struct_members`], which declares the members
|
|
/// themselves, so that a body written once and declared under several
|
|
/// declarators (`struct { hdr_t h; } a, b;`) records its type names once
|
|
/// rather than once per declarator.
|
|
fn bind_struct_uses(&mut self, s: &StructType, scope: ScopeId) {
|
|
for m in &s.members {
|
|
let first = m.declarators.first().map(|d| d.span.start).unwrap_or(m.span.end);
|
|
self.bind_data_type(&m.ty, TextRange::new(m.span.start, first), scope);
|
|
for d in &m.declarators {
|
|
self.bind_dims(&d.unpacked, scope);
|
|
self.bind_opt_expr(d.init.as_ref(), scope);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The members of one `struct`/`union` body, declared in `inner` — the
|
|
/// scope hanging off whatever names the aggregate (a `typedef`, a plain
|
|
/// declarator, or an enclosing member).
|
|
///
|
|
/// Each member carries its own declared type, so `s.hdr.addr` hops through
|
|
/// it exactly as a class property does. A member whose type is an **inline
|
|
/// nested** aggregate (`struct packed { struct packed { logic q; } ctrl; }`
|
|
/// — the shape every generated register package is made of) has no type
|
|
/// name to hop through at all, so it gets a `Struct` scope of its own and
|
|
/// `resolve.rs` reaches its members through that instead.
|
|
fn bind_struct_members(&mut self, s: &StructType, inner: ScopeId) {
|
|
for m in &s.members {
|
|
for d in &m.declarators {
|
|
// The unpacked-declarator rule, as everywhere: `logic [7:0]
|
|
// arr [4]` names a container, not a value of the type.
|
|
let plain = d.unpacked.is_empty();
|
|
let id = self.declare_typed(
|
|
inner,
|
|
&d.name,
|
|
SymbolKind::StructMember,
|
|
d.name_span,
|
|
d.span,
|
|
plain.then_some(&m.ty),
|
|
);
|
|
let (Some(id), Some(TypeExtra::Struct(nested))) = (id, m.ty.inline()) else {
|
|
continue;
|
|
};
|
|
if !plain {
|
|
continue;
|
|
}
|
|
let child = self.new_scope(ScopeKind::Struct, inner, d.span);
|
|
self.symbols[id.0 as usize].child_scope = Some(child);
|
|
self.scopes[child.0 as usize].owner = Some(id);
|
|
self.bind_struct_members(nested, child);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn bind_data_decl(&mut self, d: &DataDecl, span: TextRange, scope: ScopeId) {
|
|
let kind = if d.net.is_some() { SymbolKind::Net } else { SymbolKind::Var };
|
|
self.bind_opt_expr(d.delay.as_ref(), scope);
|
|
self.bind_declarators(&d.declarators, kind, &d.ty, span, scope);
|
|
}
|
|
|
|
fn bind_port_decl(&mut self, p: &PortDecl, span: TextRange, scope: ScopeId) {
|
|
let kind = match self.scopes[scope.0 as usize].kind {
|
|
ScopeKind::Function | ScopeKind::Task => SymbolKind::TfArg,
|
|
_ => SymbolKind::Port,
|
|
};
|
|
// A non-ANSI port's body declaration (`input signed [31:0] data_in;`)
|
|
// completes a port the header only named (`data_in,`). `declare`
|
|
// below attaches it as a reference to that header symbol — and here,
|
|
// the whole declaration's span is recorded on the symbol as where its
|
|
// direction, type and width are written. First writer wins, so a
|
|
// header that names a port twice is the linter's to complain about
|
|
// and this stays deterministic.
|
|
//
|
|
// An old-style task or function argument (`task t(a);` finished by
|
|
// `input [7:0] a;`) is the same header-names-it, body-completes-it
|
|
// shape and merges through the same `declare`, so it is recorded on
|
|
// the same terms — matching on `kind` rather than on `Port` alone.
|
|
//
|
|
// The type is recorded on the same terms: a header port is named
|
|
// without one, and the body declaration is where it is written.
|
|
let names = &self.scopes[scope.0 as usize].names;
|
|
let existing: Vec<(SymbolId, bool)> = p
|
|
.declarators
|
|
.iter()
|
|
.filter_map(|d| Some((names.get(d.name.as_str()).copied()?, d.unpacked.is_empty())))
|
|
.collect();
|
|
for (id, plain) in existing {
|
|
let type_name = plain.then(|| type_name_of(&p.ty)).flatten();
|
|
let symbol = &mut self.symbols[id.0 as usize];
|
|
if symbol.kind != kind {
|
|
continue;
|
|
}
|
|
if symbol.detail_span.is_none() {
|
|
symbol.detail_span = Some(span);
|
|
}
|
|
if symbol.type_name.is_none() {
|
|
symbol.type_name = type_name;
|
|
}
|
|
}
|
|
self.bind_declarators(&p.declarators, kind, &p.ty, span, scope);
|
|
}
|
|
|
|
fn bind_declarators(
|
|
&mut self,
|
|
declarators: &[Declarator],
|
|
kind: SymbolKind,
|
|
ty: &DataType,
|
|
span: TextRange,
|
|
scope: ScopeId,
|
|
) {
|
|
let first = declarators.first().map(|d| d.span.start).unwrap_or(span.end);
|
|
self.bind_data_type(ty, TextRange::new(span.start, first), scope);
|
|
for d in declarators {
|
|
let plain = d.unpacked.is_empty();
|
|
let id =
|
|
self.declare_typed(scope, &d.name, kind, d.name_span, d.span, plain.then_some(ty));
|
|
// An **anonymous** aggregate (`struct packed { logic a; } s;`) has
|
|
// no type name for `s.a` to be looked up through, so the members
|
|
// hang off the declarator itself. In-file only: no other file can
|
|
// name this variable's type, because it has none.
|
|
if let (Some(id), Some(TypeExtra::Struct(s)), true) = (id, ty.inline(), plain) {
|
|
let child = self.new_scope(ScopeKind::Struct, scope, d.span);
|
|
self.symbols[id.0 as usize].child_scope = Some(child);
|
|
self.scopes[child.0 as usize].owner = Some(id);
|
|
self.bind_struct_members(s, child);
|
|
}
|
|
self.bind_dims(&d.unpacked, scope);
|
|
self.bind_opt_expr(d.init.as_ref(), scope);
|
|
}
|
|
}
|
|
|
|
fn bind_instantiation(&mut self, i: &Instantiation, scope: ScopeId) {
|
|
// `module_span` has the documented despace caveat for `pkg :: mod`,
|
|
// so it is taken only when it slices back to the stored name.
|
|
let ns = self.exact_or_empty(i.module_span, &i.module);
|
|
self.use_name(scope, &i.module, ns, Target::Module);
|
|
for c in i.params.iter().flatten() {
|
|
self.bind_connection(c, &i.module, scope);
|
|
}
|
|
for inst in &i.instances {
|
|
let id =
|
|
self.declare(scope, &inst.name, SymbolKind::Instance, inst.name_span, inst.span);
|
|
// An instance's "declared type" is the module it instantiates, so
|
|
// `u.port` and `u.internal_sig` are looked up exactly as a class
|
|
// handle's members are — in the module's own scope, or in the
|
|
// workspace's member table when the module is another file's.
|
|
// An **array** of instances (`sub u[3:0] (...)`) is not one of
|
|
// them, on the same rule an unpacked declarator follows: the
|
|
// element is reached through an index, which is not a head this
|
|
// crate types.
|
|
// A macro standing where the module name belongs (`` `REGS u ()``)
|
|
// names no declaration, exactly as it names none in `use_name`.
|
|
let named =
|
|
!i.module.is_empty() && !i.module.starts_with('`') && !is_keyword(&i.module);
|
|
if let (Some(id), true) = (id, inst.dims.is_empty() && named) {
|
|
self.symbols[id.0 as usize].type_name = Some(i.module.clone());
|
|
}
|
|
self.bind_dims(&inst.dims, scope);
|
|
match &inst.conns {
|
|
Connections::Named(conns) => {
|
|
for c in conns {
|
|
self.bind_connection(c, &i.module, scope);
|
|
}
|
|
}
|
|
Connections::Ordered(conns) => {
|
|
for c in conns {
|
|
self.bind_opt_expr(c.expr.as_ref(), scope);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A `bind` directive (IEEE 1800 §23.11), which instantiates into
|
|
/// **another** scope.
|
|
///
|
|
/// Three halves, each resolved where the LRM says it lives:
|
|
///
|
|
/// * the **target**, when it is a lone name, is a module or interface —
|
|
/// `bind prim_fifo_sync …` names the declaration a `go to definition`
|
|
/// should jump to. A hierarchical *instance* path (`top.u_dut.u_core`,
|
|
/// `top.gen[0].u_core`) names a place in an elaborated hierarchy that no
|
|
/// single file knows, so it records nothing at all: the alternative is a
|
|
/// guess, and a guess here is a wrong answer.
|
|
/// * the **instantiated module**, exactly as before, so a bind still makes
|
|
/// the bound module a dependency of this file.
|
|
/// * each connection: the `.port` half against the module being bound in
|
|
/// (an ordinary [`Target::Port`], the same one a plain instantiation
|
|
/// uses), and the expression half in the target's scope
|
|
/// ([`Target::BindScope`]).
|
|
///
|
|
/// The instance name is still **not** declared here: it belongs to the
|
|
/// target scope, and declaring it in this one would put a name in a scope
|
|
/// that does not have it.
|
|
fn bind_bind(&mut self, b: &'a BindDirective, scope: ScopeId) {
|
|
let head = match &b.target[..] {
|
|
[only] if only.index.is_none() => Some(only),
|
|
_ => None,
|
|
};
|
|
if let Some(h) = head {
|
|
let ns = self.exact_or_empty(h.span, &h.name);
|
|
self.use_name(scope, &h.name, ns, Target::Module);
|
|
}
|
|
let i = &b.inst;
|
|
let ns = self.exact_or_empty(i.module_span, &i.module);
|
|
self.use_name(scope, &i.module, ns, Target::Module);
|
|
let previous = std::mem::replace(&mut self.bind_target, head.map(|h| h.name.clone()));
|
|
for c in i.params.iter().flatten() {
|
|
self.bind_connection(c, &i.module, scope);
|
|
}
|
|
for inst in &i.instances {
|
|
match &inst.conns {
|
|
Connections::Named(conns) => {
|
|
for c in conns {
|
|
self.bind_connection(c, &i.module, scope);
|
|
}
|
|
}
|
|
Connections::Ordered(conns) => {
|
|
for c in conns {
|
|
self.bind_opt_expr(c.expr.as_ref(), scope);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
self.bind_target = previous;
|
|
}
|
|
|
|
fn bind_connection(&mut self, c: &Connection, module: &str, scope: ScopeId) {
|
|
if c.name != "*" {
|
|
self.use_name(scope, &c.name, c.name_span, Target::Port(module.to_string()));
|
|
}
|
|
self.bind_opt_expr(c.expr.as_ref(), scope);
|
|
}
|
|
|
|
fn bind_gen_if(&mut self, g: &'a GenIf, scope: ScopeId) {
|
|
self.bind_expr(&g.cond, scope);
|
|
self.bind_gen_block(&g.then, scope);
|
|
match g.els.as_deref() {
|
|
Some(GenElse::If(_, inner)) => self.bind_gen_if(inner, scope),
|
|
Some(GenElse::Block(b)) => self.bind_gen_block(b, scope),
|
|
None => {}
|
|
}
|
|
}
|
|
|
|
fn bind_gen_for(&mut self, g: &'a GenFor, span: TextRange, scope: ScopeId) {
|
|
// The loop header opens a scope of its own so that `for (genvar i...)`
|
|
// does not leak `i` into the enclosing module.
|
|
let header = self.new_scope(ScopeKind::Generate, scope, span);
|
|
match (g.init_genvar, &g.init.lhs.kind) {
|
|
// `for (genvar i = 0; ...)` declares; `for (i = 0; ...)` uses a
|
|
// `genvar i;` written outside the loop.
|
|
(true, ExprKind::Ident(name)) => {
|
|
self.declare(header, name, SymbolKind::Genvar, g.init.lhs.span, g.init.lhs.span);
|
|
}
|
|
_ => self.bind_expr(&g.init.lhs, header),
|
|
}
|
|
self.bind_expr(&g.init.rhs, header);
|
|
self.bind_expr(&g.cond, header);
|
|
match &g.step {
|
|
ForStep::Assign(a) => {
|
|
self.bind_expr(&a.lhs, header);
|
|
self.bind_expr(&a.rhs, header);
|
|
}
|
|
ForStep::Expr(e) => self.bind_expr(e, header),
|
|
}
|
|
self.bind_gen_block(&g.body, header);
|
|
}
|
|
|
|
fn bind_gen_block(&mut self, b: &'a GenBlock, scope: ScopeId) {
|
|
let inner = match (&b.label, b.label_span) {
|
|
(Some(label), Some(ls)) => self.declare_with_scope(
|
|
scope,
|
|
label,
|
|
SymbolKind::Block,
|
|
ls,
|
|
b.span,
|
|
ScopeKind::Generate,
|
|
),
|
|
_ => self.new_scope(ScopeKind::Generate, scope, b.span),
|
|
};
|
|
self.bind_items(&b.items, inner);
|
|
self.use_end_label(scope, inner, b.end_label.as_ref(), b.end_label_span);
|
|
}
|
|
|
|
fn bind_function(&mut self, f: &'a FunctionDecl, span: TextRange, scope: ScopeId) {
|
|
if !f.scope.is_empty() {
|
|
// An out-of-body definition binds into its class's scope, which
|
|
// may not exist yet — deferred until the whole file is bound.
|
|
self.deferred.push(Deferred::Function(f, span, scope));
|
|
return;
|
|
}
|
|
if let Some(ret) = &f.ret {
|
|
self.bind_data_type(ret, TextRange::new(span.start, f.name_span.start), scope);
|
|
}
|
|
let inner = self.declare_with_scope(
|
|
scope,
|
|
&f.name,
|
|
SymbolKind::Function,
|
|
f.name_span,
|
|
span,
|
|
ScopeKind::Function,
|
|
);
|
|
self.bind_tf_args(&f.args, inner);
|
|
self.bind_stmts(&f.stmts, inner);
|
|
self.use_end_label(scope, inner, f.end_label.as_ref(), f.end_label_span);
|
|
}
|
|
|
|
fn bind_task(&mut self, t: &'a TaskDecl, span: TextRange, scope: ScopeId) {
|
|
if !t.scope.is_empty() {
|
|
self.deferred.push(Deferred::Task(t, span, scope));
|
|
return;
|
|
}
|
|
let inner = self.declare_with_scope(
|
|
scope,
|
|
&t.name,
|
|
SymbolKind::Task,
|
|
t.name_span,
|
|
span,
|
|
ScopeKind::Task,
|
|
);
|
|
self.bind_tf_args(&t.args, inner);
|
|
self.bind_stmts(&t.stmts, inner);
|
|
self.use_end_label(scope, inner, t.end_label.as_ref(), t.end_label_span);
|
|
}
|
|
|
|
fn bind_tf_args(&mut self, args: &'a [TfArg], scope: ScopeId) {
|
|
for a in args {
|
|
self.bind_data_type(&a.ty, TextRange::new(a.span.start, a.name_span.start), scope);
|
|
let ty = a.unpacked.is_empty().then_some(&a.ty);
|
|
self.declare_typed(scope, &a.name, SymbolKind::TfArg, a.name_span, a.span, ty);
|
|
self.bind_dims(&a.unpacked, scope);
|
|
self.bind_opt_expr(a.default.as_ref(), scope);
|
|
}
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// Interfaces
|
|
// ------------------------------------------------------------------
|
|
|
|
/// One `modport mp (input a, output b)` of a `modport` declaration.
|
|
///
|
|
/// The name is a **declaration**: it is what an interface port's
|
|
/// `bus_if.mp` type names, what a connection's `b_if.mp` names, and what a
|
|
/// hover on either should show. Every name in the port list is a *use* of
|
|
/// something the interface itself declares — a signal for
|
|
/// `input`/`output`/`inout`/`ref`, a method for `import`/`export`, the
|
|
/// clocking block for `clocking cb` — and all of them are one lookup in
|
|
/// this very scope ([`Target::InScope`]), because IEEE 1800 §25.5 says a
|
|
/// modport port names a member of the interface and nothing else.
|
|
fn bind_modport(&mut self, d: &ModportDecl, scope: ScopeId) {
|
|
self.declare(scope, &d.name, SymbolKind::Modport, d.name_span, d.span);
|
|
for port in &d.ports {
|
|
for name in &port.names {
|
|
self.use_name(scope, &name.text, name.span, Target::InScope);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A `clocking cb @(posedge clk); … endclocking` block.
|
|
///
|
|
/// A **named** block with an event declares a symbol whose declared type is
|
|
/// the enclosing unit's own name. A clocking signal is an alias of the
|
|
/// same-named signal in that unit (IEEE 1800 §14.4), so pointing the
|
|
/// block's "type" back at the interface makes `vif.cb.data` and `cb.data`
|
|
/// answer with `data`'s own declaration — the honest answer — through
|
|
/// exactly the member chase round 2 already built, with no new machinery.
|
|
///
|
|
/// `default clocking cb;` selects a block declared elsewhere in the same
|
|
/// scope and is therefore a *use*; an anonymous `default clocking @(...)`
|
|
/// names nothing and declares nothing.
|
|
fn bind_clocking(&mut self, c: &ClockingDecl, span: TextRange, scope: ScopeId) {
|
|
match (&c.name, &c.event) {
|
|
(Some(name), Some(_)) => {
|
|
let ty = self.enclosing_unit_name(scope);
|
|
let id = self.declare(scope, &name.text, SymbolKind::Clocking, name.span, span);
|
|
if let Some(id) = id {
|
|
self.symbols[id.0 as usize].type_name = ty;
|
|
}
|
|
}
|
|
(Some(name), None) => self.use_name(scope, &name.text, name.span, Target::InScope),
|
|
(None, _) => {}
|
|
}
|
|
if let Some(event) = &c.event {
|
|
self.bind_sens_list(event, scope);
|
|
}
|
|
for item in &c.items {
|
|
let ClockingItemKind::Signals { signals, .. } = &item.kind else {
|
|
continue;
|
|
};
|
|
for signal in signals {
|
|
match &signal.value {
|
|
// `output tms = _tms_internal;` binds the clocking name to
|
|
// something *else*, so the name on the left is the block's
|
|
// own and names no signal of the enclosing scope
|
|
// (IEEE 1800 §14.4). Only the value is a use.
|
|
Some(value) => self.bind_expr(value, scope),
|
|
None => {
|
|
self.use_name(scope, &signal.name.text, signal.name.span, Target::InScope)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The name of the nearest enclosing `module` or `interface` — what a
|
|
/// clocking block records as its declared type. `None` at file scope, and
|
|
/// inside a class, where a clocking block cannot stand anyway.
|
|
fn enclosing_unit_name(&self, scope: ScopeId) -> Option<String> {
|
|
let mut current = Some(scope);
|
|
while let Some(id) = current {
|
|
let s = self.scopes.get(id.0 as usize)?;
|
|
if let Some(owner) = s.owner {
|
|
let symbol = self.symbols.get(owner.0 as usize)?;
|
|
if symbol.kind.is_instantiable() {
|
|
return Some(symbol.name.clone());
|
|
}
|
|
}
|
|
current = s.parent;
|
|
}
|
|
None
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// Classes
|
|
// ------------------------------------------------------------------
|
|
|
|
fn bind_class(&mut self, c: &'a ClassDecl, span: TextRange, scope: ScopeId) {
|
|
let inner = self.declare_with_scope(
|
|
scope,
|
|
&c.name,
|
|
SymbolKind::Class,
|
|
c.name_span,
|
|
span,
|
|
ScopeKind::Class,
|
|
);
|
|
// Header `#( ... )` parameters bind like a module's.
|
|
self.bind_param_ports(c.param_ports.iter().flatten(), inner);
|
|
if let Some(ext) = &c.extends {
|
|
self.scopes[inner.0 as usize].extends = Some(ext.name.clone());
|
|
// The base name resolves from the *enclosing* scope — a class is
|
|
// not visible to its own header.
|
|
self.use_scoped_unit(&ext.name, ext.name_span, scope);
|
|
// Overrides: `.W` is the base's parameter; the value may name the
|
|
// class's own parameters (`extends base #(.W(W))`), so it binds
|
|
// in the class scope.
|
|
let base = last_segment(&ext.name);
|
|
for conn in ext.params.iter().flatten() {
|
|
if conn.name != "*" {
|
|
self.use_name(
|
|
inner,
|
|
&conn.name,
|
|
conn.name_span,
|
|
Target::UnitMember(base.to_string()),
|
|
);
|
|
}
|
|
self.bind_opt_expr(conn.expr.as_ref(), inner);
|
|
}
|
|
}
|
|
for n in &c.implements {
|
|
self.use_scoped_unit(&n.text, n.span, scope);
|
|
}
|
|
// The body is an ordinary item list; everything the item walk already
|
|
// does — declarations, typedefs, localparams, imports, methods,
|
|
// nested classes — works here unchanged. Verbatim members
|
|
// (constraints, covergroups) stay opaque through the item arm.
|
|
self.bind_items(&c.items, inner);
|
|
self.use_end_label(scope, inner, c.end_label.as_ref(), c.end_label_span);
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// Constraints
|
|
// ------------------------------------------------------------------
|
|
|
|
/// A `constraint c { ... }` declaration, its `constraint cls::c { ... }`
|
|
/// out-of-block form, and the `extern constraint c;` prototype.
|
|
///
|
|
/// The out-of-block form is deferred exactly as an out-of-body method is,
|
|
/// so `cls` can be looked up wherever it is written — or stood in for
|
|
/// when it lives in another file.
|
|
fn bind_constraint(&mut self, c: &'a ConstraintDecl, span: TextRange, scope: ScopeId) {
|
|
if !c.scope.is_empty() {
|
|
self.deferred.push(Deferred::Constraint(c, span, scope));
|
|
return;
|
|
}
|
|
self.bind_constraint_in(c, span, scope);
|
|
}
|
|
|
|
/// Declare the constraint in `home` and bind its body in a scope hanging
|
|
/// off it.
|
|
///
|
|
/// The body scope's parent is the class scope, which is the whole point:
|
|
/// every bare name inside a constraint is a property of the class with
|
|
/// `this.` implicit (IEEE 1800 §18.5), so ordinary lexical lookup gives
|
|
/// the right answer with no special target. A prototype has no body and
|
|
/// opens no scope.
|
|
fn bind_constraint_in(&mut self, c: &'a ConstraintDecl, span: TextRange, home: ScopeId) {
|
|
let Some(set) = &c.body else {
|
|
self.declare(home, &c.name.text, SymbolKind::Constraint, c.name.span, span);
|
|
return;
|
|
};
|
|
let inner = self.declare_with_scope(
|
|
home,
|
|
&c.name.text,
|
|
SymbolKind::Constraint,
|
|
c.name.span,
|
|
span,
|
|
ScopeKind::Constraint,
|
|
);
|
|
// A definition whose `extern` prototype already bound the name gets no
|
|
// symbol of its own; point the body scope at the prototype so the
|
|
// constraint's `foreach` variables do not spill into the class
|
|
// outline — the rule out-of-body methods follow.
|
|
if self.scopes[inner.0 as usize].owner.is_none() {
|
|
self.scopes[inner.0 as usize].owner =
|
|
self.scopes[home.0 as usize].lookup_local(&c.name.text);
|
|
}
|
|
self.bind_constraint_set(set, inner);
|
|
}
|
|
|
|
fn bind_constraint_set(&mut self, set: &ConstraintSet, scope: ScopeId) {
|
|
for item in &set.items {
|
|
self.bind_constraint_item(item, scope);
|
|
}
|
|
}
|
|
|
|
fn bind_constraint_item(&mut self, item: &ConstraintItem, scope: ScopeId) {
|
|
match &item.kind {
|
|
ConstraintItemKind::Expr { expr, .. }
|
|
| ConstraintItemKind::DisableSoft { expr, .. } => self.bind_expr(expr, scope),
|
|
ConstraintItemKind::Implication { cond, set, .. } => {
|
|
self.bind_expr(cond, scope);
|
|
self.bind_constraint_set(set, scope);
|
|
}
|
|
ConstraintItemKind::If { cond, then, els, .. } => {
|
|
self.bind_expr(cond, scope);
|
|
self.bind_constraint_set(then, scope);
|
|
if let Some(els) = els {
|
|
self.bind_constraint_set(els, scope);
|
|
}
|
|
}
|
|
// The loop variables are the constraint's own, so they get a
|
|
// scope of their own — the same shape a `foreach` statement uses,
|
|
// and for the same reason: `i` must not leak to the item beside
|
|
// this one. A skipped dimension arrives as an empty name, which
|
|
// `declare` already ignores.
|
|
ConstraintItemKind::Foreach { array, vars, set, .. } => {
|
|
let inner = self.new_scope(ScopeKind::Block, scope, item.span);
|
|
self.bind_expr(array, scope);
|
|
for v in vars {
|
|
let span = v.name.span;
|
|
self.declare(inner, &v.name.text, SymbolKind::Var, span, span);
|
|
}
|
|
self.bind_constraint_set(set, inner);
|
|
}
|
|
ConstraintItemKind::SolveBefore { solve, before, .. } => {
|
|
for e in solve.iter().chain(before.iter()) {
|
|
self.bind_expr(&e.expr, scope);
|
|
}
|
|
}
|
|
ConstraintItemKind::Unique { items, .. } => {
|
|
for e in items {
|
|
self.bind_expr(&e.expr, scope);
|
|
}
|
|
}
|
|
// A directive is not evaluated, and a null item names nothing.
|
|
ConstraintItemKind::Directive(_) | ConstraintItemKind::Empty => {}
|
|
}
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// Covergroups
|
|
// ------------------------------------------------------------------
|
|
|
|
/// A `covergroup g (args) with function sample (args); ... endgroup`
|
|
/// (IEEE 1800 §19.3).
|
|
///
|
|
/// The covergroup opens a scope of its own whose parent is where it is
|
|
/// written, so every sampled expression reaches an enclosing class's
|
|
/// properties with `this.` implicit — the rule a constraint body follows,
|
|
/// for the same reason. What that scope *declares* is the covergroup's
|
|
/// formal arguments, its `sample` method and that method's arguments, and
|
|
/// one symbol per labelled coverpoint and cross: those are the names
|
|
/// `cg.cp` and a `cross a, b` reach.
|
|
fn bind_covergroup(&mut self, c: &'a CovergroupDecl, span: TextRange, scope: ScopeId) {
|
|
let inner = self.declare_with_scope(
|
|
scope,
|
|
&c.name.text,
|
|
SymbolKind::Covergroup,
|
|
c.name.span,
|
|
span,
|
|
ScopeKind::Covergroup,
|
|
);
|
|
if let Some(args) = &c.args {
|
|
self.bind_tf_args(args, inner);
|
|
}
|
|
match &c.event {
|
|
Some(CoverageEvent::Sample(s)) => {
|
|
// `sample` is a real method of the covergroup — `cg.sample(x)`
|
|
// is how every DV testbench calls it.
|
|
self.declare(inner, &s.name.text, SymbolKind::Function, s.name.span, s.name.span);
|
|
self.bind_tf_args(&s.args, inner);
|
|
}
|
|
// A clocking event names signals of the *enclosing* scope, not of
|
|
// the covergroup: it is written in the header, before anything
|
|
// inside exists.
|
|
Some(CoverageEvent::Clocking { sens, .. }) => self.bind_sens_list(sens, scope),
|
|
None => {}
|
|
}
|
|
for item in &c.items {
|
|
self.bind_cover_item(item, inner);
|
|
}
|
|
self.use_end_label(
|
|
scope,
|
|
inner,
|
|
c.end_label.as_ref().map(|l| &l.text),
|
|
c.end_label.as_ref().map(|l| l.span),
|
|
);
|
|
}
|
|
|
|
fn bind_cover_item(&mut self, item: &'a CoverItem, scope: ScopeId) {
|
|
match &item.kind {
|
|
CoverItemKind::Coverpoint(cp) => {
|
|
self.declare_cover_label(
|
|
cp.label.as_ref(),
|
|
SymbolKind::Coverpoint,
|
|
item.span,
|
|
scope,
|
|
);
|
|
self.bind_expr(&cp.expr, scope);
|
|
self.bind_cover_iff(cp.iff.as_deref(), scope);
|
|
self.bind_cover_body(cp.body.as_ref(), scope);
|
|
}
|
|
CoverItemKind::Cross(cr) => {
|
|
self.declare_cover_label(cr.label.as_ref(), SymbolKind::Cross, item.span, scope);
|
|
for e in &cr.items {
|
|
self.bind_expr(&e.expr, scope);
|
|
}
|
|
self.bind_cover_iff(cr.iff.as_deref(), scope);
|
|
self.bind_cover_body(cr.body.as_ref(), scope);
|
|
}
|
|
// `option.per_instance` names a built-in of the covergroup, not
|
|
// anything this crate models, so neither half is recorded — the
|
|
// value is an ordinary expression and is.
|
|
CoverItemKind::Option(o) => self.bind_expr(&o.value, scope),
|
|
CoverItemKind::Directive(_) | CoverItemKind::Empty => {}
|
|
}
|
|
}
|
|
|
|
fn declare_cover_label(
|
|
&mut self,
|
|
label: Option<&CoverLabel>,
|
|
kind: SymbolKind,
|
|
span: TextRange,
|
|
scope: ScopeId,
|
|
) {
|
|
if let Some(l) = label {
|
|
self.declare(scope, &l.name.text, kind, l.name.span, span);
|
|
}
|
|
}
|
|
|
|
fn bind_cover_iff(&mut self, iff: Option<&'a CoverIff>, scope: ScopeId) {
|
|
if let Some(i) = iff {
|
|
self.bind_expr(&i.expr, scope);
|
|
}
|
|
}
|
|
|
|
fn bind_cover_body(&mut self, body: Option<&'a CoverBody>, scope: ScopeId) {
|
|
let Some(body) = body else { return };
|
|
for item in &body.items {
|
|
match &item.kind {
|
|
BinItemKind::Bin(b) => self.bind_bin_decl(b, scope),
|
|
BinItemKind::Option(o) => self.bind_expr(&o.value, scope),
|
|
BinItemKind::Directive(_) | BinItemKind::Empty => {}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn bind_bin_decl(&mut self, b: &'a BinDecl, scope: ScopeId) {
|
|
if let Some(a) = &b.array {
|
|
self.bind_opt_expr(a.size.as_ref(), scope);
|
|
}
|
|
self.bind_bin_spec(&b.spec, scope);
|
|
if let Some(w) = &b.with_expr {
|
|
// `item` is the implicit iteration variable of a `with` filter
|
|
// (IEEE 1800 §19.5.1.1) — the coverpoint's own sampled value, not
|
|
// anything the enclosing class declares. It gets a scope of its
|
|
// own so that a class property that happens to be called `item`
|
|
// is not silently taken to be it, which would make renaming that
|
|
// property rewrite a filter that never named it. `item` is not
|
|
// *written* as a declaration anywhere, so the first use of it
|
|
// inside the filter is what the declaration points at — every
|
|
// name this crate records must be locatable in the source, and an
|
|
// invented span is not.
|
|
let inner = self.new_scope(ScopeKind::Block, scope, w.expr.span);
|
|
let first = self.pending.len();
|
|
self.bind_expr(&w.expr, inner);
|
|
if let Some(at) =
|
|
self.pending[first..].iter().find(|p| p.name == "item").map(|p| p.name_span)
|
|
{
|
|
self.declare(inner, "item", SymbolKind::Var, at, w.expr.span);
|
|
}
|
|
}
|
|
self.bind_cover_iff(b.iff.as_deref(), scope);
|
|
}
|
|
|
|
fn bind_bin_spec(&mut self, spec: &'a BinSpec, scope: ScopeId) {
|
|
match spec {
|
|
BinSpec::Values(items) => {
|
|
for e in items {
|
|
self.bind_expr(&e.expr, scope);
|
|
}
|
|
}
|
|
BinSpec::Transitions(sets) => {
|
|
for set in sets {
|
|
for r in &set.ranges {
|
|
for e in &r.items {
|
|
self.bind_expr(&e.expr, scope);
|
|
}
|
|
if let Some(rep) = &r.repeat {
|
|
self.bind_expr(&rep.lo, scope);
|
|
self.bind_opt_expr(rep.hi.as_ref(), scope);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
BinSpec::Default | BinSpec::DefaultSequence { .. } => {}
|
|
BinSpec::Select(s) => self.bind_select_expr(s, scope),
|
|
}
|
|
}
|
|
|
|
fn bind_select_expr(&mut self, s: &'a SelectExpr, scope: ScopeId) {
|
|
match s {
|
|
SelectExpr::Binsof(b) => {
|
|
self.bind_expr(&b.name, scope);
|
|
for e in b.intersect.iter().flatten() {
|
|
self.bind_expr(&e.expr, scope);
|
|
}
|
|
}
|
|
SelectExpr::Not { expr, .. } | SelectExpr::Paren { expr, .. } => {
|
|
self.bind_select_expr(expr, scope)
|
|
}
|
|
SelectExpr::Binary { lhs, rhs, .. } => {
|
|
self.bind_select_expr(lhs, scope);
|
|
self.bind_select_expr(rhs, scope);
|
|
}
|
|
SelectExpr::Expr(e) => self.bind_expr(e, scope),
|
|
}
|
|
}
|
|
|
|
/// `base with { ... }` — a `randomize` call's inline constraint block
|
|
/// (IEEE 1800 §18.7).
|
|
///
|
|
/// The block is bound in the **caller's** scope, because that is where it
|
|
/// is written and what its `local::`-free names may also reach. What
|
|
/// makes it useful is the head: in `req.randomize() with { addr == 0; }`
|
|
/// the `addr` is `req`'s property, not the writing scope's, so while the
|
|
/// block is bound every bare name is recorded as a member of `req` —
|
|
/// through the very `Target::MemberOf` chase `head.member` already uses,
|
|
/// which then follows `req`'s declared type into its class, across files
|
|
/// included.
|
|
///
|
|
/// A call with no such head — a bare `randomize() with` (implicitly
|
|
/// `this`, already in scope) or `std::randomize(x) with` (whose names are
|
|
/// the caller's own) — keeps ordinary lexical binding.
|
|
fn bind_with_constraint(&mut self, base: &Expr, block: &ConstraintBody, scope: ScopeId) {
|
|
self.bind_expr(base, scope);
|
|
let ConstraintBody::Items(set) = block else {
|
|
// A body the grammar did not recognise stays opaque, exactly as
|
|
// every `with` block was before this round.
|
|
return;
|
|
};
|
|
let outer = std::mem::replace(&mut self.with_head, randomize_head(base));
|
|
self.bind_constraint_set(set, scope);
|
|
self.with_head = outer;
|
|
}
|
|
|
|
/// Record a unit name that may be scope-qualified: `base`, `pkg::base`.
|
|
/// A qualified spelling records head and tail (middle segments of a
|
|
/// longer chain are not recorded; the corpus has none). Both spans are
|
|
/// derived from the glued text, so each is verified before use and the
|
|
/// reference is skipped when the source spelled spaces around the `::`.
|
|
fn use_scoped_unit(&mut self, name: &str, span: TextRange, scope: ScopeId) {
|
|
match name.rsplit_once("::") {
|
|
Some((head_path, tail)) => {
|
|
let head = head_path.split("::").next().unwrap_or(head_path);
|
|
let head_span = self.exact_or_empty(
|
|
TextRange::new(span.start, span.start.saturating_add(head.len() as u32)),
|
|
head,
|
|
);
|
|
if !head_span.is_empty() {
|
|
self.use_name(scope, head, head_span, Target::Unit);
|
|
}
|
|
let tail_span = self.exact_or_empty(
|
|
TextRange::new(span.end.saturating_sub(tail.len() as u32), span.end),
|
|
tail,
|
|
);
|
|
let owner = head_path.rsplit("::").next().unwrap_or(head_path);
|
|
if !tail_span.is_empty() {
|
|
self.use_name(scope, tail, tail_span, Target::UnitMember(owner.to_string()));
|
|
}
|
|
}
|
|
None => self.use_name(scope, name, span, Target::Unit),
|
|
}
|
|
}
|
|
|
|
/// Bind the out-of-body method definitions collected during the walk.
|
|
/// Runs after everything else is bound, so the class scope exists no
|
|
/// matter where the class is written.
|
|
pub(crate) fn bind_deferred(&mut self) {
|
|
let deferred = std::mem::take(&mut self.deferred);
|
|
for d in deferred {
|
|
match d {
|
|
Deferred::Function(f, span, scope) => {
|
|
let path = scope_path(&f.scope);
|
|
let path = path.as_str();
|
|
self.bind_out_of_body(
|
|
path,
|
|
&f.name,
|
|
f.name_span,
|
|
span,
|
|
scope,
|
|
SymbolKind::Function,
|
|
ScopeKind::Function,
|
|
f.ret.as_ref(),
|
|
&f.args,
|
|
&f.stmts,
|
|
);
|
|
}
|
|
Deferred::Constraint(c, span, scope) => {
|
|
let path = scope_path(&c.scope);
|
|
let path = path.as_str();
|
|
let home = self.out_of_body_home(path, c.name.span, span, scope);
|
|
self.bind_constraint_in(c, span, home);
|
|
}
|
|
Deferred::Task(t, span, scope) => {
|
|
let path = scope_path(&t.scope);
|
|
let path = path.as_str();
|
|
self.bind_out_of_body(
|
|
path,
|
|
&t.name,
|
|
t.name_span,
|
|
span,
|
|
scope,
|
|
SymbolKind::Task,
|
|
ScopeKind::Task,
|
|
None,
|
|
&t.args,
|
|
&t.stmts,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The scope an out-of-body definition's name belongs in, and the
|
|
/// reference to the class path that names it.
|
|
///
|
|
/// The written class path is a reference of its own — navigation from
|
|
/// `cls::method` to the class. Its span is derived (the parser glues the
|
|
/// path), so it is verified and skipped when inexact.
|
|
///
|
|
/// The home is the class scope when the class is in this file, else a
|
|
/// scope standing in for that class — empty, so every lookup falls
|
|
/// straight through it to the writing scope, but marked with the class
|
|
/// path as its `extends`, so a name nothing else binds is an
|
|
/// `InheritedCandidate` against the class rather than a `NotFound`. That
|
|
/// is what lets `task c2::update(...)` — and `constraint c2::c { ... }` —
|
|
/// see `c2`'s properties when `c2` is declared in another file: the
|
|
/// workspace index finishes the candidate through its member table.
|
|
fn out_of_body_home(
|
|
&mut self,
|
|
path: &str,
|
|
name_span: TextRange,
|
|
span: TextRange,
|
|
decl_scope: ScopeId,
|
|
) -> ScopeId {
|
|
let cls = last_segment(path);
|
|
let cls_span = TextRange::new(
|
|
name_span.start.saturating_sub(2).saturating_sub(cls.len() as u32),
|
|
name_span.start.saturating_sub(2),
|
|
);
|
|
let cls_span = self.exact_or_empty(cls_span, cls);
|
|
if !cls_span.is_empty() {
|
|
self.use_name(decl_scope, cls, cls_span, Target::Unit);
|
|
}
|
|
match self.find_unit_scope(decl_scope, path) {
|
|
Some(scope) => scope,
|
|
None if path.is_empty() => decl_scope,
|
|
None => {
|
|
let stand_in = self.new_scope(ScopeKind::Class, decl_scope, span);
|
|
self.scopes[stand_in.0 as usize].extends = Some(path.to_string());
|
|
stand_in
|
|
}
|
|
}
|
|
}
|
|
|
|
/// One `function void cls::method(...)` definition: the method symbol
|
|
/// binds into `cls`'s scope (an `extern` prototype already there makes
|
|
/// the definition-site name a reference to it — the same
|
|
/// redeclaration-becomes-reference rule non-ANSI ports use), and the body
|
|
/// scope hangs under `cls`'s scope so properties resolve.
|
|
// The allow: one call shape, two callers; a parameter struct would
|
|
// outweigh the function.
|
|
#[allow(clippy::too_many_arguments)]
|
|
fn bind_out_of_body(
|
|
&mut self,
|
|
path: &str,
|
|
name: &str,
|
|
name_span: TextRange,
|
|
span: TextRange,
|
|
decl_scope: ScopeId,
|
|
kind: SymbolKind,
|
|
scope_kind: ScopeKind,
|
|
ret: Option<&DataType>,
|
|
args: &'a [TfArg],
|
|
stmts: &'a [Stmt],
|
|
) {
|
|
let home = self.out_of_body_home(path, name_span, span, decl_scope);
|
|
let sym = self.declare(home, name, kind, name_span, span);
|
|
let body = self.new_scope(scope_kind, home, span);
|
|
match sym {
|
|
Some(sym) => {
|
|
self.symbols[sym.0 as usize].child_scope = Some(body);
|
|
self.scopes[body.0 as usize].owner = Some(sym);
|
|
}
|
|
// The prototype owns the name; mark the body scope as owned by it
|
|
// (without touching the prototype's own scope) so method locals
|
|
// do not spill into the class outline.
|
|
None => {
|
|
self.scopes[body.0 as usize].owner =
|
|
self.scopes[home.0 as usize].lookup_local(name);
|
|
}
|
|
}
|
|
// Return type, arguments and body all resolve from inside the class,
|
|
// as the LRM says an out-of-body declaration does.
|
|
if let Some(ret) = ret {
|
|
self.bind_data_type(ret, TextRange::new(span.start, name_span.start), body);
|
|
}
|
|
self.bind_tf_args(args, body);
|
|
self.bind_stmts(stmts, body);
|
|
}
|
|
|
|
/// The scope of the unit `path` names, resolved lexically from `from` and
|
|
/// descended segment by segment (`pkg::cls`).
|
|
fn find_unit_scope(&self, from: ScopeId, path: &str) -> Option<ScopeId> {
|
|
let mut segments = path.split("::");
|
|
let mut scope = self.lookup_unit_scope(from, segments.next()?)?;
|
|
for seg in segments {
|
|
let sym = self.scopes[scope.0 as usize].lookup_local(seg)?;
|
|
let s = self.symbols.get(sym.0 as usize)?;
|
|
if !matches!(s.kind, SymbolKind::Class | SymbolKind::Package) {
|
|
return None;
|
|
}
|
|
scope = s.child_scope?;
|
|
}
|
|
Some(scope)
|
|
}
|
|
|
|
/// The scope of the class or package `name`, found on the lexical chain
|
|
/// from `from`; non-unit shadowers are skipped.
|
|
fn lookup_unit_scope(&self, from: ScopeId, name: &str) -> Option<ScopeId> {
|
|
let mut current = Some(from);
|
|
while let Some(id) = current {
|
|
let scope = self.scopes.get(id.0 as usize)?;
|
|
if let Some(sym) = scope.lookup_local(name) {
|
|
if let Some(s) = self.symbols.get(sym.0 as usize) {
|
|
if matches!(s.kind, SymbolKind::Class | SymbolKind::Package) {
|
|
if let Some(child) = s.child_scope {
|
|
return Some(child);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
current = scope.parent;
|
|
}
|
|
None
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// Statements
|
|
// ------------------------------------------------------------------
|
|
|
|
fn bind_stmts(&mut self, stmts: &'a [Stmt], scope: ScopeId) {
|
|
for s in stmts {
|
|
self.bind_stmt(s, scope);
|
|
}
|
|
}
|
|
|
|
fn bind_stmt(&mut self, stmt: &'a Stmt, scope: ScopeId) {
|
|
match &stmt.kind {
|
|
StmtKind::Block { label, label_span, stmts, end_label, end_label_span, .. }
|
|
| StmtKind::ForkJoin { label, label_span, stmts, end_label, end_label_span, .. } => {
|
|
let inner = match (label, *label_span) {
|
|
(Some(label), Some(ls)) => self.declare_with_scope(
|
|
scope,
|
|
label,
|
|
SymbolKind::Block,
|
|
ls,
|
|
stmt.span,
|
|
ScopeKind::Block,
|
|
),
|
|
_ => self.new_scope(ScopeKind::Block, scope, stmt.span),
|
|
};
|
|
self.bind_stmts(stmts, inner);
|
|
self.use_end_label(scope, inner, end_label.as_ref(), *end_label_span);
|
|
}
|
|
StmtKind::If { cond, then, els, .. } => {
|
|
self.bind_expr(cond, scope);
|
|
self.bind_stmt(then, scope);
|
|
if let Some(e) = els {
|
|
self.bind_stmt(e, scope);
|
|
}
|
|
}
|
|
StmtKind::Case { expr, items, .. } => {
|
|
self.bind_expr(expr, scope);
|
|
self.bind_case_items(items, scope);
|
|
}
|
|
StmtKind::RandCase { items, .. } => self.bind_case_items(items, scope),
|
|
// A statement label names the statement the way a block name
|
|
// names its block (IEEE 1800 §9.3.5).
|
|
StmtKind::Labeled { name, name_span, stmt: inner } => {
|
|
self.declare(scope, name, SymbolKind::Block, *name_span, stmt.span);
|
|
self.bind_stmt(inner, scope);
|
|
}
|
|
StmtKind::Import(paths) => {
|
|
for path in paths {
|
|
self.bind_import_path(path, scope);
|
|
}
|
|
}
|
|
StmtKind::For { init, cond, step, body, .. } => {
|
|
let inner = self.new_scope(ScopeKind::Block, scope, stmt.span);
|
|
for i in init {
|
|
match &i.ty {
|
|
Some(ty) => {
|
|
if let ExprKind::Ident(name) = &i.assign.lhs.kind {
|
|
self.bind_data_type(
|
|
ty,
|
|
TextRange::new(stmt.span.start, i.assign.lhs.span.start),
|
|
scope,
|
|
);
|
|
self.declare(
|
|
inner,
|
|
name,
|
|
SymbolKind::Var,
|
|
i.assign.lhs.span,
|
|
i.assign.lhs.span,
|
|
);
|
|
}
|
|
}
|
|
None => self.bind_expr(&i.assign.lhs, inner),
|
|
}
|
|
self.bind_expr(&i.assign.rhs, inner);
|
|
}
|
|
self.bind_opt_expr(cond.as_ref(), inner);
|
|
for s in step {
|
|
match s {
|
|
ForStep::Assign(a) => {
|
|
self.bind_expr(&a.lhs, inner);
|
|
self.bind_expr(&a.rhs, inner);
|
|
}
|
|
ForStep::Expr(e) => self.bind_expr(e, inner),
|
|
}
|
|
}
|
|
self.bind_stmt(body, inner);
|
|
}
|
|
StmtKind::While { cond, body } | StmtKind::DoWhile { body, cond, .. } => {
|
|
self.bind_expr(cond, scope);
|
|
self.bind_stmt(body, scope);
|
|
}
|
|
StmtKind::Repeat { count, body } => {
|
|
self.bind_expr(count, scope);
|
|
self.bind_stmt(body, scope);
|
|
}
|
|
StmtKind::Forever { body } => self.bind_stmt(body, scope),
|
|
StmtKind::Foreach { target, vars, body, .. } => {
|
|
let inner = self.new_scope(ScopeKind::Block, scope, stmt.span);
|
|
self.bind_expr(target, scope);
|
|
// A skipped dimension (`arr[, j]`) arrives as an empty name;
|
|
// `declare` already ignores those, so it declares nothing.
|
|
for v in vars {
|
|
let span = v.name.span;
|
|
self.declare(inner, &v.name.text, SymbolKind::Var, span, span);
|
|
}
|
|
self.bind_stmt(body, inner);
|
|
}
|
|
StmtKind::Assign { lhs, timing, rhs, .. } => {
|
|
self.bind_expr(lhs, scope);
|
|
if let Some(t) = timing {
|
|
self.bind_timing(t, scope);
|
|
}
|
|
self.bind_expr(rhs, scope);
|
|
}
|
|
StmtKind::ForceRelease { lhs, rhs, .. } => {
|
|
self.bind_expr(lhs, scope);
|
|
self.bind_opt_expr(rhs.as_ref(), scope);
|
|
}
|
|
StmtKind::Timing { control, stmt } => {
|
|
self.bind_timing(control, scope);
|
|
if let Some(s) = stmt {
|
|
self.bind_stmt(s, scope);
|
|
}
|
|
}
|
|
StmtKind::Wait { cond, stmt } => {
|
|
self.bind_expr(cond, scope);
|
|
if let Some(s) = stmt {
|
|
self.bind_stmt(s, scope);
|
|
}
|
|
}
|
|
StmtKind::ExprStmt(e) | StmtKind::EventTrigger(e) => self.bind_expr(e, scope),
|
|
StmtKind::VarDecl(d) => self.bind_data_decl(d, stmt.span, scope),
|
|
// A block-scoped type binds exactly as an item-level one does; the
|
|
// scope it lands in is the enclosing block's, which is what
|
|
// IEEE 1800 §6.18 asks for.
|
|
StmtKind::Typedef(t) => self.bind_typedef(t, stmt.span, scope),
|
|
StmtKind::ParamDecl(p) => {
|
|
self.bind_declarators(&p.declarators, param_kind(&p.kw), &p.ty, stmt.span, scope)
|
|
}
|
|
StmtKind::PortDecl(p) => self.bind_port_decl(p, stmt.span, scope),
|
|
StmtKind::Return { expr: e, .. } => self.bind_opt_expr(e.as_ref(), scope),
|
|
StmtKind::Disable(name) => {
|
|
// A use, not a declaration, so name-spans deliberately gave it
|
|
// no range — the one statement still located by scanning.
|
|
let ns = name_after_kw(self.src, stmt.span, "disable", name);
|
|
self.use_name(scope, name, ns, Target::Lexical);
|
|
}
|
|
StmtKind::Assert { cond, then, els, .. } => {
|
|
self.bind_expr(cond, scope);
|
|
if let Some(s) = then {
|
|
self.bind_stmt(s, scope);
|
|
}
|
|
if let Some(s) = els {
|
|
self.bind_stmt(s, scope);
|
|
}
|
|
}
|
|
StmtKind::Break
|
|
| StmtKind::Continue
|
|
| StmtKind::WaitFork
|
|
| StmtKind::Directive(_)
|
|
| StmtKind::MacroStmt(_)
|
|
| StmtKind::Null
|
|
// Verbatim source: opaque to name resolution, exactly as the
|
|
// item-level `ItemKind::Verbatim` is.
|
|
| StmtKind::Verbatim(_)
|
|
| StmtKind::Error(_) => {}
|
|
}
|
|
}
|
|
|
|
/// The labels and bodies of a `case`/`randcase` body.
|
|
fn bind_case_items(&mut self, items: &'a [CaseItem], scope: ScopeId) {
|
|
for it in items {
|
|
for l in &it.labels {
|
|
self.bind_expr(&l.expr, scope);
|
|
}
|
|
self.bind_stmt(&it.stmt, scope);
|
|
}
|
|
}
|
|
|
|
fn bind_timing(&mut self, t: &'a TimingControl, scope: ScopeId) {
|
|
match t {
|
|
TimingControl::Delay { expr, .. } => self.bind_expr(expr, scope),
|
|
TimingControl::Event(list) => self.bind_sens_list(list, scope),
|
|
}
|
|
}
|
|
|
|
/// An `@( ... )` sensitivity list — a statement's timing control, and a
|
|
/// clocking block's event, which is the same grammar.
|
|
fn bind_sens_list(&mut self, list: &SensList, scope: ScopeId) {
|
|
match list {
|
|
SensList::List { items, .. } => {
|
|
for i in items {
|
|
self.bind_expr(&i.expr, scope);
|
|
// `@(posedge clk iff enable)` reads `enable` as ordinarily
|
|
// as it reads `clk`.
|
|
if let Some(iff) = &i.iff {
|
|
self.bind_expr(iff, scope);
|
|
}
|
|
}
|
|
}
|
|
SensList::Star { .. } => {}
|
|
// `@name;` without parens — the expression is an ordinary use.
|
|
SensList::Bare { expr, .. } => self.bind_expr(expr, scope),
|
|
}
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// Expressions
|
|
// ------------------------------------------------------------------
|
|
|
|
fn bind_opt_expr(&mut self, e: Option<&Expr>, scope: ScopeId) {
|
|
if let Some(e) = e {
|
|
self.bind_expr(e, scope);
|
|
}
|
|
}
|
|
|
|
fn bind_dims(&mut self, dims: &[Dim], scope: ScopeId) {
|
|
for d in dims {
|
|
match &d.kind {
|
|
DimKind::Range { a, b } => {
|
|
self.bind_expr(a, scope);
|
|
self.bind_opt_expr(b.as_ref(), scope);
|
|
}
|
|
// `[]` and `[*]` hold no expressions — nothing to bind.
|
|
DimKind::Dynamic | DimKind::Wildcard => {}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn bind_expr(&mut self, e: &Expr, scope: ScopeId) {
|
|
match &e.kind {
|
|
ExprKind::Ident(name) => self.use_name(scope, name, e.span, Target::Lexical),
|
|
// `Verbatim` is a region whose value depends on a `` `ifdef ``
|
|
// this crate never evaluates: opaque bytes record nothing.
|
|
ExprKind::Number(_) | ExprKind::Str(_) | ExprKind::Empty(_) | ExprKind::Verbatim(_) => {
|
|
}
|
|
ExprKind::Unary { expr, .. } | ExprKind::Postfix { expr, .. } => {
|
|
self.bind_expr(expr, scope)
|
|
}
|
|
ExprKind::Binary { lhs, rhs, .. } => {
|
|
self.bind_expr(lhs, scope);
|
|
self.bind_expr(rhs, scope);
|
|
}
|
|
ExprKind::Ternary { cond, then, els, .. } => {
|
|
self.bind_expr(cond, scope);
|
|
self.bind_expr(then, scope);
|
|
self.bind_expr(els, scope);
|
|
}
|
|
ExprKind::Call { callee, args, .. } => {
|
|
self.bind_expr(callee, scope);
|
|
for a in args {
|
|
self.bind_expr(&a.expr, scope);
|
|
}
|
|
}
|
|
// `new src` — the source handle is an ordinary reference.
|
|
ExprKind::New(src) => self.bind_expr(src, scope),
|
|
ExprKind::Index { base, index, .. } => {
|
|
self.bind_expr(base, scope);
|
|
self.bind_expr(index, scope);
|
|
}
|
|
ExprKind::Range { base, msb, lsb, .. } => {
|
|
self.bind_expr(base, scope);
|
|
self.bind_expr(msb, scope);
|
|
self.bind_expr(lsb, scope);
|
|
}
|
|
// `a.b`, `a.b.c`, `tb.dut.u_core.sig`: every link is bound —
|
|
// the head as an ordinary use, each `.name` as a tail against the
|
|
// type of what precedes it (`Target::MemberOf`) — because the
|
|
// recursion below meets each `Member` node in turn. `this.b` and
|
|
// `super.b` go to the enclosing class instead, and a spine that
|
|
// starts at one of them stops there: `this.a.b` records `a` and
|
|
// nothing more (`this` is not a name this crate binds, so there
|
|
// is no head to walk from). An indexed or called head
|
|
// (`a[i].b`, `f().b`) still resolves its own head and stops:
|
|
// chasing those needs the element type of a container, which
|
|
// this crate does not model (TODO.md §15).
|
|
ExprKind::Member { base, name, .. } => match &base.kind {
|
|
ExprKind::Ident(head) if head == "this" => {
|
|
self.use_member(scope, name, member_tail(e.span, name), Target::ThisMember)
|
|
}
|
|
ExprKind::Ident(head) if head == "super" => {
|
|
self.use_member(scope, name, member_tail(e.span, name), Target::SuperMember)
|
|
}
|
|
_ => {
|
|
self.bind_expr(base, scope);
|
|
if let Some((head, mids)) = spine(base) {
|
|
// The tail's range is derived from the whole
|
|
// expression's, so it is verified before use: a
|
|
// spelling this crate cannot place costs a reference,
|
|
// never a wrong span.
|
|
let tail = self.exact_or_empty(member_tail(e.span, name), name);
|
|
if !tail.is_empty() {
|
|
self.use_member(scope, name, tail, Target::MemberOf { head, mids });
|
|
}
|
|
}
|
|
}
|
|
},
|
|
ExprKind::Scope { base, name, .. } => self.bind_scoped(base, name, e.span, scope),
|
|
// The `#( ... )` is verbatim text this crate makes no claims
|
|
// about; the name it parameterizes is still an ordinary use.
|
|
ExprKind::Parameterized { base, .. } => self.bind_expr(base, scope),
|
|
ExprKind::Concat(items) => {
|
|
for i in items {
|
|
self.bind_expr(&i.expr, scope);
|
|
}
|
|
}
|
|
ExprKind::Inside { lhs, ranges, .. } => {
|
|
self.bind_expr(lhs, scope);
|
|
for i in ranges {
|
|
self.bind_expr(&i.expr, scope);
|
|
}
|
|
}
|
|
ExprKind::Repl { count, inner } | ExprKind::AssignPatternRepl { count, inner } => {
|
|
self.bind_expr(count, scope);
|
|
for i in inner {
|
|
self.bind_expr(&i.expr, scope);
|
|
}
|
|
}
|
|
// The slice size of a streaming concatenation is a constant
|
|
// expression or a type; both are ordinary uses.
|
|
ExprKind::Stream { slice, inner, .. } => {
|
|
if let Some(s) = slice {
|
|
self.bind_expr(s, scope);
|
|
}
|
|
for i in inner {
|
|
self.bind_expr(&i.expr, scope);
|
|
}
|
|
}
|
|
// The set is a macro invocation this crate makes no claims about.
|
|
ExprKind::InsideMacro { lhs, .. } => self.bind_expr(lhs, scope),
|
|
ExprKind::With { base, arg, .. } => {
|
|
self.bind_expr(base, scope);
|
|
self.bind_expr(arg, scope);
|
|
}
|
|
ExprKind::WithConstraint { base, block, .. } => {
|
|
self.bind_with_constraint(base, block, scope)
|
|
}
|
|
ExprKind::Dist { expr, weights, .. } => {
|
|
self.bind_expr(expr, scope);
|
|
for w in weights {
|
|
self.bind_expr(&w.value, scope);
|
|
self.bind_opt_expr(w.weight.as_ref(), scope);
|
|
}
|
|
}
|
|
ExprKind::Paren { inner, .. } => self.bind_expr(inner, scope),
|
|
// Both halves of a cast are bound: the operand as an ordinary
|
|
// expression, the casting type as a *type* — which
|
|
// `bind_data_type` has resolved since `overlay-promotion`.
|
|
ExprKind::Cast { ty, expr, .. } => {
|
|
self.bind_type_expr(ty, scope);
|
|
self.bind_expr(expr, scope);
|
|
}
|
|
ExprKind::AssignPattern(items) => {
|
|
for i in items {
|
|
self.bind_opt_expr(i.key.as_ref(), scope);
|
|
self.bind_expr(&i.value, scope);
|
|
}
|
|
}
|
|
ExprKind::ValueRange { lo, hi, .. } => {
|
|
self.bind_expr(lo, scope);
|
|
self.bind_expr(hi, scope);
|
|
}
|
|
// A named call argument: the value is an ordinary use; the name
|
|
// is the callee's formal, which this crate does not model (the
|
|
// same rule connection port names follow).
|
|
ExprKind::NamedArg { value, .. } => self.bind_expr(value, scope),
|
|
ExprKind::Type(ty) => self.bind_data_type(ty, e.span, scope),
|
|
ExprKind::MinTypMax { min, typ, max } => {
|
|
self.bind_expr(min, scope);
|
|
self.bind_expr(typ, scope);
|
|
self.bind_expr(max, scope);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// `p::x`: the head names a unit (package or class), the tail is looked
|
|
/// up inside it.
|
|
/// The **casting type** of a `ty'(expr)`, bound as a type reference.
|
|
///
|
|
/// The parser keeps a `casting_type` (IEEE 1800 §6.24.1) as an
|
|
/// expression, because that is the only shape wide enough for everything
|
|
/// the grammar spells there. Three of those shapes name a type and bind
|
|
/// as one; the rest are values, and bind as values:
|
|
///
|
|
/// - a bare name — `state_t'(0)` — is a [`Target::Type`], the same
|
|
/// binding `bind_data_type` gives a declaration's type name. A type
|
|
/// *keyword* (`int'(x)`) names nothing declared and binds to nothing,
|
|
/// which is what `bind_data_type` does with one too.
|
|
/// - `pkg::t'(x)` is a [`Target::Unit`] plus a [`Target::UnitMember`] —
|
|
/// again exactly what `bind_data_type` records for `pkg::t x;`.
|
|
/// - a size, a parenthesized constant expression (`(N)'(x)`) or a system
|
|
/// function call (`$bits(t)'(x)`) is an *expression* standing in for a
|
|
/// width. Its operands are ordinary uses and are bound as such.
|
|
fn bind_type_expr(&mut self, ty: &Expr, scope: ScopeId) {
|
|
match &ty.kind {
|
|
ExprKind::Ident(name) => {
|
|
if !is_keyword(name) {
|
|
self.use_name(scope, name, ty.span, Target::Type);
|
|
}
|
|
}
|
|
ExprKind::Scope { base, name, .. } => self.bind_scoped(base, name, ty.span, scope),
|
|
// A number is a size (`4'(x)`) and names nothing at all.
|
|
ExprKind::Number(_) => {}
|
|
_ => self.bind_expr(ty, scope),
|
|
}
|
|
}
|
|
|
|
fn bind_scoped(&mut self, base: &Expr, name: &str, span: TextRange, scope: ScopeId) {
|
|
let tail = TextRange::new(span.end.saturating_sub(name.len() as u32), span.end);
|
|
match &base.kind {
|
|
ExprKind::Ident(unit) => {
|
|
self.use_name(scope, unit, base.span, Target::Unit);
|
|
self.use_member(scope, name, tail, Target::UnitMember(unit.clone()));
|
|
}
|
|
_ => self.bind_expr(base, scope),
|
|
}
|
|
}
|
|
|
|
/// A user-defined type name is a use like any other. `window` is the
|
|
/// source range the name was written in — everything before the declared
|
|
/// name, which is where a data type always sits. A `DataType` has no span
|
|
/// of its own (vlog-semantic handshake item 2, still open), so this is
|
|
/// the one declaration surface still located by scanning.
|
|
fn bind_data_type(&mut self, ty: &DataType, window: TextRange, scope: ScopeId) {
|
|
if !ty.name.is_empty() && !is_keyword(&ty.name) {
|
|
match ty.name.split_once("::") {
|
|
Some((unit, item)) => {
|
|
let mut scanner = NameScanner::new(self.src, window);
|
|
let unit_span = scanner.find(unit).unwrap_or(TextRange::empty(window.start));
|
|
let item_span = scanner.find(item).unwrap_or(TextRange::empty(window.start));
|
|
self.use_name(scope, unit, unit_span, Target::Unit);
|
|
self.use_name(scope, item, item_span, Target::UnitMember(unit.to_string()));
|
|
}
|
|
// `bus_if.mp bus`, `virtual bus_if.mp vif` — the parser glues
|
|
// an interface port's modport onto the type name (IEEE 1800
|
|
// §25.4, §25.9). The interface is the type and the modport is
|
|
// a **member** of it, so both halves are bound: the head as
|
|
// any other type name, the tail against the interface's own
|
|
// scope (or, cross-file, as `bus_if::mp` in the workspace's
|
|
// member table). "The tail is where to bind them" —
|
|
// `docs/features/ansi-first-port/README.md`.
|
|
None => {
|
|
let (name, modport) = match ty.name.split_once('.') {
|
|
Some((iface, modport)) => (iface, Some(modport)),
|
|
None => (ty.name.as_str(), None),
|
|
};
|
|
let ns = crate::scan::name_span(self.src, window, name);
|
|
self.use_name(scope, name, ns, Target::Type);
|
|
if let Some(modport) = modport {
|
|
let tail = self.modport_tail(ns, modport);
|
|
if !tail.is_empty() {
|
|
self.use_member(
|
|
scope,
|
|
modport,
|
|
tail,
|
|
Target::TypeMember { unit: name.to_string() },
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
self.bind_dims(&ty.packed, scope);
|
|
// An anonymous aggregate written where a type name would go. Its
|
|
// *names* are declared by whoever owns the declarator (an enum's
|
|
// variants below, a struct's members in `bind_struct_members`); what
|
|
// is written here is every use inside it, recorded once for the type
|
|
// however many declarators share it.
|
|
match ty.inline() {
|
|
Some(TypeExtra::Enum(e)) => self.bind_enum_body(e, window, scope),
|
|
Some(TypeExtra::Struct(s)) => self.bind_struct_uses(s, scope),
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
/// The byte range of the `mp` in a glued `bus_if.mp` type name, given the
|
|
/// range the interface half was located at.
|
|
///
|
|
/// Derived rather than received — a `DataType` carries no spans at all —
|
|
/// so it is verified twice over: the byte after the interface half must be
|
|
/// a literal `.`, and the bytes after that must spell the modport exactly.
|
|
/// A source that wrote the two apart, or a name a macro produced, costs the
|
|
/// reference and never mislocates it.
|
|
fn modport_tail(&self, iface: TextRange, modport: &str) -> TextRange {
|
|
if iface.is_empty() || self.src.as_bytes().get(iface.end as usize) != Some(&b'.') {
|
|
return TextRange::empty(iface.end);
|
|
}
|
|
let start = iface.end.saturating_add(1);
|
|
let span = TextRange::new(start, start.saturating_add(modport.len() as u32));
|
|
self.exact_or_empty(span, modport)
|
|
}
|
|
}
|
|
|
|
/// The chain of names written before a member access's tail: the plain
|
|
/// identifier it starts at, and the members already selected from it, in the
|
|
/// order they are written (`a.b.c`'s tail `c` gives `("a", ["b"])`).
|
|
///
|
|
/// `None` for a spine this crate cannot start walking — an index, a call, a
|
|
/// cast, or `this`/`super`, which name no declaration to take a type from.
|
|
fn spine(base: &Expr) -> Option<(String, Vec<String>)> {
|
|
let mut mids = Vec::new();
|
|
let mut current = base;
|
|
loop {
|
|
match ¤t.kind {
|
|
ExprKind::Ident(name) if matches!(name.as_str(), "this" | "super") => return None,
|
|
ExprKind::Ident(name) => {
|
|
mids.reverse();
|
|
return Some((name.clone(), mids));
|
|
}
|
|
ExprKind::Member { base, name, .. } => {
|
|
mids.push(name.clone());
|
|
current = base;
|
|
}
|
|
_ => return None,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Which kind a parameter declaration's keyword names.
|
|
///
|
|
/// `localparam` is the only spelling that is not overridable, and it is the
|
|
/// only thing this distinction is for; anything else — `parameter`, and the
|
|
/// empty string a continuation entry leaves behind before any keyword has
|
|
/// been written at all — is a [`SymbolKind::Param`].
|
|
fn param_kind(kw: &str) -> SymbolKind {
|
|
match kw {
|
|
// A `specparam` is not overridable from a `#( ... )` list — only an
|
|
// SDF annotator may change one — so it travels with `localparam`,
|
|
// whose kind exists to keep exactly that promise.
|
|
"localparam" | "specparam" => SymbolKind::LocalParam,
|
|
_ => SymbolKind::Param,
|
|
}
|
|
}
|
|
|
|
/// The byte range of a member access's tail: the construct's last token, so
|
|
/// its range is exactly the last `name.len()` bytes of the whole span.
|
|
fn member_tail(span: TextRange, name: &str) -> TextRange {
|
|
TextRange::new(span.end.saturating_sub(name.len() as u32), span.end)
|
|
}
|
|
|
|
/// The type name to record on a declaration: the name as written, minus the
|
|
/// interface port's glued-on modport (`bus_if.mp bus` — the interface is the
|
|
/// type, exactly as [`Binder::bind_data_type`] reads it). `None` for a
|
|
/// built-in type, whose keyword names no declaration to look members up in.
|
|
fn type_name_of(ty: &DataType) -> Option<String> {
|
|
let name = ty.name.split_once('.').map_or(ty.name.as_str(), |(iface, _)| iface);
|
|
(!name.is_empty() && !is_keyword(name)).then(|| name.to_string())
|
|
}
|
|
|
|
/// The last `::` segment of a possibly-qualified path (`pkg::cls` → `cls`).
|
|
fn last_segment(path: &str) -> &str {
|
|
path.rsplit("::").next().unwrap_or(path)
|
|
}
|
|
|
|
/// The `obj` / `obj.sub` a `randomize() with` call is made on, when the call
|
|
/// has such a head at all: `obj.randomize()` and `a.b.randomize()` do,
|
|
/// `randomize()` and `std::randomize(x)` do not. Returns the head name and
|
|
/// the middle links, which is exactly what [`Target::MemberOf`] carries.
|
|
fn randomize_head(base: &Expr) -> Option<(String, Vec<String>)> {
|
|
let ExprKind::Call { callee, .. } = &base.kind else { return None };
|
|
let ExprKind::Member { base, name, .. } = &callee.kind else { return None };
|
|
if name != "randomize" {
|
|
return None;
|
|
}
|
|
let mut mids = Vec::new();
|
|
let mut cur = base.as_ref();
|
|
loop {
|
|
match &cur.kind {
|
|
ExprKind::Ident(head) => {
|
|
mids.reverse();
|
|
return Some((head.clone(), mids));
|
|
}
|
|
ExprKind::Member { base, name, .. } => {
|
|
mids.push(name.clone());
|
|
cur = base;
|
|
}
|
|
_ => return None,
|
|
}
|
|
}
|
|
}
|