335 lines
12 KiB
Rust
335 lines
12 KiB
Rust
//! Function, task and `let` declarations: qualifiers, the port list, the
|
|
//! body and its closing label.
|
|
|
|
use super::*;
|
|
|
|
/// A task/function body: its statements, the comments left dangling before
|
|
/// the closing keyword, and that keyword's own `: name` label.
|
|
type TfBody = (Vec<Stmt>, Vec<Comment>, Box<LabelComments>, Option<Name>);
|
|
|
|
impl<'a> Parser<'a> {
|
|
pub(super) fn parse_function(&mut self) -> PResult<FunctionDecl> {
|
|
self.parse_function_decl(None)
|
|
}
|
|
|
|
pub(super) fn parse_function_decl(&mut self, dpi: Option<DpiImport>) -> PResult<FunctionDecl> {
|
|
let prefix = self.take_member_prefix();
|
|
// `extern`, `pure virtual` and a DPI import all introduce a prototype:
|
|
// a header, a `;`, and no `endfunction`. The body is written elsewhere
|
|
// (in C for a DPI import, nowhere at all for a pure virtual).
|
|
let is_prototype =
|
|
dpi.is_some() || prefix.iter().any(|q| q.text == "extern" || q.text == "pure");
|
|
let TfPrototype {
|
|
after_kw,
|
|
qualifiers,
|
|
ret,
|
|
before_name,
|
|
scope,
|
|
name,
|
|
name_span,
|
|
has_parens,
|
|
before_args,
|
|
args,
|
|
args_dangling,
|
|
..
|
|
} = self.parse_tf_header(TfKind::Function, dpi.is_some())?;
|
|
let before_semi = self.gap_before(";");
|
|
self.expect_sym(";")?;
|
|
let (stmts, dangling, end_label_comments, end) = if is_prototype {
|
|
(Vec::new(), Vec::new(), Box::default(), None)
|
|
} else {
|
|
self.parse_tf_body("endfunction")?
|
|
};
|
|
let (end_label, end_label_span) = split_label(end);
|
|
// `endfunction : f;` — the stray `;` is a null class item
|
|
// (IEEE 1800 A.1.9). It is recorded here so the footer can print it
|
|
// back where it was written instead of on a line of its own.
|
|
let end_semi = !is_prototype && self.eat_sym(";");
|
|
Ok(FunctionDecl {
|
|
dpi,
|
|
prefix,
|
|
after_kw,
|
|
qualifiers,
|
|
is_prototype,
|
|
scope,
|
|
ret,
|
|
before_name,
|
|
name,
|
|
name_span,
|
|
has_parens,
|
|
before_args,
|
|
args,
|
|
args_dangling,
|
|
before_semi,
|
|
stmts,
|
|
dangling,
|
|
end_label_comments,
|
|
end_label,
|
|
end_label_span,
|
|
end_semi,
|
|
})
|
|
}
|
|
|
|
pub(super) fn parse_task(&mut self) -> PResult<TaskDecl> {
|
|
self.parse_task_decl(None)
|
|
}
|
|
|
|
pub(super) fn parse_task_decl(&mut self, dpi: Option<DpiImport>) -> PResult<TaskDecl> {
|
|
let prefix = self.take_member_prefix();
|
|
let is_prototype =
|
|
dpi.is_some() || prefix.iter().any(|q| q.text == "extern" || q.text == "pure");
|
|
let TfPrototype {
|
|
after_kw,
|
|
qualifiers,
|
|
before_name,
|
|
scope,
|
|
name,
|
|
name_span,
|
|
has_parens,
|
|
before_args,
|
|
args,
|
|
args_dangling,
|
|
..
|
|
} = self.parse_tf_header(TfKind::Task, dpi.is_some())?;
|
|
let before_semi = self.gap_before(";");
|
|
self.expect_sym(";")?;
|
|
let (stmts, dangling, end_label_comments, end) = if is_prototype {
|
|
(Vec::new(), Vec::new(), Box::default(), None)
|
|
} else {
|
|
self.parse_tf_body("endtask")?
|
|
};
|
|
let (end_label, end_label_span) = split_label(end);
|
|
// See `parse_function_decl`: a stray `;` after the footer.
|
|
let end_semi = !is_prototype && self.eat_sym(";");
|
|
Ok(TaskDecl {
|
|
dpi,
|
|
prefix,
|
|
after_kw,
|
|
qualifiers,
|
|
is_prototype,
|
|
scope,
|
|
before_name,
|
|
name,
|
|
name_span,
|
|
has_parens,
|
|
before_args,
|
|
args,
|
|
args_dangling,
|
|
before_semi,
|
|
stmts,
|
|
dangling,
|
|
end_label_comments,
|
|
end_label,
|
|
end_label_span,
|
|
end_semi,
|
|
})
|
|
}
|
|
|
|
/// `let name [( ports )] = expr;` (IEEE 1800 §11.12), corpus
|
|
/// `common_cells/test/clk_int_div_tb.sv:53`.
|
|
///
|
|
/// A `let` port is a task/function port without a direction — an optional
|
|
/// type, a name, optional dimensions and an optional default — so the
|
|
/// port list is read by [`Parser::parse_tf_ports`]. A `let` with no ports
|
|
/// writes no parens at all, which is what `has_parens` records.
|
|
pub(super) fn parse_let(&mut self) -> PResult<LetDecl> {
|
|
self.expect_kw("let")?;
|
|
let after_kw = self.take_gap();
|
|
let Name { text: name, span: name_span } = self.expect_name()?;
|
|
let has_parens = self.cur().is_sym("(");
|
|
let ports = self.parse_tf_ports(false)?.args;
|
|
let mut eq_comments = Box::new(BinaryComments::default());
|
|
eq_comments.before = self.gap_before("=");
|
|
self.expect_sym("=")?;
|
|
eq_comments.after = self.take_gap();
|
|
let value = self.parse_expr()?;
|
|
let before_semi = self.gap_before(";");
|
|
self.expect_sym(";")?;
|
|
Ok(LetDecl {
|
|
after_kw,
|
|
name,
|
|
name_span,
|
|
has_parens,
|
|
ports,
|
|
eq_comments,
|
|
value,
|
|
before_semi,
|
|
})
|
|
}
|
|
|
|
/// The header a subroutine declaration and a `modport` method prototype
|
|
/// (IEEE 1800 §25.5.4) are both written as: the keyword, the lifetime
|
|
/// qualifiers, a function's return type, the name and the port list. It
|
|
/// stops at the `)` of that list, so the caller owns whatever ends the
|
|
/// construct — a `;` for a declaration, a `,` or `)` for a prototype.
|
|
///
|
|
/// `allow_unnamed` is the DPI rule (§35.5.5): only an imported prototype's
|
|
/// formal may be written without a name.
|
|
pub(super) fn parse_tf_header(
|
|
&mut self,
|
|
kind: TfKind,
|
|
allow_unnamed: bool,
|
|
) -> PResult<TfPrototype> {
|
|
self.expect_kw(kind.keyword())?;
|
|
let after_kw = self.take_gap();
|
|
let mut qualifiers = Vec::new();
|
|
while self.cur().is_kw("automatic") || self.cur().is_kw("static") {
|
|
let text = self.bump().text;
|
|
qualifiers.push(Qualifier { text, after: self.take_gap() });
|
|
}
|
|
// Return type: everything up to the identifier followed by `(` or `;`.
|
|
// A task has none at all.
|
|
let ret = match kind {
|
|
TfKind::Task => None,
|
|
TfKind::Function => {
|
|
let t = self.cur();
|
|
if t.kind == TokenKind::Ident
|
|
&& (self.nth(1).is_sym("(") || self.nth(1).is_sym(";"))
|
|
{
|
|
None
|
|
} else {
|
|
let ty = self.parse_opt_data_type()?;
|
|
if ty.is_implicit() {
|
|
None
|
|
} else {
|
|
Some(ty)
|
|
}
|
|
}
|
|
}
|
|
};
|
|
// Out-of-body definitions of an `extern` method write the class name
|
|
// in front: `function void my_class::configure(...)`.
|
|
let before_name = self.take_gap();
|
|
let (scope, Name { text: name, span: name_span }) = self.parse_method_name()?;
|
|
let has_parens = self.cur().is_sym("(");
|
|
let before_args = self.take_before_open("(");
|
|
let TfPorts { args, dangling: args_dangling } = self.parse_tf_ports(allow_unnamed)?;
|
|
Ok(TfPrototype {
|
|
kind,
|
|
after_kw,
|
|
qualifiers,
|
|
ret,
|
|
before_name,
|
|
scope,
|
|
name,
|
|
name_span,
|
|
has_parens,
|
|
before_args,
|
|
args,
|
|
args_dangling,
|
|
})
|
|
}
|
|
|
|
/// The `( ... )` of a subroutine's formals. `allow_unnamed` is the DPI
|
|
/// rule; see [`Parser::parse_tf_header`] and [`TfArg::name`].
|
|
pub(super) fn parse_tf_ports(&mut self, allow_unnamed: bool) -> PResult<TfPorts> {
|
|
let mut args = Vec::new();
|
|
if !self.cur().is_sym("(") {
|
|
return Ok(TfPorts::default());
|
|
}
|
|
self.skip();
|
|
if self.cur().is_sym(")") {
|
|
// Only what was written *inside* the empty `()`, as an empty
|
|
// argument list of a call keeps.
|
|
let dangling = self.take_gap();
|
|
self.skip();
|
|
return Ok(TfPorts { args, dangling });
|
|
}
|
|
let mark = self.diags_mark();
|
|
loop {
|
|
let at = self.pos;
|
|
args.push(self.element_or_recover(|p| p.parse_tf_arg(allow_unnamed))?);
|
|
let recovered = args.last().is_some_and(ListElem::is_error);
|
|
let more;
|
|
if let Some(ListElem::Ok(last)) = args.last_mut() {
|
|
self.take_before_sep(&mut last.meta, ",");
|
|
more = self.eat_sym(",");
|
|
last.meta.trailing = self.take_trailing();
|
|
} else {
|
|
more = self.eat_sym(",");
|
|
}
|
|
if self.list_ends(at, more, false, recovered) {
|
|
break;
|
|
}
|
|
}
|
|
if let Some(ListElem::Ok(last)) = args.last_mut() {
|
|
self.take_before_sep(&mut last.meta, ")");
|
|
}
|
|
self.close_list(mark)?;
|
|
Ok(TfPorts { args, dangling: Vec::new() })
|
|
}
|
|
|
|
/// One formal of a subroutine's port list: everything up to the `,` or
|
|
/// `)` that follows it, which the list itself reads.
|
|
fn parse_tf_arg(&mut self, allow_unnamed: bool) -> PResult<TfArg> {
|
|
let start = self.here();
|
|
let meta = self.take_meta();
|
|
let mut after_const = Vec::new();
|
|
let dir = {
|
|
let t = self.cur();
|
|
if t.is_kw("const") && self.nth(1).is_kw("ref") {
|
|
// `const ref` (IEEE 1800 §13.5.2), corpus
|
|
// `ibex/vendor/lowrisc_ip/dv/sv/dv_utils/dv_utils_pkg.sv:104`.
|
|
// Two keywords, so the gap between them is a slot of its own —
|
|
// no other direction has an inside.
|
|
self.skip();
|
|
after_const = self.take_gap();
|
|
self.skip();
|
|
Some("const ref".to_string())
|
|
} else if t.is_kw("input") || t.is_kw("output") || t.is_kw("inout") || t.is_kw("ref") {
|
|
Some(self.bump().text)
|
|
} else {
|
|
None
|
|
}
|
|
};
|
|
let after_dir = if dir.is_some() { self.take_gap() } else { Vec::new() };
|
|
let ty = self.parse_opt_data_type()?;
|
|
let before_name = self.take_gap();
|
|
// A DPI import's formal may name its type and stop (§35.5.5); every
|
|
// other formal must be named. The empty name is the crate's convention
|
|
// for "written without one" — see [`TfArg::name`].
|
|
let unnamed = allow_unnamed && (self.cur().is_sym(",") || self.cur().is_sym(")"));
|
|
let Name { text: name, span: name_span } = if unnamed {
|
|
Name { text: String::new(), span: TextRange::empty(self.span_from(start).end) }
|
|
} else {
|
|
self.expect_name()?
|
|
};
|
|
let unpacked = self.parse_dims()?;
|
|
let mut eq_comments = BinaryComments::default();
|
|
let default = if self.cur().is_sym("=") {
|
|
eq_comments.before = self.take_gap();
|
|
self.skip();
|
|
eq_comments.after = self.take_gap();
|
|
Some(self.parse_expr()?)
|
|
} else {
|
|
None
|
|
};
|
|
let span = self.span_from(start);
|
|
Ok(TfArg {
|
|
meta,
|
|
dir,
|
|
after_const,
|
|
after_dir,
|
|
ty,
|
|
before_name,
|
|
name,
|
|
name_span,
|
|
unpacked,
|
|
eq_comments: Box::new(eq_comments),
|
|
default,
|
|
span,
|
|
})
|
|
}
|
|
|
|
fn parse_tf_body(&mut self, end_kw: &str) -> PResult<TfBody> {
|
|
let mut stmts = Vec::new();
|
|
while !self.at_body_end(end_kw) {
|
|
stmts.push(self.stmt_or_recover()?);
|
|
}
|
|
let dangling = self.take_dangling();
|
|
self.expect_closer(end_kw)?;
|
|
let (comments, name) = self.parse_label_with()?;
|
|
Ok((stmts, dangling, comments, name))
|
|
}
|
|
}
|