Block-scoped typedef statements (new StmtKind, three arms plus the typeDefinition hop QA's audit caught missing), system-function casting types (the bucket the analysis had mislabeled as drive strengths), enum member ranges, casts over assignment patterns, gate delay lists, virtual iface.modport through ansi-first-port's own gate, rand on struct members, foreach skipped dimensions, hierarchical bind instance paths. OpenTitan sweep 88 -> 43 with zero newly-failing files and the whole-tree superset check proving 0 of 3,855 previously-passing files moved; corpus byte-identical at 813/820; tolerant diagnostics 147 -> 0 on the fixed set and 0 files worse anywhere. One skip written up (pullup drive strength: two AST changes for one file). verify.rs untouched, no capture added. Co-Authored-By: Claude Fable 5 <[email protected]>
1613 lines
57 KiB
Rust
1613 lines
57 KiB
Rust
//! AST for the supported Verilog/SystemVerilog subset.
|
|
//!
|
|
//! Nodes that begin a line carry a [`Meta`] with leading comments, an
|
|
//! optional trailing comment, and a blank-line-before flag so the formatter
|
|
//! can reproduce vertical structure the way ruff's formatter does.
|
|
//!
|
|
//! Spans: a node's `span` is its **code extent** — from the first to the last
|
|
//! token of the construct itself. Attached comments are *not* part of it and
|
|
//! carry their own spans on [`Comment`]; attribute instances `(* ... *)` are
|
|
//! real tokens and *are* inside it. See `docs/features/spans/README.md` for
|
|
//! which node types carry a span and why the rest do not.
|
|
//!
|
|
//! Names: a node that declares a name keeps the name as a `String` and the
|
|
//! byte range of the identifier that spells it in a sibling field —
|
|
//! `name_span` for the declared name, `label_span` / `end_label_span` /
|
|
//! `module_span` for the other names a node can carry — so
|
|
//! `src[name_span] == name`. Where a node holds a *list* of names the two are
|
|
//! bundled into a [`Name`] instead, because parallel `Vec`s cannot be kept in
|
|
//! step by construction. See `docs/features/name-spans/README.md`.
|
|
|
|
use crate::span::TextRange;
|
|
use crate::token::Comment;
|
|
|
|
/// A name as written, together with the byte range it occupies:
|
|
/// `src[span] == text`.
|
|
///
|
|
/// Used where a node holds a *list* of names; a node with a single name keeps
|
|
/// it as a `String` plus a sibling `name_span`.
|
|
#[derive(Debug, Clone)]
|
|
pub struct Name {
|
|
pub text: String,
|
|
pub span: TextRange,
|
|
}
|
|
|
|
/// One `pkg::item` (or `pkg::*`) path of an `import`, each half with its own
|
|
/// range — a language server resolves the package and the imported name
|
|
/// separately.
|
|
#[derive(Debug, Clone)]
|
|
pub struct ImportPath {
|
|
pub pkg: Name,
|
|
/// The imported name, or `*` for a wildcard (whose span is the `*`).
|
|
pub item: Name,
|
|
}
|
|
|
|
impl ImportPath {
|
|
/// The path as it is written: `pkg::item`.
|
|
pub fn text(&self) -> String {
|
|
format!("{}::{}", self.pkg.text, self.item.text)
|
|
}
|
|
}
|
|
|
|
/// The `import "DPI-C" [context|pure] [c_name =]` prefix that makes a function
|
|
/// or task prototype a DPI import (IEEE 1800 §35.5). A DPI import *is* a
|
|
/// prototype, so it is modelled as [`FunctionDecl`] / [`TaskDecl`] with this
|
|
/// hung off it rather than as an item kind of its own.
|
|
#[derive(Debug, Clone)]
|
|
pub struct DpiImport {
|
|
/// The linkage spec string as written, quotes included: `"DPI-C"`.
|
|
pub spec: String,
|
|
/// The DPI import property, when written: `context` or `pure`.
|
|
pub property: Option<String>,
|
|
/// The linkage name of the `c_name = function ...` spelling.
|
|
pub c_identifier: Option<Name>,
|
|
}
|
|
|
|
/// An `export` item: a DPI export or a package re-export.
|
|
#[derive(Debug)]
|
|
pub enum ExportDecl {
|
|
/// `export "DPI-C" [c_name =] function|task name;` (IEEE 1800 §35.5.4).
|
|
Dpi(DpiExport),
|
|
/// `export pkg::item, pkg::*;` (IEEE 1800 §26.6). The LRM's `export *::*;`
|
|
/// is one path whose package half is `*` as well.
|
|
Package(Vec<ImportPath>),
|
|
}
|
|
|
|
/// One `export "DPI-C" ... ;` declaration.
|
|
#[derive(Debug)]
|
|
pub struct DpiExport {
|
|
/// The linkage spec string as written, quotes included: `"DPI-C"`.
|
|
pub spec: String,
|
|
/// The linkage name of the `c_name = function ...` spelling.
|
|
pub c_identifier: Option<Name>,
|
|
/// `function` or `task`.
|
|
pub kw: String,
|
|
/// The exported SystemVerilog task or function name.
|
|
pub name: Name,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Default)]
|
|
pub struct Meta {
|
|
pub leading: Vec<Comment>,
|
|
/// The comment written *after* the node and the separator that closes it
|
|
/// (`,` or `;`), on the same line. Several are merged into one, joined by
|
|
/// a space; the merged value keeps the first one's position and spans
|
|
/// from the first byte of the first to the last byte of the last.
|
|
pub trailing: Option<Comment>,
|
|
/// Comments written *between* the node and the separator that follows it
|
|
/// (`element /* c */,`). They print on the node's side of the separator,
|
|
/// which is where the source put them — printing them after it moves a
|
|
/// comment across a token and the safety net rejects the output. Empty
|
|
/// for a node no separator follows.
|
|
pub before_sep: Vec<Comment>,
|
|
pub blank_before: bool,
|
|
/// Attribute instances `(* ... *)` that preceded the node, verbatim.
|
|
pub attrs: Vec<RawText>,
|
|
}
|
|
|
|
/// Source text passed through verbatim: a compiler directive or an attribute
|
|
/// instance. `col` is the 1-based column it started in, which is what lets the
|
|
/// formatter re-indent continuation lines of a multi-line one without drifting
|
|
/// further right on every pass.
|
|
#[derive(Debug, Clone)]
|
|
pub struct RawText {
|
|
pub text: String,
|
|
pub col: u32,
|
|
/// Byte extent of the passed-through token.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
/// A compiler directive written *between* the elements of a port,
|
|
/// parameter-port, enum-member, parameter-override or instance-connection
|
|
/// list (`` `ifdef``/`` `else``/`` `elsif``/`` `endif``, `` `define``,
|
|
/// `` `undef``, `` `include``) — or a standalone macro invocation standing in
|
|
/// the same place, which is passed through the same way.
|
|
///
|
|
/// It is kept in a vector *beside* the elements rather than folded into one
|
|
/// enum with them: the element vectors are read by `vlog_semantic` and
|
|
/// `vlog_linter`, and a directive is not a port. `before` is what puts the
|
|
/// two back into source order. lucisv does not evaluate the condition — both
|
|
/// arms' elements are ordinary elements of the one list, exactly as an
|
|
/// `` `ifdef`` at item position already works.
|
|
#[derive(Debug, Clone)]
|
|
pub struct ListDirective {
|
|
/// The directive line, verbatim.
|
|
pub raw: RawText,
|
|
/// Comments written before the directive (`leading`) and the one written
|
|
/// after it on the same line (`trailing`).
|
|
pub meta: Meta,
|
|
/// Index of the element this directive was written before;
|
|
/// `elements.len()` when it stands after the last one. Several directives
|
|
/// at the same index keep their source order in the vector.
|
|
pub before: usize,
|
|
/// True when the `,` separating the surrounding elements was written
|
|
/// *after* this directive (`` `ifdef X`` ⏎ `, parameter string P = ""`),
|
|
/// which is how a conditional list tail stays legal in both arms.
|
|
pub comma: bool,
|
|
}
|
|
|
|
/// A macro invocation standing where a module item or a statement belongs
|
|
/// (`` `ASSERT_INIT(...) ``, `` `FFARNC(...) ``). Its arguments are token soup
|
|
/// handed to a preprocessor we do not run, so the whole invocation is kept as
|
|
/// verbatim source text — same treatment as a compiler directive.
|
|
#[derive(Debug)]
|
|
pub struct MacroCall {
|
|
/// The invocation from the backtick through the closing `)`, verbatim.
|
|
pub raw: RawText,
|
|
/// True if a `;` followed it in the source. Both spellings occur in real
|
|
/// RTL, so the formatter reproduces whichever one was written.
|
|
pub semi: bool,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct SourceFile {
|
|
pub items: Vec<Item>,
|
|
/// Comments at the very end of the file.
|
|
pub dangling: Vec<Comment>,
|
|
/// The whole source text: `0..len`.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct Item {
|
|
pub meta: Meta,
|
|
pub kind: ItemKind,
|
|
/// Code extent of the item, its `(* ... *)` attributes included.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum ItemKind {
|
|
Module(Module),
|
|
Package {
|
|
kw: String,
|
|
name: String,
|
|
/// Byte range of `name`.
|
|
name_span: TextRange,
|
|
items: Vec<Item>,
|
|
dangling: Vec<Comment>,
|
|
/// Label after `endpackage :`.
|
|
end_label: Option<String>,
|
|
/// Byte range of `end_label`.
|
|
end_label_span: Option<TextRange>,
|
|
},
|
|
Directive(RawText),
|
|
/// Macro invocation used as a module item.
|
|
MacroItem(MacroCall),
|
|
/// An elaboration system task standing where a module or generate item
|
|
/// belongs: `$error("...");`, `$fatal(1, "...");` (IEEE 1800 §20.11).
|
|
/// The payload is the ordinary system-task-call expression — only the
|
|
/// position is special.
|
|
ElabTask(Expr),
|
|
Import(Vec<ImportPath>),
|
|
/// `export "DPI-C" function f;` or `export pkg::*;`.
|
|
Export(ExportDecl),
|
|
Timeunit(String),
|
|
Typedef(Typedef),
|
|
ParamDecl(ParamDecl),
|
|
DataDecl(DataDecl),
|
|
/// Non-ANSI port declaration at module scope: `input [7:0] data;`
|
|
PortDecl(PortDecl),
|
|
ContAssign(ContAssign),
|
|
/// The body statement carries any `@(...)`/`#delay` timing control.
|
|
Always {
|
|
kw: String,
|
|
stmt: Stmt,
|
|
},
|
|
Initial(Stmt),
|
|
Final(Stmt),
|
|
Instantiation(Instantiation),
|
|
GateInstantiation(GateInstantiation),
|
|
/// `bind <target> [: <instances>] <instantiation>` (IEEE 1800 §23.11).
|
|
Bind(BindDirective),
|
|
Genvar(Vec<Name>),
|
|
GenerateRegion {
|
|
items: Vec<Item>,
|
|
dangling: Vec<Comment>,
|
|
},
|
|
GenIf(GenIf),
|
|
GenFor(GenFor),
|
|
GenCase(GenCase),
|
|
Function(FunctionDecl),
|
|
Task(TaskDecl),
|
|
/// `let name(ports) = expr;` (IEEE 1800 §11.12).
|
|
Let(LetDecl),
|
|
/// A `class` / `virtual class` declaration and its body.
|
|
Class(ClassDecl),
|
|
/// `modport a (input x, output y), b (...);` (IEEE 1800 §25.5). One
|
|
/// declaration may name several modports, so the payload is a list.
|
|
Modport(Vec<ModportDecl>),
|
|
/// `clocking cb @(posedge clk); ... endclocking` and the `default`
|
|
/// spellings of it (IEEE 1800 §14).
|
|
Clocking(ClockingDecl),
|
|
/// A block lucisv deliberately does not model, kept byte-for-byte:
|
|
/// `constraint c { ... }`, `covergroup ... endgroup`, a concurrent
|
|
/// assertion (`assert property ( ... );`) and a `property`/`sequence`
|
|
/// declaration. See `docs/features/classes/README.md` and
|
|
/// `docs/features/assertions/README.md` for why each one is verbatim.
|
|
Verbatim(RawText),
|
|
Defparam(Vec<(Expr, Expr)>),
|
|
/// Source that could not be parsed as an item, kept verbatim.
|
|
///
|
|
/// Only error-tolerant parsing
|
|
/// (`vlog_parser::parse_source_tolerant`) ever produces this; the
|
|
/// strict entry point fails on the first error instead, so the formatter
|
|
/// never sees one. See `docs/features/error-recovery/README.md`.
|
|
Error(RawText),
|
|
}
|
|
|
|
/// A `module` / `macromodule` / `interface` declaration.
|
|
///
|
|
/// An interface header is the module header's grammar (IEEE 1800 §25.3) and an
|
|
/// interface body is the module body's item list plus `modport` and `clocking`,
|
|
/// so the two share one node; `kw` says which was written and
|
|
/// [`Module::end_kw`] the closing keyword that goes with it.
|
|
#[derive(Debug)]
|
|
pub struct Module {
|
|
pub kw: String,
|
|
pub name: String,
|
|
/// Byte range of `name`.
|
|
pub name_span: TextRange,
|
|
/// One entry per `import` statement written in the header. A header may
|
|
/// write several (`module m import a::*; import b::t; (...)`), and merging
|
|
/// them into one statement would replace a `;` with a `,`.
|
|
pub imports: Vec<HeaderImport>,
|
|
/// Comments written between the header's name (or its last `import`) and
|
|
/// the `#` of the parameter port list. Empty when the header wrote none.
|
|
pub param_comments: Vec<Comment>,
|
|
/// `None` when the header wrote no `#( ... )` at all; an empty `Some` is a
|
|
/// written-but-empty `#()`, which must be printed back.
|
|
pub param_ports: Option<Vec<ParamPort>>,
|
|
/// Directives written between the parameter ports, in source order.
|
|
pub param_directives: Vec<ListDirective>,
|
|
/// Comments written after the last parameter port and before the `)`.
|
|
pub param_dangling: Vec<Comment>,
|
|
/// Comments written between whatever precedes the port list and its `(`.
|
|
/// Empty when the header wrote no parenthesized port list.
|
|
pub port_comments: Vec<Comment>,
|
|
pub ports: PortList,
|
|
/// Directives written between the header's ports, in source order. They
|
|
/// live here rather than inside [`PortList`] because a list whose every
|
|
/// element sits in an `` `ifdef`` arm is still a [`PortList::Empty`].
|
|
pub port_directives: Vec<ListDirective>,
|
|
/// Comments written after the last port and before the `)`.
|
|
pub port_dangling: Vec<Comment>,
|
|
pub items: Vec<Item>,
|
|
pub dangling: Vec<Comment>,
|
|
pub end_label: Option<String>,
|
|
/// Byte range of `end_label`.
|
|
pub end_label_span: Option<TextRange>,
|
|
}
|
|
|
|
impl Module {
|
|
/// The keyword that closes this declaration: `endmodule` or `endinterface`.
|
|
pub fn end_kw(&self) -> &'static str {
|
|
if self.kw == "interface" {
|
|
"endinterface"
|
|
} else {
|
|
"endmodule"
|
|
}
|
|
}
|
|
}
|
|
|
|
/// One `import` statement in a module header, with the comments written
|
|
/// around it. A header import is not an [`Item`], so it needs its own place
|
|
/// to keep them.
|
|
#[derive(Debug)]
|
|
pub struct HeaderImport {
|
|
pub meta: Meta,
|
|
/// The comma-separated paths of this one statement.
|
|
pub paths: Vec<ImportPath>,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum PortList {
|
|
/// No port list at all: `module m;`
|
|
None,
|
|
/// Empty parens: `module m();`
|
|
Empty,
|
|
/// ANSI-style declarations in the header.
|
|
Ansi(Vec<AnsiPort>),
|
|
/// Non-ANSI: just names in the header, declared in the body.
|
|
NonAnsi(Vec<NonAnsiPort>),
|
|
}
|
|
|
|
/// A name-only port in a non-ANSI header (`module m (a, b);`). It has no
|
|
/// `name_span`: the port *is* its name, so `span` already is that range.
|
|
#[derive(Debug)]
|
|
pub struct NonAnsiPort {
|
|
pub meta: Meta,
|
|
pub name: String,
|
|
/// True when a `,` followed this element in the source. See
|
|
/// [`ListDirective::comma`]: a directive between two elements can move
|
|
/// the comma to the other side of it, so the formatter reproduces the
|
|
/// placement it parsed rather than deriving it from the element's index.
|
|
pub comma: bool,
|
|
/// Code extent of the port name.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct AnsiPort {
|
|
pub meta: Meta,
|
|
pub dir: Option<String>,
|
|
pub net: Option<String>,
|
|
pub ty: DataType,
|
|
pub name: String,
|
|
/// Byte range of `name`.
|
|
pub name_span: TextRange,
|
|
pub unpacked: Vec<Dim>,
|
|
pub default: Option<Expr>,
|
|
/// True when a `,` followed this element in the source; see
|
|
/// [`NonAnsiPort::comma`].
|
|
pub comma: bool,
|
|
/// Code extent of the port declaration.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct ParamPort {
|
|
pub meta: Meta,
|
|
/// `parameter` or `localparam`; empty if omitted (continuation params).
|
|
pub kw: Option<String>,
|
|
pub ty: DataType,
|
|
pub is_type: bool,
|
|
pub name: String,
|
|
/// Byte range of `name`.
|
|
pub name_span: TextRange,
|
|
pub unpacked: Vec<Dim>,
|
|
pub default: Option<Expr>,
|
|
/// True when a `,` followed this element in the source; see
|
|
/// [`NonAnsiPort::comma`].
|
|
pub comma: bool,
|
|
/// Code extent of the parameter port.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
/// A data type: keyword or user-defined name, plus signing and packed dims.
|
|
/// `name` may be empty for implicit types (`input [7:0] x`).
|
|
#[derive(Debug, Clone, Default)]
|
|
pub struct DataType {
|
|
/// The `virtual` / `virtual interface` prefix of a virtual interface type
|
|
/// reference, as written (IEEE 1800 §25.9): `virtual clk_rst_if`,
|
|
/// `virtual interface bus_if`. Legal in every type position — a typedef's
|
|
/// base type, a task/function argument, a variable declaration.
|
|
///
|
|
/// A *declaration* keeps it in [`DataDecl::qualifiers`] instead, where the
|
|
/// rest of the member qualifiers already live; this field carries the
|
|
/// positions that have no qualifier list of their own.
|
|
///
|
|
/// A one-byte enum rather than the text, for the same reason `extra` is
|
|
/// boxed: every `Expr` carries a `DataType` through [`ExprKind::Type`],
|
|
/// so a rare field must not widen the common case.
|
|
pub virt: Option<VirtualKw>,
|
|
pub name: String,
|
|
/// The rare tail of a type: parameterization or an anonymous aggregate
|
|
/// body. See [`TypeExtra`]; read it through [`DataType::params`] /
|
|
/// [`DataType::inline`].
|
|
///
|
|
/// Boxed — and the two spellings share the one box — to keep `DataType`,
|
|
/// and through [`ExprKind::Type`] every `Expr`, small.
|
|
pub extra: Option<Box<TypeExtra>>,
|
|
pub signing: Option<String>,
|
|
pub packed: Vec<Dim>,
|
|
}
|
|
|
|
impl DataType {
|
|
pub fn is_implicit(&self) -> bool {
|
|
self.virt.is_none()
|
|
&& self.name.is_empty()
|
|
&& self.extra.is_none()
|
|
&& self.signing.is_none()
|
|
&& self.packed.is_empty()
|
|
}
|
|
|
|
/// The `#( ... )` of a parameterized type reference, if one was written.
|
|
pub fn params(&self) -> Option<&RawText> {
|
|
match self.extra.as_deref() {
|
|
Some(TypeExtra::Params(p)) => Some(p),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
/// The anonymous aggregate that *is* this type, if one was written.
|
|
pub fn inline(&self) -> Option<&TypeExtra> {
|
|
match self.extra.as_deref() {
|
|
Some(e @ (TypeExtra::Enum(_) | TypeExtra::Struct(_))) => Some(e),
|
|
_ => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// What a type carries beyond its name, signing and packed dimensions. A type
|
|
/// never has two of these at once — an anonymous aggregate has no name to
|
|
/// parameterize — so they share one box on [`DataType`].
|
|
#[derive(Debug, Clone)]
|
|
pub enum TypeExtra {
|
|
/// The `#( ... )` of a parameterized type reference, verbatim:
|
|
/// `uvm_analysis_port #(bit [31:0]) port;`. Its arguments are types as
|
|
/// often as expressions (`#(virtual bus_if)`, `#(item #(A, B))`), so lucisv
|
|
/// reproduces them as written instead of modelling a second grammar.
|
|
Params(RawText),
|
|
/// An anonymous enum standing where a type name would:
|
|
/// `enum logic [1:0] {IDLE, STOP} state;`.
|
|
Enum(EnumType),
|
|
/// An anonymous struct or union standing where a type name would:
|
|
/// `struct packed { ... } s;`.
|
|
Struct(StructType),
|
|
}
|
|
|
|
/// How a virtual interface type reference is spelled. The `interface` keyword
|
|
/// is optional (IEEE 1800 §25.9) and both spellings occur, so which one was
|
|
/// written is recorded and printed back.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub enum VirtualKw {
|
|
/// `virtual bus_if h;`
|
|
Bare,
|
|
/// `virtual interface bus_if h;`
|
|
Interface,
|
|
}
|
|
|
|
impl VirtualKw {
|
|
/// The prefix as it is written, `interface` keyword included.
|
|
pub fn text(self) -> &'static str {
|
|
match self {
|
|
VirtualKw::Bare => "virtual",
|
|
VirtualKw::Interface => "virtual interface",
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A dimension: `[expr]`, `[a:b]`, `[]`, or `[*]`.
|
|
#[derive(Debug, Clone)]
|
|
pub struct Dim {
|
|
pub kind: DimKind,
|
|
/// Code extent including the brackets.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
/// What stands between a dimension's brackets.
|
|
#[derive(Debug, Clone)]
|
|
pub enum DimKind {
|
|
/// `[expr]` or `[a:b]` — also `[$]`, `[$:N]` (a bare `$` is an ordinary
|
|
/// expression) and `[string]` / `[key_t]` (a type or name used as an
|
|
/// associative-array key parses as an expression).
|
|
Range { a: Expr, b: Option<Expr> },
|
|
/// `[]` — a dynamic array.
|
|
Dynamic,
|
|
/// `[*]` — a wildcard-keyed associative array.
|
|
Wildcard,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct Typedef {
|
|
pub name: String,
|
|
/// Byte range of `name`.
|
|
pub name_span: TextRange,
|
|
pub unpacked: Vec<Dim>,
|
|
pub kind: TypedefKind,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum TypedefKind {
|
|
Alias(DataType),
|
|
Enum(EnumType),
|
|
Struct(StructType),
|
|
/// A forward declaration: `typedef class my_class;`
|
|
ForwardClass,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct EnumType {
|
|
pub base: Option<DataType>,
|
|
pub members: Vec<EnumMember>,
|
|
/// Directives written between the members, in source order.
|
|
pub directives: Vec<ListDirective>,
|
|
pub dangling: Vec<Comment>,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct EnumMember {
|
|
pub meta: Meta,
|
|
pub name: String,
|
|
/// Byte range of `name`.
|
|
pub name_span: TextRange,
|
|
/// The enum name range of IEEE 1800 §6.19 — `Name[N]` or `Name[hi:lo]`,
|
|
/// which declares a run of sequentially-valued members. One dimension is
|
|
/// all the LRM allows; it is kept as an ordinary [`Dim`] so the existing
|
|
/// dimension parser and printer serve it, and it is printed back tight
|
|
/// against the name because it is part of the name, not a layout choice.
|
|
pub range: Vec<Dim>,
|
|
pub value: Option<Expr>,
|
|
/// True when a `,` followed this element in the source; see
|
|
/// [`NonAnsiPort::comma`].
|
|
pub comma: bool,
|
|
/// Code extent of the member.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct StructType {
|
|
/// "struct" or "union"
|
|
pub kw: String,
|
|
pub packed: bool,
|
|
pub signing: Option<String>,
|
|
pub members: Vec<StructMember>,
|
|
pub dangling: Vec<Comment>,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct StructMember {
|
|
pub meta: Meta,
|
|
/// `rand` / `randc` written before the member's type. The LRM allows the
|
|
/// randomization qualifiers only on a class property, but vendor tools
|
|
/// accept them on a plain struct member and real code uses them
|
|
/// (`spi_host/dv/env/spi_host_env_pkg.sv:64`); lucisv models no
|
|
/// randomization semantics, so the words are simply carried and printed.
|
|
pub qualifiers: Vec<String>,
|
|
pub ty: DataType,
|
|
pub declarators: Vec<Declarator>,
|
|
/// Code extent of the member, its `;` included.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct ParamDecl {
|
|
/// `parameter` or `localparam`
|
|
pub kw: String,
|
|
/// A type parameter: `localparam type payload_t = logic [7:0];`. The
|
|
/// declarator's `init` is then an [`ExprKind::Type`], not a value.
|
|
pub is_type: bool,
|
|
pub ty: DataType,
|
|
pub declarators: Vec<Declarator>,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct DataDecl {
|
|
/// Net type (`wire`, `tri`, ...) if any.
|
|
pub net: Option<String>,
|
|
/// Net delay: the `5` in `wire #5 w;`
|
|
pub delay: Option<Expr>,
|
|
/// Qualifiers written before the type, in source order: `var`,
|
|
/// `automatic`, `static`, `const`, and — inside a class body — `rand`,
|
|
/// `randc`, `local`, `protected`, and the `virtual [interface]` of a
|
|
/// virtual-interface property.
|
|
pub qualifiers: Vec<String>,
|
|
pub ty: DataType,
|
|
pub declarators: Vec<Declarator>,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct PortDecl {
|
|
pub dir: String,
|
|
pub net: Option<String>,
|
|
pub ty: DataType,
|
|
pub declarators: Vec<Declarator>,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct Declarator {
|
|
pub meta: Meta,
|
|
pub name: String,
|
|
/// Byte range of `name`.
|
|
pub name_span: TextRange,
|
|
pub unpacked: Vec<Dim>,
|
|
/// `=` for variables; net declarations also use this for the initial
|
|
/// continuous assignment.
|
|
pub init: Option<Expr>,
|
|
/// Code extent of the declarator.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct ContAssign {
|
|
/// Drive strength: the `(weak0, pull1)` in
|
|
/// `assign (weak0, pull1) x = y;` (IEEE 1800 §10.3.1), in source order.
|
|
pub strength: Option<DriveStrength>,
|
|
/// Delay expression: the `5` in `assign #5 x = y;`
|
|
pub delay: Option<Expr>,
|
|
pub assigns: Vec<AssignPair>,
|
|
}
|
|
|
|
/// The `( strength0 , strength1 )` pair of a continuous assignment. The two
|
|
/// strengths are stored in the order the source wrote them — the language
|
|
/// allows either order and lucisv is not a legality checker.
|
|
#[derive(Debug)]
|
|
pub struct DriveStrength {
|
|
pub first: String,
|
|
pub second: String,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct AssignPair {
|
|
pub meta: Meta,
|
|
pub lhs: Expr,
|
|
pub rhs: Expr,
|
|
/// Comments written between the `=` and the right-hand side. Kept here so
|
|
/// they print on the `=`'s side of the value instead of drifting into it.
|
|
pub rhs_comments: Vec<Comment>,
|
|
/// Code extent of `lhs = rhs`.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum SensList {
|
|
/// `@*` or `@(*)`, with any comments written next to the `*` so the
|
|
/// formatter can print them back in the same place.
|
|
Star {
|
|
/// Comments between the `@`/`(` and the `*`.
|
|
before: Vec<Comment>,
|
|
/// Comments between the `*` and the closing `)`.
|
|
after: Vec<Comment>,
|
|
},
|
|
List(Vec<SensItem>),
|
|
/// `@name` or `@a.b` written without parens; printed back without them —
|
|
/// inventing a `( ... )` would be a token-stream change the safety net
|
|
/// refuses.
|
|
Bare(Expr),
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct SensItem {
|
|
/// Comments written around this item: `leading` before it (after the `(`
|
|
/// or the separator), `before_sep` between it and the separator that
|
|
/// follows — or the closing `)` for the last item.
|
|
pub meta: Meta,
|
|
/// The separator written before this item; `None` on the first item.
|
|
pub sep: Option<SensSep>,
|
|
pub edge: Option<String>,
|
|
pub expr: Expr,
|
|
}
|
|
|
|
/// How two event-list items were separated in the source. Both spellings are
|
|
/// legal and mean the same thing, but rewriting one as the other changes the
|
|
/// token stream, so the formatter reproduces whichever was written.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub enum SensSep {
|
|
/// `@(posedge clk, negedge rst_n)`
|
|
Comma,
|
|
/// `@(posedge clk or negedge rst_n)`
|
|
Or,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct Instantiation {
|
|
pub module: String,
|
|
/// Byte range of `module`. A scope-qualified name (`pkg::mod`) is stored
|
|
/// with its `::` glued, so there the slice matches `module` only modulo
|
|
/// the whitespace the source may have written around the `::`.
|
|
pub module_span: TextRange,
|
|
/// Comments written between the module name and the `#` of the parameter
|
|
/// override list. Empty when the source wrote no `#(...)`.
|
|
pub param_comments: Vec<Comment>,
|
|
/// The parameter override list, `None` when the source wrote no `#(...)`
|
|
/// at all. An empty `Some` is a written-but-empty `#()`, which must be
|
|
/// printed back: dropping it would lose two tokens.
|
|
pub params: Option<Vec<Connection>>,
|
|
/// Directives written between the parameter overrides, in source order.
|
|
/// They live here rather than inside `params` because a `#( ... )` whose
|
|
/// whole body is one `` `include`` has no override to hang them on.
|
|
pub param_directives: Vec<ListDirective>,
|
|
pub instances: Vec<Instance>,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct Instance {
|
|
pub meta: Meta,
|
|
pub name: String,
|
|
/// Byte range of `name`.
|
|
pub name_span: TextRange,
|
|
pub dims: Vec<Dim>,
|
|
/// Comments written between the instance name (or its dimensions) and the
|
|
/// `(` opening its connection list.
|
|
pub conn_comments: Vec<Comment>,
|
|
pub conns: Connections,
|
|
/// Directives written between the connections, in source order. They live
|
|
/// here rather than inside [`Connections`] because a list whose every
|
|
/// element sits in an `` `ifdef`` arm is still an empty
|
|
/// [`Connections::Ordered`].
|
|
pub conn_directives: Vec<ListDirective>,
|
|
/// Code extent of the instance, its connection list included.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum Connections {
|
|
Ordered(Vec<OrderedConn>),
|
|
Named(Vec<Connection>),
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct OrderedConn {
|
|
pub meta: Meta,
|
|
pub expr: Option<Expr>,
|
|
/// True when a `,` followed this element in the source; see
|
|
/// [`NonAnsiPort::comma`].
|
|
pub comma: bool,
|
|
/// Code extent of the connection; empty for an omitted one.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct Connection {
|
|
pub meta: Meta,
|
|
/// Port/parameter name; `*` for the SystemVerilog `.*` wildcard.
|
|
pub name: String,
|
|
/// Byte range of `name`; empty for a positional connection, which has no
|
|
/// name to point at.
|
|
pub name_span: TextRange,
|
|
/// `None` for `.name` (implicit) and `.*`; `Some` for `.name(expr)`.
|
|
/// An explicitly empty connection `.name()` is `Some(Expr::Empty)`.
|
|
pub expr: Option<Expr>,
|
|
/// Comments written between the value and the connection's own `)`
|
|
/// (`.data_i (x /* verilator lint_off */)`). Printed back inside the
|
|
/// parens: after them they would sit in the enclosing list instead.
|
|
pub close_comments: Vec<Comment>,
|
|
/// True when a `,` followed this element in the source; see
|
|
/// [`NonAnsiPort::comma`].
|
|
pub comma: bool,
|
|
/// Code extent of the connection.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
/// A `bind` directive: everything after the target scope is an ordinary
|
|
/// module instantiation, so it is parsed and printed as one.
|
|
#[derive(Debug)]
|
|
pub struct BindDirective {
|
|
/// The bind target: a module or interface name (`prim_secded_22_16_tb`),
|
|
/// an instance name (`i_nopass1`) or a hierarchical path
|
|
/// (`top.u_dut.u_core`), stored with its `.` separators glued.
|
|
pub target: String,
|
|
/// Byte range of `target`. A hierarchical target is stored with its `.`
|
|
/// glued, so there the slice matches `target` only modulo the whitespace
|
|
/// the source may have written around the dots — the same caveat
|
|
/// [`Instantiation::module_span`] carries for `::`.
|
|
pub target_span: TextRange,
|
|
/// Comments written between `bind` and the target name. They have no node
|
|
/// of their own to hang on — the target is a bare string — so they are
|
|
/// kept here and printed back between the same two tokens.
|
|
pub target_comments: Vec<Comment>,
|
|
/// The `: u_a, u_b` instance list that narrows a module target to
|
|
/// particular instances. Empty when the source wrote no `:`.
|
|
pub instances: Vec<Name>,
|
|
/// Comments written between the target (or the instance list) and the
|
|
/// module name of the instantiation, for the same reason.
|
|
pub module_comments: Vec<Comment>,
|
|
/// The instantiation being bound, identical in shape to a standalone one.
|
|
pub inst: Instantiation,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct GateInstantiation {
|
|
/// `and`, `or`, `not`, ...
|
|
pub gate: String,
|
|
/// The delay written after `#`: empty when there is none, one element for
|
|
/// every spelling a net or a continuous assignment also accepts, and two
|
|
/// or three for the `(rise, fall[, turn-off])` list that only a gate
|
|
/// takes (IEEE 1800 §28.14, `top_earlgrey/ip/ast/rtl/aon_osc.sv:41`).
|
|
pub delay: Vec<Expr>,
|
|
pub instances: Vec<GateInstance>,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct GateInstance {
|
|
pub meta: Meta,
|
|
pub name: Option<String>,
|
|
/// Byte range of `name`.
|
|
pub name_span: Option<TextRange>,
|
|
pub args: Vec<ExprItem>,
|
|
/// Code extent of the gate instance.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct GenIf {
|
|
pub cond: Expr,
|
|
pub then: GenBlock,
|
|
pub els: Option<Box<GenElse>>,
|
|
/// Code extent of the whole `if`, its `else` branch included.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum GenElse {
|
|
/// `else if`: boxed because a `Meta` plus a whole `GenIf` inline makes
|
|
/// this variant several times the size of `Block`.
|
|
If(Meta, Box<GenIf>),
|
|
Block(GenBlock),
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct GenFor {
|
|
pub init: SimpleAssign,
|
|
pub init_genvar: bool,
|
|
pub cond: Expr,
|
|
pub step: ForStep,
|
|
pub body: GenBlock,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct GenCase {
|
|
pub expr: Expr,
|
|
pub items: Vec<GenCaseItem>,
|
|
pub dangling: Vec<Comment>,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct GenCaseItem {
|
|
pub meta: Meta,
|
|
/// Empty means `default`. Each label carries the comments written around
|
|
/// it, so a comment between two labels stays between them.
|
|
pub labels: Vec<ExprItem>,
|
|
pub body: GenBlock,
|
|
/// Code extent of the case item, its body included.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
/// A single generate block: either a labeled/unlabeled begin..end with items,
|
|
/// or a single item.
|
|
#[derive(Debug)]
|
|
pub struct GenBlock {
|
|
pub label: Option<String>,
|
|
/// Byte range of `label`.
|
|
pub label_span: Option<TextRange>,
|
|
pub is_block: bool,
|
|
pub items: Vec<Item>,
|
|
pub dangling: Vec<Comment>,
|
|
/// Label after `end :`.
|
|
pub end_label: Option<String>,
|
|
/// Byte range of `end_label`.
|
|
pub end_label_span: Option<TextRange>,
|
|
/// Code extent of the block or of the single item it holds.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
/// A `class` declaration. Its body is an ordinary item list, so every item
|
|
/// kind that already works at module or package scope — declarations,
|
|
/// `typedef`, `localparam`, `import`, compiler directives, macro invocations
|
|
/// — works inside a class without any per-site code.
|
|
#[derive(Debug)]
|
|
pub struct ClassDecl {
|
|
/// Keywords written before `class`; `virtual` is the only one real code
|
|
/// uses. Kept as a list so the source order is reproduced verbatim.
|
|
pub prefix: Vec<String>,
|
|
/// `static`/`automatic` lifetime written after `class`, if any.
|
|
pub lifetime: Option<String>,
|
|
pub name: String,
|
|
/// Byte range of `name`.
|
|
pub name_span: TextRange,
|
|
/// Comments written between the class name and the `#` of its parameter
|
|
/// port list.
|
|
pub param_comments: Vec<Comment>,
|
|
/// `None` when the header wrote no `#( ... )`; an empty `Some` is a
|
|
/// written-but-empty `#()`.
|
|
pub param_ports: Option<Vec<ParamPort>>,
|
|
/// Directives written between the parameter ports, in source order.
|
|
pub param_directives: Vec<ListDirective>,
|
|
/// Comments written after the last parameter port and before its `)`.
|
|
pub param_dangling: Vec<Comment>,
|
|
/// The `extends Base #( ... )` clause, if written.
|
|
pub extends: Option<ClassExtends>,
|
|
/// Interface classes named by `implements A, B`.
|
|
pub implements: Vec<Name>,
|
|
pub items: Vec<Item>,
|
|
pub dangling: Vec<Comment>,
|
|
/// Label after `endclass :`.
|
|
pub end_label: Option<String>,
|
|
/// Byte range of `end_label`.
|
|
pub end_label_span: Option<TextRange>,
|
|
}
|
|
|
|
/// The `extends Base #( ... )` clause of a class header. The overrides are
|
|
/// [`Connection`]s — the same node an instantiation's `#( ... )` uses, because
|
|
/// both spell named (`.CFG_T(cfg_t)`) and ordered (`cfg_t`) overrides the same
|
|
/// way.
|
|
#[derive(Debug)]
|
|
pub struct ClassExtends {
|
|
/// The base-class name as written, package scope included (`pkg::base`).
|
|
pub name: String,
|
|
/// Byte range covering `name`, its `::` chain included.
|
|
pub name_span: TextRange,
|
|
/// Comments written between the base-class name and the `#`.
|
|
pub param_comments: Vec<Comment>,
|
|
/// `None` when no `#( ... )` was written.
|
|
pub params: Option<Vec<Connection>>,
|
|
/// Directives written between the overrides, in source order.
|
|
pub param_directives: Vec<ListDirective>,
|
|
/// Code extent of the clause, from `extends` through its last token.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
/// One modport of a `modport a (...), b (...);` declaration (IEEE 1800 §25.5).
|
|
#[derive(Debug)]
|
|
pub struct ModportDecl {
|
|
/// Comments written around this modport within the declaration.
|
|
pub meta: Meta,
|
|
pub name: String,
|
|
/// Byte range of `name`.
|
|
pub name_span: TextRange,
|
|
pub ports: Vec<ModportPort>,
|
|
/// Comments written after the last port and before the `)`.
|
|
pub dangling: Vec<Comment>,
|
|
/// True when a `,` followed this modport in the source.
|
|
pub comma: bool,
|
|
/// Code extent from the name through the closing `)`.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
/// One direction group of a modport's port list: the keyword plus every name
|
|
/// it governs. `input a, b, output c` is two groups, which is how the source's
|
|
/// grouping survives a reformat.
|
|
#[derive(Debug)]
|
|
pub struct ModportPort {
|
|
pub meta: Meta,
|
|
/// `input`, `output`, `inout`, `ref`, `import`, `export` or `clocking`.
|
|
pub kw: String,
|
|
/// The names this keyword governs. A `clocking` group names exactly one.
|
|
pub names: Vec<Name>,
|
|
/// True when a `,` followed the last name of this group in the source.
|
|
pub comma: bool,
|
|
/// Code extent from the keyword through its last name.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
/// `clocking cb @(posedge clk); ... endclocking` (IEEE 1800 §14.3), including
|
|
/// the `default clocking` spellings and the body-less
|
|
/// `default clocking cb;` clocking assignment (§14.12).
|
|
#[derive(Debug)]
|
|
pub struct ClockingDecl {
|
|
/// True when the declaration was written `default clocking ...`.
|
|
pub is_default: bool,
|
|
/// `None` for the anonymous `default clocking @(posedge clk);` form.
|
|
pub name: Option<Name>,
|
|
/// The `@( ... )` clocking event. `None` for `default clocking cb;`, which
|
|
/// selects an existing clocking block and has no body.
|
|
pub event: Option<SensList>,
|
|
/// Empty when there is no body (`default clocking cb;`).
|
|
pub items: Vec<ClockingItem>,
|
|
pub dangling: Vec<Comment>,
|
|
/// True when the declaration wrote an `endclocking`.
|
|
pub has_end: bool,
|
|
/// Label after `endclocking :`.
|
|
pub end_label: Option<String>,
|
|
/// Byte range of `end_label`.
|
|
pub end_label_span: Option<TextRange>,
|
|
}
|
|
|
|
/// One item of a clocking block's body.
|
|
#[derive(Debug)]
|
|
pub struct ClockingItem {
|
|
pub meta: Meta,
|
|
pub kind: ClockingItemKind,
|
|
/// Code extent of the item, its `;` excluded.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum ClockingItemKind {
|
|
/// `default input #1step output #0;` — at least one half is written.
|
|
Default { input: Option<String>, output: Option<String> },
|
|
/// `input #2 a, b;`, `output negedge c;`, `inout d;`. `skew` is the delay
|
|
/// or edge as written (`#2`, `negedge`), or `None` when omitted.
|
|
Signals { dir: String, skew: Option<String>, names: Vec<Name> },
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct FunctionDecl {
|
|
/// `Some` when this is a DPI import — `import "DPI-C" function ...;` —
|
|
/// which is a prototype with a linkage prefix.
|
|
pub dpi: Option<DpiImport>,
|
|
/// Class-member qualifiers written *before* the `function` keyword, in
|
|
/// source order: `virtual`, `local`, `protected`, `static`, `pure`,
|
|
/// `extern`. Empty outside a class body.
|
|
pub prefix: Vec<String>,
|
|
pub qualifiers: Vec<String>,
|
|
/// True for a body-less prototype — `extern function ...;`,
|
|
/// `pure virtual function ...;` or a DPI import — which has no
|
|
/// `endfunction`.
|
|
pub is_prototype: bool,
|
|
/// The `my_class::` written before the name when an `extern` method is
|
|
/// *defined* outside its class body. Kept apart from `name` so
|
|
/// `src[name_span] == name` still holds for the method's own name.
|
|
pub scope: Option<String>,
|
|
pub ret: Option<DataType>,
|
|
pub name: String,
|
|
/// Byte range of `name`.
|
|
pub name_span: TextRange,
|
|
/// False for old-style `function f;` declarations without a port list.
|
|
pub has_parens: bool,
|
|
pub args: Vec<TfArg>,
|
|
pub stmts: Vec<Stmt>,
|
|
pub dangling: Vec<Comment>,
|
|
/// Label after `endfunction :`.
|
|
pub end_label: Option<String>,
|
|
/// Byte range of `end_label`.
|
|
pub end_label_span: Option<TextRange>,
|
|
/// True when a `;` was written after the footer (`endfunction : f;`) — a
|
|
/// null class item (IEEE 1800 A.1.9), reproduced where it was written so
|
|
/// the token stream is unchanged.
|
|
pub end_semi: bool,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct TaskDecl {
|
|
/// `Some` when this is a DPI import; see [`FunctionDecl::dpi`].
|
|
pub dpi: Option<DpiImport>,
|
|
/// Class-member qualifiers written *before* the `task` keyword; see
|
|
/// [`FunctionDecl::prefix`].
|
|
pub prefix: Vec<String>,
|
|
pub qualifiers: Vec<String>,
|
|
/// True for a body-less prototype; see [`FunctionDecl::is_prototype`].
|
|
pub is_prototype: bool,
|
|
/// Out-of-body definition scope; see [`FunctionDecl::scope`].
|
|
pub scope: Option<String>,
|
|
pub name: String,
|
|
/// Byte range of `name`.
|
|
pub name_span: TextRange,
|
|
/// False for old-style `task t;` declarations without a port list.
|
|
pub has_parens: bool,
|
|
pub args: Vec<TfArg>,
|
|
pub stmts: Vec<Stmt>,
|
|
pub dangling: Vec<Comment>,
|
|
/// Label after `endtask :`.
|
|
pub end_label: Option<String>,
|
|
/// Byte range of `end_label`.
|
|
pub end_label_span: Option<TextRange>,
|
|
/// True when a `;` was written after the footer; see
|
|
/// [`FunctionDecl::end_semi`].
|
|
pub end_semi: bool,
|
|
}
|
|
|
|
/// A `let` declaration: `let max(a, b) = (a > b) ? a : b;` (IEEE 1800 §11.12).
|
|
///
|
|
/// A `let` port is the same `[type] name [= default]` a task/function port is
|
|
/// minus the direction keyword, so the ports reuse [`TfArg`] rather than
|
|
/// growing a near-identical node of their own.
|
|
#[derive(Debug)]
|
|
pub struct LetDecl {
|
|
pub name: String,
|
|
/// Byte range of `name`.
|
|
pub name_span: TextRange,
|
|
/// False for the port-less spelling `let x = expr;` — an empty `()` was
|
|
/// written when this is true and [`LetDecl::ports`] is empty.
|
|
pub has_parens: bool,
|
|
pub ports: Vec<TfArg>,
|
|
pub value: Expr,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct TfArg {
|
|
pub meta: Meta,
|
|
pub dir: Option<String>,
|
|
pub ty: DataType,
|
|
pub name: String,
|
|
/// Byte range of `name`.
|
|
pub name_span: TextRange,
|
|
pub unpacked: Vec<Dim>,
|
|
pub default: Option<Expr>,
|
|
/// Code extent of the argument declaration.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct Stmt {
|
|
pub meta: Meta,
|
|
pub kind: StmtKind,
|
|
/// Code extent of the statement, its `(* ... *)` attributes included.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum StmtKind {
|
|
Block {
|
|
label: Option<String>,
|
|
/// Byte range of `label`.
|
|
label_span: Option<TextRange>,
|
|
stmts: Vec<Stmt>,
|
|
dangling: Vec<Comment>,
|
|
/// Label after `end :`.
|
|
end_label: Option<String>,
|
|
/// Byte range of `end_label`.
|
|
end_label_span: Option<TextRange>,
|
|
},
|
|
ForkJoin {
|
|
label: Option<String>,
|
|
/// Byte range of `label`.
|
|
label_span: Option<TextRange>,
|
|
join_kw: String,
|
|
stmts: Vec<Stmt>,
|
|
dangling: Vec<Comment>,
|
|
/// Label after `join :` (any join kind).
|
|
end_label: Option<String>,
|
|
/// Byte range of `end_label`.
|
|
end_label_span: Option<TextRange>,
|
|
},
|
|
If {
|
|
qualifier: Option<String>,
|
|
cond: Expr,
|
|
then: Box<Stmt>,
|
|
/// Comments written between the then-branch and the `else` keyword.
|
|
/// They are kept here, not on `els`, because printing them after the
|
|
/// keyword moves them across it and the safety net rejects that.
|
|
else_comments: Vec<Comment>,
|
|
els: Option<Box<Stmt>>,
|
|
},
|
|
Case {
|
|
qualifier: Option<String>,
|
|
kw: String,
|
|
expr: Expr,
|
|
/// True for `case (expr) inside`, whose labels may be `[lo:hi]`
|
|
/// value ranges.
|
|
inside: bool,
|
|
items: Vec<CaseItem>,
|
|
dangling: Vec<Comment>,
|
|
},
|
|
For {
|
|
init: Vec<ForInit>,
|
|
cond: Option<Expr>,
|
|
step: Vec<ForStep>,
|
|
body: Box<Stmt>,
|
|
},
|
|
While {
|
|
cond: Expr,
|
|
body: Box<Stmt>,
|
|
},
|
|
DoWhile {
|
|
body: Box<Stmt>,
|
|
cond: Expr,
|
|
},
|
|
Repeat {
|
|
count: Expr,
|
|
body: Box<Stmt>,
|
|
},
|
|
Forever {
|
|
body: Box<Stmt>,
|
|
},
|
|
/// `foreach (arr[i, j]) stmt` (IEEE 1800 §12.7.3).
|
|
///
|
|
/// `target` is the array being iterated, which may be a package-scoped or
|
|
/// hierarchical name with index and member selects along the way
|
|
/// (`riscv_instr_pkg::supported_isa`, `page_table[i].pte`); `vars` are the
|
|
/// names in the *last* bracket group, the only one that declares loop
|
|
/// variables.
|
|
Foreach {
|
|
target: Expr,
|
|
vars: Vec<Name>,
|
|
body: Box<Stmt>,
|
|
},
|
|
/// `randcase w1 : stmt; w2 : stmt; endcase` (IEEE 1800 §18.16) — a `case`
|
|
/// whose items are weights and which has no selector expression.
|
|
RandCase {
|
|
items: Vec<CaseItem>,
|
|
dangling: Vec<Comment>,
|
|
},
|
|
/// `wait fork;` — block until every child process finishes
|
|
/// (IEEE 1800 §9.6.1). `fork` is a keyword, not an expression, which is
|
|
/// why this is not a [`StmtKind::Wait`].
|
|
WaitFork,
|
|
/// `import pkg::item;` / `import pkg::*;` standing among a block's
|
|
/// declarations (IEEE 1800 §13.4.2) — the statement twin of
|
|
/// [`ItemKind::Import`].
|
|
Import(Vec<ImportPath>),
|
|
/// `typedef enum bit {WRITE, READ} rdwr_e;` written inside a
|
|
/// function/task body or a `begin` block, scoping the type to that block
|
|
/// (IEEE 1800 §6.18) — the statement twin of [`ItemKind::Typedef`], with
|
|
/// the same payload and the same layout.
|
|
Typedef(Typedef),
|
|
/// `name : stmt` — a statement label (IEEE 1800 §9.3.5).
|
|
///
|
|
/// `begin`/`fork` keep their label inside their own node, because the
|
|
/// source writes it *after* the keyword; this is the general prefix form,
|
|
/// which any statement may carry.
|
|
Labeled {
|
|
name: String,
|
|
/// Byte range of `name`.
|
|
name_span: TextRange,
|
|
stmt: Box<Stmt>,
|
|
},
|
|
/// Blocking/nonblocking/compound assignment.
|
|
Assign {
|
|
lhs: Expr,
|
|
op: String,
|
|
/// Comments written between the left-hand side and the operator
|
|
/// (`instr[6:0] /* opcode */ = ...;`). Kept here so they print on
|
|
/// the operator's left instead of being lost.
|
|
op_comments: Vec<Comment>,
|
|
/// Comments written between the operator and the right-hand side,
|
|
/// kept on the operator's line as a continuous assignment's are.
|
|
rhs_comments: Vec<Comment>,
|
|
/// Intra-assignment timing control (e.g. `#1`, `@(posedge clk)`).
|
|
timing: Option<TimingControl>,
|
|
rhs: Expr,
|
|
},
|
|
/// `force`/`release`/`deassign` statements, raw pieces.
|
|
ForceRelease {
|
|
kw: String,
|
|
lhs: Expr,
|
|
rhs: Option<Expr>,
|
|
},
|
|
/// Procedural timing control: `#10 stmt;`, `@(posedge clk) stmt;`,
|
|
/// `#10;`, `@(ev);`
|
|
Timing {
|
|
control: TimingControl,
|
|
stmt: Option<Box<Stmt>>,
|
|
},
|
|
Wait {
|
|
cond: Expr,
|
|
stmt: Option<Box<Stmt>>,
|
|
},
|
|
ExprStmt(Expr),
|
|
/// `-> event_name;`
|
|
EventTrigger(Expr),
|
|
VarDecl(DataDecl),
|
|
ParamDecl(ParamDecl),
|
|
/// Old-style task/function port declaration: `input [7:0] a;`
|
|
PortDecl(PortDecl),
|
|
Return(Option<Expr>),
|
|
Break,
|
|
Continue,
|
|
Disable(String),
|
|
Directive(RawText),
|
|
/// Macro invocation used as a statement.
|
|
MacroStmt(MacroCall),
|
|
/// A concurrent assertion written in statement position
|
|
/// (`assert property ( ... ) else $error("...");` inside an `initial`),
|
|
/// kept byte-for-byte. The statement-level twin of the assertion half of
|
|
/// [`ItemKind::Verbatim`]; see `docs/features/assertions/README.md`.
|
|
Verbatim(RawText),
|
|
/// Immediate assertion: kept simple — `assert (expr) stmt else stmt;`
|
|
Assert {
|
|
kw: String,
|
|
cond: Expr,
|
|
then: Option<Box<Stmt>>,
|
|
els: Option<Box<Stmt>>,
|
|
},
|
|
Null,
|
|
/// Source that could not be parsed as a statement, kept verbatim.
|
|
///
|
|
/// The statement-level twin of [`ItemKind::Error`]: produced only by
|
|
/// error-tolerant parsing, never seen by the formatter.
|
|
Error(RawText),
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum TimingControl {
|
|
/// `#expr`
|
|
Delay(Expr),
|
|
/// `@(...)`, `@*`
|
|
Event(SensList),
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct CaseItem {
|
|
pub meta: Meta,
|
|
/// Empty means `default`. Each label carries the comments written around
|
|
/// it, so a comment between two labels stays between them.
|
|
pub labels: Vec<ExprItem>,
|
|
pub stmt: Stmt,
|
|
/// Code extent of the case item, its body included.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct ForInit {
|
|
/// Declared type for `for (int i = 0; ...)`.
|
|
pub ty: Option<DataType>,
|
|
pub assign: SimpleAssign,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct SimpleAssign {
|
|
pub lhs: Expr,
|
|
pub op: String,
|
|
pub rhs: Expr,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum ForStep {
|
|
Assign(SimpleAssign),
|
|
Expr(Expr),
|
|
}
|
|
|
|
/// An expression: its code extent plus what it is.
|
|
///
|
|
/// The span sits on this wrapper rather than on each [`ExprKind`] variant so
|
|
/// that "the span of any expression" is one field access no matter which
|
|
/// variant it is — the same shape [`Item`] and [`Stmt`] already have.
|
|
#[derive(Debug, Clone)]
|
|
pub struct Expr {
|
|
pub kind: ExprKind,
|
|
/// Code extent of the expression.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub enum ExprKind {
|
|
Ident(String),
|
|
Number(String),
|
|
Str(String),
|
|
/// Explicitly empty (used for `.port()` connections), carrying any
|
|
/// comments written between the parens so they stay inside them.
|
|
Empty(Vec<Comment>),
|
|
Unary {
|
|
op: String,
|
|
expr: Box<Expr>,
|
|
},
|
|
Binary {
|
|
lhs: Box<Expr>,
|
|
op: String,
|
|
rhs: Box<Expr>,
|
|
/// Boxed to keep `Expr` small, as for `TernaryComments`.
|
|
comments: Box<BinaryComments>,
|
|
},
|
|
Ternary {
|
|
cond: Box<Expr>,
|
|
then: Box<Expr>,
|
|
els: Box<Expr>,
|
|
/// Boxed to keep `Expr` small: a ternary with comments in it is rare,
|
|
/// but every `Expr` would otherwise carry four empty vectors.
|
|
comments: Box<TernaryComments>,
|
|
},
|
|
Call {
|
|
callee: Box<Expr>,
|
|
args: Vec<ExprItem>,
|
|
/// Boxed to keep `Expr` small, as for `TernaryComments`: a call with
|
|
/// comments written around its brackets is rare, and there is one
|
|
/// `Call` in every other line of real code.
|
|
comments: Box<CallComments>,
|
|
},
|
|
/// A shallow copy, IEEE 1800 §8.12: `new instr_template[name]` (corpus
|
|
/// `ibex/vendor/google_riscv-dv/src/isa/riscv_instr.sv:238`). The payload
|
|
/// is the source handle — a postfix expression, never a call, because
|
|
/// `new(...)` and `new[N]` are the constructor and the dynamic-array
|
|
/// resize, which stay an ordinary `Call`/`Index` on the name `new`.
|
|
New(Box<Expr>),
|
|
Index {
|
|
base: Box<Expr>,
|
|
index: Box<Expr>,
|
|
},
|
|
Range {
|
|
base: Box<Expr>,
|
|
op: String,
|
|
msb: Box<Expr>,
|
|
lsb: Box<Expr>,
|
|
},
|
|
Member {
|
|
base: Box<Expr>,
|
|
name: String,
|
|
},
|
|
Scope {
|
|
base: Box<Expr>,
|
|
name: String,
|
|
},
|
|
/// A parameterized class reference: the `uvm_config_db#(virtual bus_if)`
|
|
/// of `uvm_config_db#(virtual bus_if)::get(...)`. The `#( ... )` is
|
|
/// verbatim, for the reason [`DataType::params`] gives.
|
|
Parameterized {
|
|
base: Box<Expr>,
|
|
params: RawText,
|
|
},
|
|
Concat(Vec<ExprItem>),
|
|
Repl {
|
|
count: Box<Expr>,
|
|
/// Elements carry their comments the way a concatenation's do, so a
|
|
/// comment inside a replication stays inside it.
|
|
inner: Vec<ExprItem>,
|
|
},
|
|
/// A streaming concatenation, IEEE 1800 §11.4.14:
|
|
/// `{<<8{v}}`, `{>>{a, b}}`, `{<< byte {x}}`. Legal as an lvalue too,
|
|
/// which is why it is an ordinary primary expression.
|
|
Stream {
|
|
/// `<<` or `>>`.
|
|
dir: String,
|
|
/// The optional slice size: a constant expression or a type.
|
|
slice: Option<Box<Expr>>,
|
|
/// The stream expressions, carrying their comments the way a
|
|
/// concatenation's elements do.
|
|
inner: Vec<ExprItem>,
|
|
},
|
|
Paren {
|
|
inner: Box<Expr>,
|
|
/// Comments written just inside the `(`.
|
|
before: Vec<Comment>,
|
|
/// Comments left over just before the `)`.
|
|
after: Vec<Comment>,
|
|
},
|
|
/// `ty'(expr)` cast. The casting type is an expression because it may be
|
|
/// a name (`int`, `my_t`), a package-scoped name
|
|
/// (`lc_ctrl_pkg::TxWidth'(x)`), a size (`4'(x)`), a parenthesized
|
|
/// constant expression (`(DepthW + 3)'(1'b1)`) or a system function call
|
|
/// (`$bits(x)'(y)`) — IEEE 1800 §6.24.1.
|
|
Cast {
|
|
ty: Box<Expr>,
|
|
expr: Box<Expr>,
|
|
/// True for the `ty'{...}` spelling (IEEE 1800 §10.9.1), where the
|
|
/// argument is an assignment pattern. The pattern brings its own
|
|
/// `'{` and `}`, so such a cast prints no brackets of its own —
|
|
/// which is also why the two spellings must stay distinguishable:
|
|
/// `ty'('{a: 1})` is a different token stream from `ty'{a: 1}`.
|
|
pattern: bool,
|
|
},
|
|
/// `'{...}` assignment pattern.
|
|
AssignPattern(Vec<PatternItem>),
|
|
/// The replication form of an assignment pattern, IEEE 1800 §10.9.1:
|
|
/// `'{NumRegs{0}}`. Distinct from [`ExprKind::Repl`], which is the
|
|
/// concatenation form `{N{x}}`.
|
|
AssignPatternRepl {
|
|
count: Box<Expr>,
|
|
inner: Vec<ExprItem>,
|
|
},
|
|
Postfix {
|
|
expr: Box<Expr>,
|
|
op: String,
|
|
},
|
|
Inside {
|
|
lhs: Box<Expr>,
|
|
ranges: Vec<ExprItem>,
|
|
},
|
|
/// `x inside `MACRO` — the set is a macro invocation that expands to a
|
|
/// `{...}` set, so it is kept verbatim (corpus
|
|
/// `opentitan/hw/ip/uart/dv/env/uart_scoreboard.sv:155`).
|
|
InsideMacro {
|
|
lhs: Box<Expr>,
|
|
/// The macro invocation token, exactly as written.
|
|
set: String,
|
|
},
|
|
/// `expr with (expr)` — the iterator argument of an array method
|
|
/// (IEEE 1800 §7.12): `fld.sum() with (item.bit_width)`. The argument is
|
|
/// an ordinary parenthesized expression, so `base` and `arg` are both
|
|
/// laid out structurally.
|
|
With {
|
|
base: Box<Expr>,
|
|
arg: Box<Expr>,
|
|
/// Boxed to keep `Expr` small, as for `TernaryComments`.
|
|
comments: Box<WithComments>,
|
|
},
|
|
/// `expr with { ... }` — the inline constraint block of a `randomize`
|
|
/// call (IEEE 1800 §18.7). The block is a `constraint` body written in an
|
|
/// expression, so it is kept byte-for-byte exactly as
|
|
/// [`ItemKind::Verbatim`]'s `constraint c { ... }` is.
|
|
WithConstraint {
|
|
base: Box<Expr>,
|
|
block: RawText,
|
|
/// Boxed to keep `Expr` small, as for `TernaryComments`.
|
|
comments: Box<WithComments>,
|
|
},
|
|
/// A named call argument: the `.ptr(ral.ovrd)` of
|
|
/// `csr_wr(.ptr(ral.ovrd), .value(x))`. `value` is [`ExprKind::Empty`]
|
|
/// for `.name()`, exactly as an empty connection is.
|
|
NamedArg {
|
|
name: String,
|
|
/// Byte range of `name`.
|
|
name_span: TextRange,
|
|
value: Box<Expr>,
|
|
/// Comments written between the value and the closing `)`
|
|
/// (`.src(5'd0 /* x0 */)`), printed back inside the parens — the
|
|
/// same slot a connection has.
|
|
close_comments: Vec<Comment>,
|
|
},
|
|
/// `[a:b]` inside an `inside` range list or value range.
|
|
ValueRange {
|
|
lo: Box<Expr>,
|
|
hi: Box<Expr>,
|
|
},
|
|
/// A type used in expression position (`parameter type T = logic [7:0]`).
|
|
Type(DataType),
|
|
/// `(min : typ : max)` delay expression.
|
|
MinTypMax {
|
|
min: Box<Expr>,
|
|
typ: Box<Expr>,
|
|
max: Box<Expr>,
|
|
},
|
|
}
|
|
|
|
/// Comments written next to a binary operator. Kept per side so neither
|
|
/// drifts across the operator: `a && // why` and `a // why` + `&& b` are
|
|
/// different token streams.
|
|
#[derive(Debug, Clone, Default)]
|
|
pub struct BinaryComments {
|
|
/// Between the left operand and the operator. These force the chain to
|
|
/// break, with the operator starting the new line.
|
|
pub before: Vec<Comment>,
|
|
/// Between the operator and the right operand. These force the chain to
|
|
/// break, with the right operand starting the new line.
|
|
pub after: Vec<Comment>,
|
|
}
|
|
|
|
/// Comments written inside a `cond ? then : els`, kept per position so the
|
|
/// formatter prints each back between the same two tokens it was written
|
|
/// between. Anywhere else they would drift across `?` or `:`.
|
|
#[derive(Debug, Clone, Default)]
|
|
pub struct TernaryComments {
|
|
/// Between the condition and `?`.
|
|
pub before_question: Vec<Comment>,
|
|
/// Between `?` and the then-branch.
|
|
pub after_question: Vec<Comment>,
|
|
/// Between the then-branch and `:`.
|
|
pub before_colon: Vec<Comment>,
|
|
/// Between `:` and the else-branch.
|
|
pub after_colon: Vec<Comment>,
|
|
}
|
|
|
|
/// Comments written where a call's argument list has no element to carry
|
|
/// them: before the `(`, and inside an empty `()`. Both are kept so the
|
|
/// formatter can print them back between the same two tokens — dropped or
|
|
/// moved, the safety net rejects the output.
|
|
#[derive(Debug, Clone, Default)]
|
|
pub struct CallComments {
|
|
/// Between the callee and the `(` (`x.randomize /*c*/ ()`).
|
|
pub before_open: Vec<Comment>,
|
|
/// Inside an empty argument list (`x.randomize(/*c*/)`). A list with
|
|
/// elements carries its own comments on them, so this is empty there.
|
|
pub dangling: Vec<Comment>,
|
|
}
|
|
|
|
impl CallComments {
|
|
/// True when nothing was written around the brackets — the common case.
|
|
pub fn is_empty(&self) -> bool {
|
|
self.before_open.is_empty() && self.dangling.is_empty()
|
|
}
|
|
}
|
|
|
|
/// Comments written around the `with` of a postfix `with (expr)` or
|
|
/// `with { ... }`, kept per position for the reason [`TernaryComments`] gives.
|
|
#[derive(Debug, Clone, Default)]
|
|
pub struct WithComments {
|
|
/// Between the base expression and `with`.
|
|
pub before_kw: Vec<Comment>,
|
|
/// Between `with` and the `(` or `{` that follows it.
|
|
pub after_kw: Vec<Comment>,
|
|
}
|
|
|
|
impl TernaryComments {
|
|
/// True when nothing was written inside the `? :` — the common case, laid
|
|
/// out without any comment handling.
|
|
pub fn is_empty(&self) -> bool {
|
|
self.before_question.is_empty()
|
|
&& self.after_question.is_empty()
|
|
&& self.before_colon.is_empty()
|
|
&& self.after_colon.is_empty()
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct PatternItem {
|
|
pub meta: Meta,
|
|
/// `key: value` (key may be `default`, a type, or an expression).
|
|
pub key: Option<Expr>,
|
|
pub value: Expr,
|
|
/// Code extent of the pattern element.
|
|
pub span: TextRange,
|
|
}
|
|
|
|
/// One element of a bracketed expression list (a concatenation, a call
|
|
/// argument list, an `inside` set), with the comments written around it. A
|
|
/// comment on an element is what forces its list to break one per line.
|
|
#[derive(Debug, Clone)]
|
|
pub struct ExprItem {
|
|
pub meta: Meta,
|
|
pub expr: Expr,
|
|
/// Code extent of the element.
|
|
pub span: TextRange,
|
|
}
|