//! Data types, declarators, typedefs, enums, structs, and function/task/let //! declarations. use super::*; #[test] fn typedefs() { parse_ok( "typedef enum logic [1:0] {IDLE, RUN = 2, DONE} state_t; typedef struct packed { logic [7:0] addr; logic valid; } req_t; typedef logic [31:0] word_t;", ); } #[test] fn functions_tasks() { parse_ok( "module f; function automatic logic [7:0] add1(input logic [7:0] x); return x + 1; endfunction task do_thing(input int n); repeat (n) @(posedge clk); endtask endmodule", ); } #[test] fn a_parameterized_type_is_a_declaration_and_a_parameterized_module_is_an_instance() { let f = parse_ok("module m;\n port_t #(bit [3:0]) p;\n sub #(.W(8)) u (.a(a));\nendmodule\n"); let ItemKind::Module(m) = &f.items[0].kind else { panic!("expected a module") }; let ItemKind::DataDecl(d) = &m.items[0].kind else { panic!("expected a declaration") }; assert_eq!(d.ty.name, "port_t"); assert_eq!(d.ty.params().expect("type parameters").text, "#(bit [3:0])"); assert!(matches!(m.items[1].kind, ItemKind::Instantiation(_))); } #[test] fn a_typedef_can_forward_declare_a_class() { let f = parse_ok("typedef class c;\n"); let ItemKind::Typedef(t) = &f.items[0].kind else { panic!("expected a typedef") }; assert_eq!(t.name, "c"); assert!(matches!(t.kind, TypedefKind::ForwardClass)); } // ------------------------------------------------------------------ // Dynamic types (queues, dynamic/associative arrays, named args) // ------------------------------------------------------------------ fn first_decl(file: &SourceFile) -> &DataDecl { let ItemKind::DataDecl(d) = &file.items[0].kind else { panic!("expected a declaration") }; d } #[test] fn a_queue_dim_is_a_range_whose_bound_is_a_dollar_ident() { let f = parse_ok("module m;\n string q[$];\nendmodule\n"); let ItemKind::Module(m) = &f.items[0].kind else { panic!("expected a module") }; let ItemKind::DataDecl(d) = &m.items[0].kind else { panic!("expected a declaration") }; let [dim] = &d.declarators[0].unpacked[..] else { panic!("expected one dim") }; let DimKind::Range { a, b } = &dim.kind else { panic!("expected a range dim") }; assert!(matches!(&a.kind, ExprKind::Ident(n) if n == "$")); assert!(b.is_none()); } #[test] fn a_bounded_queue_keeps_its_bound() { let f = parse_ok("int q[$:8];\n"); let [dim] = &first_decl(&f).declarators[0].unpacked[..] else { panic!("one dim") }; let DimKind::Range { a, b } = &dim.kind else { panic!("expected a range dim") }; assert!(matches!(&a.kind, ExprKind::Ident(n) if n == "$")); assert!(matches!(&b.as_ref().expect("bound").kind, ExprKind::Number(n) if n == "8")); } #[test] fn a_dynamic_array_dim_is_empty_and_a_wildcard_dim_is_a_star() { let f = parse_ok("int a[];\nlogic b[*];\n"); let [dim] = &first_decl(&f).declarators[0].unpacked[..] else { panic!("one dim") }; assert!(matches!(dim.kind, DimKind::Dynamic)); let ItemKind::DataDecl(d) = &f.items[1].kind else { panic!("expected a declaration") }; assert!(matches!(d.declarators[0].unpacked[0].kind, DimKind::Wildcard)); } #[test] fn a_multi_dim_declarator_may_end_in_a_queue_or_dynamic_dim() { let f = parse_ok("payload_t q[NumInp-1:0][$];\nseq_t s[NUM_HARTS][];\n"); let dims = &first_decl(&f).declarators[0].unpacked; assert_eq!(dims.len(), 2); assert!(matches!(&dims[0].kind, DimKind::Range { b: Some(_), .. })); assert!(matches!(&dims[1].kind, DimKind::Range { a, b: None } if matches!(&a.kind, ExprKind::Ident(n) if n == "$"))); let ItemKind::DataDecl(d) = &f.items[1].kind else { panic!("expected a declaration") }; assert!(matches!(d.declarators[0].unpacked[1].kind, DimKind::Dynamic)); } #[test] fn a_parameterized_type_with_a_queue_declarator_is_a_declaration_not_an_instance() { let f = parse_ok("class c;\n item_t #(A, B) q[$];\nendclass\n"); let c = class_of(&f); let ItemKind::DataDecl(d) = &c.items[0].kind else { panic!("expected a declaration") }; assert_eq!(d.ty.name, "item_t"); assert!(d.ty.params().is_some()); assert_eq!(d.declarators[0].name, "q"); } #[test] fn a_union_may_be_tagged() { let f = parse_ok("typedef union tagged packed {\n logic [7:0] v;\n} u_t;\n"); let ItemKind::Typedef(t) = &f.items[0].kind else { panic!("expected a typedef") }; let TypedefKind::Struct(s) = &t.kind else { panic!("expected a union") }; assert!(s.tagged); assert!(s.packed); // And a plain one is not. let f = parse_ok("typedef union packed {\n logic [7:0] v;\n} u_t;\n"); let ItemKind::Typedef(t) = &f.items[0].kind else { panic!("expected a typedef") }; let TypedefKind::Struct(s) = &t.kind else { panic!("expected a union") }; assert!(!s.tagged); } #[test] fn a_virtual_interface_type_stands_in_every_type_position() { // corpus/ibex/dv/uvm/icache/dv/env/ibex_icache_env_pkg.sv:28 (typedef) // and corpus/common_cells/test/stream_test.sv:31 (argument). let f = parse_ok( "package p;\n typedef virtual push_pull_if #(.W(4)) vif_t;\n function void f(virtual interface bus_if h);\n endfunction\nendpackage\n", ); let ItemKind::Package { items, .. } = &f.items[0].kind else { panic!("expected a package") }; let ItemKind::Typedef(t) = &items[0].kind else { panic!("expected a typedef") }; let TypedefKind::Alias(ty) = &t.kind else { panic!("expected a type alias") }; assert_eq!(ty.virt, Some(VirtualKw::Bare)); assert_eq!(ty.name, "push_pull_if"); let ItemKind::Function(fun) = &items[1].kind else { panic!("expected a function") }; assert_eq!(oks(&fun.args)[0].ty.virt, Some(VirtualKw::Interface)); assert_eq!(oks(&fun.args)[0].ty.name, "bus_if"); } #[test] fn an_anonymous_enum_declares_a_variable() { // corpus/common_cells/src/deprecated/clock_divider.sv:59 let f = parse_ok("module m;\n enum logic [1:0] {IDLE, STOP} state, state_next;\nendmodule\n"); let ItemKind::Module(m) = &f.items[0].kind else { panic!("expected a module") }; let ItemKind::DataDecl(d) = &m.items[0].kind else { panic!("expected a declaration") }; let Some(TypeExtra::Enum(e)) = d.ty.inline() else { panic!("expected an inline enum") }; assert_eq!(e.members.len(), 2); assert_eq!(e.base.as_ref().map(|b| b.name.as_str()), Some("logic")); assert_eq!(d.declarators.len(), 2); } #[test] fn an_anonymous_struct_declares_a_variable() { let f = parse_ok("module m;\n struct packed {logic a;} s;\nendmodule\n"); let ItemKind::Module(m) = &f.items[0].kind else { panic!("expected a module") }; let ItemKind::DataDecl(d) = &m.items[0].kind else { panic!("expected a declaration") }; let Some(TypeExtra::Struct(st)) = d.ty.inline() else { panic!("expected an inline struct") }; assert_eq!(st.kw, "struct"); assert!(st.packed); assert_eq!(d.declarators[0].name, "s"); } #[test] fn a_struct_member_may_itself_be_an_inline_aggregate() { // corpus/opentitan/hw/ip/uart/rtl/uart_reg_pkg.sv:30 let f = parse_ok( "package p;\n typedef struct packed {\n struct packed {logic q;} tx;\n } r_t;\nendpackage\n", ); let ItemKind::Package { items, .. } = &f.items[0].kind else { panic!("expected a package") }; let ItemKind::Typedef(t) = &items[0].kind else { panic!("expected a typedef") }; let TypedefKind::Struct(st) = &t.kind else { panic!("expected a struct") }; assert!(matches!(st.members[0].ty.inline(), Some(TypeExtra::Struct(_)))); } #[test] fn an_enum_base_type_may_be_a_typedef_name() { // corpus/ibex/vendor/lowrisc_ip/ip/prim/rtl/prim_count_pkg.sv:10 let f = parse_ok("package p;\n typedef enum action_mask_t {Clr = 4'h1} action_e;\nendpackage\n"); let ItemKind::Package { items, .. } = &f.items[0].kind else { panic!("expected a package") }; let ItemKind::Typedef(t) = &items[0].kind else { panic!("expected a typedef") }; let TypedefKind::Enum(e) = &t.kind else { panic!("expected an enum") }; assert_eq!(e.base.as_ref().map(|b| b.name.as_str()), Some("action_mask_t")); } #[test] fn packed_dimensions_may_follow_a_typedef_type_name() { // corpus/common_cells/src/stream_xbar.sv:98 let f = parse_ok("module m;\n payload_t [A-1:0][B-1:0] out_data;\nendmodule\n"); let ItemKind::Module(m) = &f.items[0].kind else { panic!("expected a module") }; let ItemKind::DataDecl(d) = &m.items[0].kind else { panic!("expected a declaration") }; assert_eq!(d.ty.name, "payload_t"); assert_eq!(d.ty.packed.len(), 2); assert_eq!(d.declarators[0].name, "out_data"); } #[test] fn an_indexed_assignment_is_not_a_declaration() { let f = parse_ok("module m;\n always_comb begin\n x[i] = 1;\n end\nendmodule\n"); let ItemKind::Module(m) = &f.items[0].kind else { panic!("expected a module") }; let ItemKind::Always { stmt, .. } = &m.items[0].kind else { panic!("expected an always") }; let StmtKind::Block { stmts, .. } = &stmt.kind else { panic!("expected a block") }; assert!( matches!(stmts[0].kind, StmtKind::Assign { .. }), "expected an assignment, got {:?}", stmts[0].kind ); } #[test] fn a_type_parameter_may_be_declared_as_an_item() { // corpus/common_cells/test/lossy_valid_to_stream_tb.sv:26 let f = parse_ok("module m;\n localparam type payload_t = logic [7:0];\nendmodule\n"); let ItemKind::Module(m) = &f.items[0].kind else { panic!("expected a module") }; let ItemKind::ParamDecl(d) = &m.items[0].kind else { panic!("expected a parameter") }; assert!(d.is_type); assert!(matches!(d.declarators[0].init.as_ref().map(|e| &e.kind), Some(ExprKind::Type(_)))); } #[test] fn an_unterminated_anonymous_enum_is_an_error() { assert!( parse_err("module m;\n enum logic [1:0] {A, B state;\nendmodule\n").contains("expected") ); } #[test] fn a_let_declaration_keeps_its_ports_and_its_value() { let f = parse_ok("module m;\n let max(a, b) = (a > b) ? a : b;\nendmodule\n"); let ItemKind::Let(l) = &module_of(&f).items[0].kind else { panic!("expected a let") }; assert_eq!(l.name, "max"); assert!(l.has_parens); assert_eq!(l.ports.len(), 2); assert_eq!(oks(&l.ports)[1].name, "b"); assert!(matches!(l.value.kind, ExprKind::Ternary { .. })); } #[test] fn an_enum_member_may_carry_a_name_range() { // hw/top_earlgrey/ip_autogen/rstmgr/rtl/rstmgr_pkg.sv:101 (`[hi:lo]`) // and hw/top_earlgrey/dv/env/chip_env_pkg.sv:88 (`[N]`). let f = parse_ok("typedef enum {A[0:1], B, C[16] = 5} e_t;\n"); let ItemKind::Typedef(t) = &f.items[0].kind else { panic!("expected a typedef") }; let TypedefKind::Enum(e) = &t.kind else { panic!("expected an enum") }; assert!(matches!( e.members[0].range.as_slice(), [Dim { kind: DimKind::Range { b: Some(_), .. }, .. }] )); assert!(e.members[1].range.is_empty(), "a plain member has no range"); assert!(matches!( e.members[2].range.as_slice(), [Dim { kind: DimKind::Range { b: None, .. }, .. }] )); assert!(e.members[2].value.is_some(), "the range does not swallow the `= 5`"); } #[test] fn a_virtual_interface_type_may_name_a_modport() { // hw/ip/spi_device/pre_dv/tb/spid_jedec_tb.sv:32 (a declaration) and // .../spid_common.sv:563 (a task argument). let f = parse_ok("module m;\n virtual spi_if.tb tb_sif = sif.tb;\nendmodule\n"); let m = module_of(&f); let ItemKind::DataDecl(d) = &m.items[0].kind else { panic!("expected a declaration") }; assert_eq!(words(&d.qualifiers), ["virtual"]); assert_eq!(d.ty.name, "spi_if.tb"); assert_eq!(d.declarators[0].name, "tb_sif"); let f = parse_ok("module m;\n task t(virtual spi_if.tb sif);\n endtask\nendmodule\n"); let m = module_of(&f); let ItemKind::Task(t) = &m.items[0].kind else { panic!("expected a task") }; assert_eq!(oks(&t.args)[0].ty.name, "spi_if.tb"); assert!(oks(&t.args)[0].ty.virt.is_some(), "still a virtual interface type"); } #[test] fn a_dot_after_a_plain_type_name_is_still_not_a_modport() { // The widening is scoped to `virtual`, which is what keeps a `.` // after an identifier reading as a member access everywhere else // (docs/features/ansi-first-port/README.md, Status). assert!(parse_err("module m;\n bus_if.mp v;\nendmodule\n").contains("expected")); } #[test] fn a_struct_member_may_be_declared_rand() { // hw/ip/spi_host/dv/env/spi_host_env_pkg.sv:64 — not LRM-legal, but // real, vendor-accepted code. let f = parse_ok("typedef struct {\n rand bit cpol;\n randc int n;\n bit p;\n} c_t;\n"); let ItemKind::Typedef(t) = &f.items[0].kind else { panic!("expected a typedef") }; let TypedefKind::Struct(s) = &t.kind else { panic!("expected a struct") }; assert_eq!(words(&s.members[0].qualifiers), ["rand"]); assert_eq!(words(&s.members[1].qualifiers), ["randc"]); assert!(s.members[2].qualifiers.is_empty()); assert_eq!(s.members[0].declarators[0].name, "cpol"); }