TODO §47 (docs/features/type-name-spans). Closes topo §2.9-2.
`DataType` carries `name_start` (a `u32` in its existing padding, so
`Expr` stays at its 96-byte pin) and derives `name_span()` and
`segments()` — the `::`/`.` split from the span's own text, with a
leading escaped identifier read to its terminator first. Five parser
construction sites set it; an implicit type gets the empty range at the
position its name would have had. `StmtKind::Disable { name, name_span }`.
The binder walks `ty.segments()` and verifies every segment's range
against the source before recording it (`exact_or_empty`); a segment that
does not read back is recorded with the empty range, never dropped, so
the census's `unplaced` counter is the alarm a join regression would
move (review rework of QA's MEDIUM). `scan.rs` deleted; `bind_data_type`
lost its `window` and ten call sites their window arithmetic.
Census unchanged in every field (860 files, 27 486 symbols, 103 870
references, 1 197 unresolved, 0 unplaced); 0 of 860 corpus files differ;
200/200 opentitan definition/hover/typeDefinition answers identical.
Co-Authored-By: Claude Fable 5.1 <[email protected]>
329 lines
15 KiB
Rust
329 lines
15 KiB
Rust
//! Declared names and the types written on them: declarators, port and
|
|
//! parameter lists, task/function arguments, typedefs, enum and struct bodies,
|
|
//! and [`Binder::bind_data_type`], the one place a type name becomes a
|
|
//! [`Target::Type`] use. Items and statements both declare through here, which
|
|
//! is why this is a sibling of both rather than part of either.
|
|
|
|
use vlog_parser::ast::*;
|
|
use vlog_parser::span::TextRange;
|
|
use vlog_parser::token::is_keyword;
|
|
|
|
use crate::model::{ScopeId, ScopeKind, SymbolId, SymbolKind};
|
|
|
|
use super::{type_name_of, Binder, Target};
|
|
|
|
impl<'a> Binder<'a> {
|
|
/// 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`.
|
|
///
|
|
/// An error element — a parameter a tolerant parse could not read — is
|
|
/// skipped, the way `bind/item.rs` skips an [`ItemKind::Error`]: it
|
|
/// declares nothing, so there is nothing to bind.
|
|
pub(super) fn bind_param_ports(
|
|
&mut self,
|
|
ports: impl Iterator<Item = &'a ListElem<ParamPort>>,
|
|
scope: ScopeId,
|
|
) {
|
|
let ports = ports.filter_map(ListElem::ok);
|
|
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, 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);
|
|
}
|
|
}
|
|
|
|
/// A subroutine's formals. An error element declares nothing, exactly as
|
|
/// in [`Binder::bind_param_ports`].
|
|
pub(super) fn bind_tf_args(&mut self, args: &'a [ListElem<TfArg>], scope: ScopeId) {
|
|
for a in args.iter().filter_map(ListElem::ok) {
|
|
self.bind_data_type(&a.ty, 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);
|
|
}
|
|
}
|
|
|
|
pub(super) 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, 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, 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, scope: ScopeId) {
|
|
if let Some(base) = &e.base {
|
|
self.bind_data_type(base, 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.
|
|
pub(super) 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 {
|
|
self.bind_data_type(&m.ty, 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);
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(super) fn bind_data_decl(&mut self, d: &DataDecl, scope: ScopeId) {
|
|
let kind = if d.net.is_some() { SymbolKind::Net } else { SymbolKind::Var };
|
|
self.bind_opt_expr(d.delay.as_ref().map(|n| &n.expr), scope);
|
|
self.bind_declarators(&d.declarators, kind, &d.ty, scope);
|
|
}
|
|
|
|
pub(super) 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, scope);
|
|
}
|
|
|
|
pub(super) fn bind_declarators(
|
|
&mut self,
|
|
declarators: &[Declarator],
|
|
kind: SymbolKind,
|
|
ty: &DataType,
|
|
scope: ScopeId,
|
|
) {
|
|
self.bind_data_type(ty, 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);
|
|
}
|
|
}
|
|
|
|
/// A user-defined type name is a use like any other, and the parser hands
|
|
/// over where every segment of it is written
|
|
/// ([`DataType::segments`]), so nothing here rescans the source.
|
|
pub(super) fn bind_data_type(&mut self, ty: &DataType, scope: ScopeId) {
|
|
if !ty.name.is_empty() && !is_keyword(&ty.name) {
|
|
self.bind_type_name(ty, scope);
|
|
}
|
|
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, scope),
|
|
Some(TypeExtra::Struct(s)) => self.bind_struct_uses(s, scope),
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
/// The segments of a written type name, each recorded as a use.
|
|
///
|
|
/// `pkg::item` is a unit and a member of it. `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) — is the interface as the
|
|
/// type plus the modport as a **member** of it, bound 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`.
|
|
///
|
|
/// Every span is checked against the source before it is recorded, the
|
|
/// rule the modport tail has always followed: a name the source spells
|
|
/// differently from the way the parser joined it — an escaped segment
|
|
/// terminated by a newline, a `bus_if . mp` written apart — costs the
|
|
/// reference and never mislocates it.
|
|
fn bind_type_name(&mut self, ty: &DataType, scope: ScopeId) {
|
|
let scoped = ty.name.contains("::");
|
|
let mut head: Option<&str> = None;
|
|
for (seg, span) in ty.segments() {
|
|
// A segment whose derived range does not read back as its own
|
|
// text (`pkg :: item`, a comment inside the name) is recorded
|
|
// with the empty range at its start, never dropped: an empty
|
|
// range is what the corpus census counts as *unplaced*, so a
|
|
// parser regression in the join would trip that alarm instead of
|
|
// silently lowering the resolution rate (§47's QA).
|
|
let span = self.exact_or_empty(span, seg);
|
|
match (head, scoped) {
|
|
(None, true) => self.use_name(scope, seg, span, Target::Unit),
|
|
(None, false) => self.use_name(scope, seg, span, Target::Type),
|
|
(Some(unit), true) => {
|
|
self.use_name(scope, seg, span, Target::UnitMember(unit.to_string()))
|
|
}
|
|
(Some(unit), false) => {
|
|
self.use_member(scope, seg, span, Target::TypeMember { unit: unit.to_string() })
|
|
}
|
|
}
|
|
head = Some(seg);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 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`].
|
|
pub(super) 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,
|
|
}
|
|
}
|