//! Functions and tasks: the argument list a `function`, `task`, //! `covergroup` and `property` share (`tf_args`), the two declarations //! with their prototype forms, `let`, and the DPI `import`/`export` //! linkage. use crate::doc::*; use vlog_parser::ast::*; use vlog_parser::token::Comment; use super::list::ListItem; use super::{join_paths, name_doc, named, Fmt}; /// The pieces of a subroutine header that a real declaration and a `modport` /// method prototype (IEEE 1800 §25.5.4) print identically: the keyword through /// the `)` of the port list. /// /// Borrowed rather than owned, so [`FunctionDecl`], [`TaskDecl`] and /// [`TfPrototype`] — which keep the same pieces in three different shapes — /// all reach one printer without a clone. pub(super) struct TfHeader<'h> { kw: &'h str, after_kw: &'h [Comment], qualifiers: &'h [Qualifier], ret: Option<&'h DataType>, before_name: &'h [Comment], scope: &'h [ScopeSeg], name: &'h str, has_parens: bool, before_args: &'h [Comment], args: &'h [ListElem], args_dangling: &'h [Comment], } impl<'h> TfHeader<'h> { /// The header of a `function` declaration; its `dpi` and `prefix` are the /// caller's, since they stand in front of the keyword. fn of_function(f: &'h FunctionDecl) -> Self { TfHeader { kw: "function", after_kw: &f.after_kw, qualifiers: &f.qualifiers, ret: f.ret.as_ref(), before_name: &f.before_name, scope: &f.scope, name: &f.name, has_parens: f.has_parens, before_args: &f.before_args, args: &f.args, args_dangling: &f.args_dangling, } } /// The header of a `task` declaration, which has no return type. fn of_task(t: &'h TaskDecl) -> Self { TfHeader { kw: "task", after_kw: &t.after_kw, qualifiers: &t.qualifiers, ret: None, before_name: &t.before_name, scope: &t.scope, name: &t.name, has_parens: t.has_parens, before_args: &t.before_args, args: &t.args, args_dangling: &t.args_dangling, } } /// The header a modport method prototype is written as. pub(super) fn of_prototype(p: &'h TfPrototype) -> Self { TfHeader { kw: p.kind.keyword(), after_kw: &p.after_kw, qualifiers: &p.qualifiers, ret: p.ret.as_ref(), before_name: &p.before_name, scope: &p.scope, name: &p.name, has_parens: p.has_parens, before_args: &p.before_args, args: &p.args, args_dangling: &p.args_dangling, } } } impl<'a> Fmt<'a> { /// The keyword through the `)` of the port list, appended to `docs`. pub(super) fn tf_header(&self, h: &TfHeader, docs: &mut Vec) { docs.push(text(h.kw.to_string())); if h.after_kw.is_empty() { docs.push(text(" ")); } else { self.inline_comments_spaced(h.after_kw, docs); } self.qualifier_run(h.qualifiers, docs); let named_ret = match h.ret.and_then(|r| self.data_type(r)) { Some(t) => { docs.push(t); true } None => false, }; if named_ret { if h.before_name.is_empty() { docs.push(text(" ")); } else { self.inline_comments_spaced(h.before_name, docs); } } else { self.slot_comments(h.before_name, docs); } self.scope_prefix(h.scope, docs); docs.push(name_doc(h.name)); if h.has_parens { self.slot_comments(h.before_args, docs); docs.push(self.tf_args(h.args, h.args_dangling)); } } pub(super) fn tf_args(&self, args: &[ListElem], dangling: &[Comment]) -> Doc { let items: Vec = args .iter() .map(|a| self.list_elem(a, |a| ListItem::from_meta(&a.meta, self.tf_arg(a)))) .collect(); self.delimited_dangling("(", items, dangling, ")", false) } /// One formal: `input logic [7:0] a = 0`. /// /// An **unnamed** formal — a DPI import's, the only one the LRM allows /// (§35.5.5) — is its direction and type and nothing more, so every /// separator that would have stood in front of the name is left out and /// the missing name takes no width. fn tf_arg(&self, a: &TfArg) -> Doc { let mut docs = Vec::new(); let ty = self.data_type(&a.ty); if let Some(dir) = &a.dir { self.tf_arg_dir(dir, &a.after_const, &mut docs); if !a.after_dir.is_empty() { self.inline_comments_spaced(&a.after_dir, &mut docs); } else if ty.is_some() || !a.name.is_empty() { docs.push(text(" ")); } } let typed = ty.is_some(); docs.extend(ty); if a.name.is_empty() { if self.inline_comments(&a.before_name, &mut docs) { docs.push(Doc::HardLine); } return concat(docs); } if typed { if a.before_name.is_empty() { docs.push(text(" ")); } else { self.inline_comments_spaced(&a.before_name, &mut docs); } } else { self.slot_comments(&a.before_name, &mut docs); } docs.push(name_doc(&a.name)); docs.extend(self.unpacked_dims(&a.unpacked)); if let Some(d) = &a.default { self.slot_comments(&a.eq_comments.before, &mut docs); docs.push(text(if a.eq_comments.before.is_empty() { " =" } else { "=" })); if a.eq_comments.after.is_empty() { docs.push(text(" ")); } else { self.inline_comments_spaced(&a.eq_comments.after, &mut docs); } docs.push(self.expr(d)); } concat(docs) } /// A formal's direction. `const ref` is the one written as two keywords, /// so it is the one with a comment slot inside it (IEEE 1800 §13.5.2). fn tf_arg_dir(&self, dir: &str, after_const: &[Comment], docs: &mut Vec) { if after_const.is_empty() { docs.push(text(dir.to_string())); return; } docs.push(text("const")); self.inline_comments_spaced(after_const, docs); docs.push(text("ref")); } pub(super) fn function(&self, f: &FunctionDecl) -> Doc { let mut header = Vec::new(); if let Some(d) = &f.dpi { header.push(text(dpi_import_prefix(d))); } self.qualifier_run(&f.prefix, &mut header); self.tf_header(&TfHeader::of_function(f), &mut header); header.push(self.semi(&f.before_semi)); // A prototype (`extern`/`pure virtual`) is the header and nothing // else: no body, no `endfunction`. if f.is_prototype { return concat(header); } let mut body = Vec::new(); if !f.stmts.is_empty() { body.push(Doc::HardLine); body.extend(self.stmt_seq(&f.stmts)); } body.extend(self.dangling(&f.dangling)); concat(vec![ concat(header), indent(body), Doc::HardLine, self.closing_labeled("endfunction", &f.end_label, &f.end_label_comments), text(if f.end_semi { ";" } else { "" }), ]) } pub(super) fn task(&self, t: &TaskDecl) -> Doc { let mut header = Vec::new(); if let Some(d) = &t.dpi { header.push(text(dpi_import_prefix(d))); } self.qualifier_run(&t.prefix, &mut header); self.tf_header(&TfHeader::of_task(t), &mut header); header.push(self.semi(&t.before_semi)); if t.is_prototype { return concat(header); } let mut body = Vec::new(); if !t.stmts.is_empty() { body.push(Doc::HardLine); body.extend(self.stmt_seq(&t.stmts)); } body.extend(self.dangling(&t.dangling)); concat(vec![ concat(header), indent(body), Doc::HardLine, self.closing_labeled("endtask", &t.end_label, &t.end_label_comments), text(if t.end_semi { ";" } else { "" }), ]) } /// `let max(a, b) = (a > b) ? a : b;` (IEEE 1800 §11.12) — a name, an /// optional port list laid out like a function's, and one expression. pub(super) fn let_decl(&self, l: &LetDecl) -> Doc { let mut docs = vec![text(format!("let {}", named(&l.name)))]; if l.has_parens { docs.push(self.tf_args(&l.ports, &[])); } docs.push(text(" =")); docs.push(self.assign_rhs(self.expr(&l.value))); concat(vec![group(docs), text(";")]) } /// `export "DPI-C" function f;` / `export pkg::*;` — one line either way; /// no list here is long enough to be worth a breakable group. pub(super) fn export(e: &ExportDecl) -> Doc { match e { ExportDecl::Dpi(d) => { let mut head = format!("export {} ", d.spec); if let Some(c) = &d.c_identifier { head.push_str(&named(&c.text)); head.push_str(" = "); } text(format!("{head}{} {};", d.kw, named(&d.name.text))) } ExportDecl::Package(paths) => text(format!("export {};", join_paths(paths))), } } } /// `import "DPI-C" [context|pure ][c_name = ]` — the linkage prefix a DPI /// import writes in front of the function or task prototype it declares. fn dpi_import_prefix(d: &DpiImport) -> String { let mut s = format!("import {} ", d.spec); if let Some(p) = &d.property { s.push_str(p); s.push(' '); } if let Some(c) = &d.c_identifier { s.push_str(&named(&c.text)); s.push_str(" = "); } s }