Add regression tests for forward typedefs

Check that all sorts of forward typedefs are supported.
Also check that any recursive use of a type results in an error.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
This commit is contained in:
Lars-Peter Clausen 2022-09-15 11:41:40 +02:00
parent cdc9629ce7
commit 3787eca248
12 changed files with 329 additions and 0 deletions

View File

@ -0,0 +1,47 @@
// Check that forward typedefs of basic types are supported
`define check(val, exp) \
if (val != exp) begin \
$display("FAILED(%0d). '%s' expected ", `__LINE__, `"val`", exp, " got ", val, ); \
failed = 1'b1; \
end
bit failed = 1'b0;
module test;
typedef T1;
typedef T1; // Check forward typedef twice for the same type
typedef T2;
typedef T3;
typedef T4;
T1 x = -1;
T2 y = 1.23;
T3 z = "Hello";
T4 w;
typedef integer T1;
// There can be as many forward typedefs as we like, even after the type
// itself has already been declared.
typedef T1;
typedef T1;
typedef real T2;
typedef string T3;
typedef logic [1:0] T4[3:0];
initial begin
`check($bits(x), $bits(integer))
`check($bits(T1), $bits(integer))
`check(x, -1)
`check(y, 1.23)
`check(z, "Hello")
`check($unpacked_dimensions(w), 1)
`check($size(w), 4)
if (!failed) begin
$display("PASSED");
end
end
endmodule

View File

@ -0,0 +1,46 @@
// Check that forward typedefs of classes are supported
module test;
`define check(val, exp) \
if (val !== exp) begin \
$display("FAILED(%0d). '%s' expected %d, got %d", `__LINE__, `"val`", exp, val); \
failed = 1'b1; \
end
bit failed = 1'b0;
typedef class C;
typedef C;
C x;
class C;
int x;
endclass
C y;
// There can be as many forward typedefs as we like, even after the type
// itself has already been declared.
typedef C;
typedef class C;
C z;
initial begin
// Check they are all the same type and can be assigned to each other
x = y;
y = z;
z = x;
`check($bits(x.x), $bits(int));
`check($bits(y.x), $bits(int));
`check($bits(z.x), $bits(int));
if (!failed) begin
$display("PASSED");
end
end
endmodule

View File

@ -0,0 +1,44 @@
// Check that forward enum typedefs are supported
module test;
`define check(val, exp) \
if (val !== exp) begin \
$display("FAILED(%0d). '%s' expected %0d, got %0d", `__LINE__, `"val`", exp, val); \
failed = 1'b1; \
end
bit failed = 1'b0;
typedef T;
typedef enum T;
T x;
typedef enum integer {
A, B
} T;
T y;
typedef enum T;
typedef T;
T z;
initial begin
// Check that they are all the same type and can be assigned to each other
x = y;
y = z;
z = x;
`check($bits(x), $bits(integer))
`check($bits(y), $bits(integer))
`check($bits(z), $bits(integer))
if (!failed) begin
$display("PASSED");
end
end
endmodule

View File

@ -0,0 +1,23 @@
// Check that the base type of an enum can be a forward typedef
module test;
typedef T1;
typedef enum T1 {
A, B
} T2;
typedef logic [31:0] T1;
T2 z;
initial begin
if ($bits(z) == 32) begin
$display("PASSED");
end else begin
$display("FAILED");
end
end
endmodule

View File

@ -0,0 +1,26 @@
// Check that a forwarded enum typedef can be referenced in a class
module test;
typedef T;
class C;
T x;
endclass
typedef enum integer {
X, Y
} T;
initial begin
C c;
c = new;
if ($bits(c.x) == 32) begin
$display("PASSED");
end else begin
$display("FAILED");
end
end
endmodule

View File

@ -0,0 +1,14 @@
// Check that a enum can't be its own base type
module test;
typedef T;
typedef enum T {
A, B
} T;
initial begin
$display("FAILED");
end
endmodule

View File

@ -0,0 +1,39 @@
// Check that forward struct typedefs are supported
module test;
`define check(val, exp) \
if (val !== exp) begin \
$display("FAILED(%0d). '%s' expected %0d, got %0d", `__LINE__, `"val`", exp, val); \
failed = 1'b1; \
end
bit failed = 1'b0;
typedef T;
typedef struct T;
T x;
typedef struct packed {
int x;
} T;
T y;
typedef struct T;
typedef T;
T z;
initial begin
`check($bits(x), $bits(int))
`check($bits(y), $bits(int))
`check($bits(z), $bits(int))
if (!failed) begin
$display("PASSED");
end
end
endmodule

View File

@ -0,0 +1,18 @@
// Check that it is an error to use a forwarded struct type as the type for the
// member in the struct itself.
module test;
typedef T;
typedef struct packed {
T x;
} T;
T x;
initial begin
$display("FAILED");
end
endmodule

View File

@ -0,0 +1,41 @@
// Check that forward typdes of unions are supported
module test;
`define check(val, exp) \
if (val !== exp) begin \
$display("FAILED(%0d). '%s' expected %0d, got %0d", `__LINE__, `"val`", exp, val); \
failed = 1'b1; \
end
bit failed = 1'b0;
typedef union T;
typedef T;
T x;
typedef union packed {
int x;
logic [3:0][7:0] y;
} T;
T y;
// There can be as many forward typedefs as we like, even after the type
// itself has already been declared.
typedef T;
typedef union T;
T z;
initial begin
`check($bits(x), $bits(int));
`check($bits(y), $bits(int));
`check($bits(z), $bits(int));
if (!failed) begin
$display("PASSED");
end
end
endmodule

View File

@ -0,0 +1,18 @@
// Check that it is an error to use a forwarded union type as the type for the
// members in the union itself.
module test;
typedef T;
typedef union packed {
T x;
} T;
T x;
initial begin
$display("FAILED");
end
endmodule

View File

@ -720,6 +720,16 @@ sv_typedef_darray_base1 normal,-g2009 ivltests
sv_typedef_darray_base2 normal,-g2009 ivltests
sv_typedef_darray_base3 normal,-g2009 ivltests
sv_typedef_darray_base4 normal,-g2009 ivltests
sv_typedef_fwd_base normal,-g2009 ivltests
sv_typedef_fwd_class normal,-g2009 ivltests
sv_typedef_fwd_union normal,-g2009 ivltests
sv_typedef_fwd_union_fail CE,-g2009 ivltests
sv_typedef_fwd_enum1 normal,-g2009 ivltests
sv_typedef_fwd_enum2 normal,-g2009 ivltests
sv_typedef_fwd_enum3 normal,-g2009 ivltests
sv_typedef_fwd_enum_fail CE,-g2009 ivltests
sv_typedef_fwd_struct normal,-g2009 ivltests
sv_typedef_fwd_struct_fail CE,-g2009 ivltests
sv_typedef_nested_array normal,-g2009 ivltests
sv_typedef_queue_base1 normal,-g2009 ivltests
sv_typedef_queue_base2 normal,-g2009 ivltests

View File

@ -302,6 +302,7 @@ sv_string5 CE,-g2009 ivltests
sv_string6 CE,-g2009,-pallowsigned=1 ivltests
sv_string7 CE,-g2009,-pallowsigned=1 ivltests
sv_string7b CE,-g2009,-pallowsigned=1 ivltests
sv_typedef_fwd_base CE,-g2009 ivltests
vhdl_string_lim CE,-g2005-sv,-pallowsigned=1,ivltests/vhdl_string_lim.vhd ivltests
vhdl_textio_write CE,-g2005-sv,-pallowsigned=1,ivltests/vhdl_textio_write.vhd ivltests
vhdl_textio_read CE,-g2005-sv,-pallowsigned=1,ivltests/vhdl_textio_read.vhd ivltests
@ -440,6 +441,8 @@ sv_port_default8 CE,-g2009,-pallowsigned=1 ivltests
sv_port_default9 CE,-g2009 ivltests
sv_ps_type_class1 CE,-g2009 ivltests
sv_root_class CE,-g2009 ivltests
sv_typedef_fwd_class CE,-g2009 ivltests
sv_typedef_fwd_enum3 CE,-g2009 ivltests
sv_typedef_scope3 CE,-g2009 ivltests
sv_unit2b CE,-g2009 ivltests
sv_unit3b CE,-g2009 ivltests