Skip to content

Commit 9a329a5

Browse files
authored
Merge pull request #119 from 0ncorhynchus/fix_data_declaration
Fix: reject implicit data types in data_declaration
2 parents f961da1 + e240928 commit 9a329a5

2 files changed

Lines changed: 58 additions & 43 deletions

File tree

sv-parser-parser/src/declarations/type_declarations.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ pub(crate) fn data_declaration_variable(s: Span) -> IResult<Span, DataDeclaratio
2525
let (s, a) = opt(r#const)(s)?;
2626
let (s, b) = opt(var)(s)?;
2727
let (s, c) = opt(lifetime)(s)?;
28-
let (s, d) = data_type_or_implicit_data_declaration_variable(s)?;
28+
let (s, d) = (verify(
29+
data_type_or_implicit_data_declaration_variable,
30+
|d| match d {
31+
DataTypeOrImplicit::DataType(_) => true,
32+
DataTypeOrImplicit::ImplicitDataType(_) => b.is_some(),
33+
},
34+
))(s)?;
2935
let (s, e) = list_of_variable_decl_assignments(s)?;
3036
let (s, f) = symbol(";")(s)?;
3137
Ok((

sv-parser-parser/src/tests.rs

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ mod unit {
5252
test!(method_call, "variable.method1().member", Ok((_, _)));
5353
}
5454

55+
#[test]
56+
fn test_data_declaration() {
57+
// Implicit data_type is not allowed unless the `var` keyword is used.
58+
test!(data_declaration, "logic x = 0;", Ok((_, _)));
59+
test!(data_declaration, " x = 0;", Err(_));
60+
test!(data_declaration, "var logic x = 0;", Ok((_, _)));
61+
test!(data_declaration, "var x = 0;", Ok((_, _)));
62+
test!(data_declaration, "const logic x = 0;", Ok((_, _)));
63+
test!(data_declaration, "const x = 0;", Err(_));
64+
}
65+
5566
#[test]
5667
fn test_pulldown_strength() {
5768
test!(pulldown_strength, "(supply0, strong1)", Ok((_, _)));
@@ -711,7 +722,7 @@ mod spec {
711722
Ok((_, _))
712723
);
713724
test!(
714-
many1(module_item),
725+
pair(many1(data_declaration), statement),
715726
r##"typedef struct {int a; shortreal b;} ab;
716727
ab c;
717728
c = '{0, 0.0}; // structure literal type determined from
@@ -724,7 +735,7 @@ mod spec {
724735
Ok((_, _))
725736
);
726737
test!(
727-
many1(module_item),
738+
many1(statement),
728739
r##"c = '{a:0, b:0.0}; // member name and value for that member
729740
c = '{default:0}; // all elements of structure c are set to 0
730741
d = ab'{int:1, shortreal:1.0}; // data type and default value for all
@@ -737,7 +748,7 @@ mod spec {
737748
Ok((_, _))
738749
);
739750
test!(
740-
many1(module_item),
751+
pair(many1(data_declaration), statement),
741752
r##"struct {int X,Y,Z;} XYZ = '{3{1}};
742753
typedef struct {int a,b[4];} ab_t;
743754
int a,b,c;
@@ -788,18 +799,18 @@ mod spec {
788799
Ok((_, _))
789800
);
790801
test!(
791-
module_item,
802+
statement,
792803
r##"a = b + (* mode = "cla" *) c; // sets the value for the attribute mode
793804
// to be the string cla."##,
794805
Ok((_, _))
795806
);
796807
test!(
797-
module_item,
808+
statement,
798809
r##"a = add (* mode = "cla" *) (b, c);"##,
799810
Ok((_, _))
800811
);
801812
test!(
802-
module_item,
813+
statement,
803814
r##"a = b ? (* no_glitch *) c : d;"##,
804815
Ok((_, _))
805816
);
@@ -1180,7 +1191,7 @@ mod spec {
11801191
Ok((_, _))
11811192
);
11821193
test!(
1183-
many1(module_item),
1194+
pair(many1(data_declaration), many1(statement)),
11841195
r##"typedef logic [15:0] r_t;
11851196
r_t r;
11861197
integer i = 1;
@@ -1204,7 +1215,7 @@ mod spec {
12041215
Ok((_, _))
12051216
);
12061217
test!(
1207-
many1(module_item),
1218+
pair(statement, data_declaration),
12081219
r##"str = "123";
12091220
int i = str.atoi(); // assigns 123 to i."##,
12101221
Ok((_, _))
@@ -1328,7 +1339,7 @@ mod spec {
13281339
Ok((_, _))
13291340
);
13301341
test!(
1331-
many1(module_item),
1342+
pair(many1(data_declaration), many1(statement)),
13321343
r##"typedef enum { red, green, blue, yellow, white, black } Colors;
13331344

13341345
Colors col;
@@ -1340,7 +1351,7 @@ mod spec {
13401351
Ok((_, _))
13411352
);
13421353
test!(
1343-
many1(module_item),
1354+
pair(many1(data_declaration), many1(statement)),
13441355
r##"typedef enum {Red, Green, Blue} Colors;
13451356
typedef enum {Mo,Tu,We,Th,Fr,Sa,Su} Week;
13461357
Colors C;
@@ -1707,7 +1718,7 @@ mod spec {
17071718
Ok((_, _))
17081719
);
17091720
test!(
1710-
many1(module_item),
1721+
pair(data_declaration, statement),
17111722
r##"var type(a+b) c, d;
17121723
c = type(i+3)'(v[15:0]);"##,
17131724
Ok((_, _))
@@ -1732,12 +1743,12 @@ mod spec {
17321743
// Ok((_, _))
17331744
//);
17341745
test!(
1735-
many1(module_item),
1746+
many1(statement),
17361747
r##"A = cast_t1'(expr_1) + cast_t2'(expr_2);"##,
17371748
Ok((_, _))
17381749
);
17391750
test!(
1740-
many1(module_item),
1751+
pair(many1(data_declaration), many1(statement)),
17411752
r##"cast_t1 temp1;
17421753
cast_t2 temp2;
17431754

@@ -1747,7 +1758,7 @@ mod spec {
17471758
Ok((_, _))
17481759
);
17491760
test!(
1750-
many1(module_item),
1761+
pair(many1(data_declaration), many1(statement)),
17511762
r##"logic [7:0] regA;
17521763
logic signed [7:0] regS;
17531764

@@ -1756,7 +1767,7 @@ mod spec {
17561767
Ok((_, _))
17571768
);
17581769
test!(
1759-
many1(module_item),
1770+
pair(many1(data_declaration), many1(statement)),
17601771
r##"typedef struct {
17611772
bit isfloat;
17621773
union { int i; shortreal f; } n; // anonymous type
@@ -1787,8 +1798,8 @@ mod spec {
17871798
end"##,
17881799
Ok((_, _))
17891800
);
1790-
test!(many1(module_item), r##"col = Colors'(2 + 3);"##, Ok((_, _)));
1791-
test!(many1(module_item), r##"B = dest_t'(A);"##, Ok((_, _)));
1801+
test!(statement, r##"col = Colors'(2 + 3);"##, Ok((_, _)));
1802+
test!(statement, r##"B = dest_t'(A);"##, Ok((_, _)));
17921803
test!(
17931804
many1(module_item),
17941805
r##"struct {bit[7:0] a; shortint b;} a;
@@ -1855,7 +1866,7 @@ mod spec {
18551866
// Ok((_, _))
18561867
//);
18571868
test!(
1858-
many1(module_item),
1869+
pair(many1(data_declaration), statement),
18591870
r##"typedef byte channel_type[$];
18601871
channel_type channel;
18611872
channel = {channel, channel_type'(genPkt())};"##,
@@ -1983,7 +1994,7 @@ mod spec {
19831994
Ok((_, _))
19841995
);
19851996
test!(
1986-
many1(module_item),
1997+
pair(many1(data_declaration), many1(statement)),
19871998
r##"typedef union packed { // default unsigned
19881999
s_atmcell acell;
19892000
bit [423:0] bit_slice;
@@ -2040,7 +2051,7 @@ mod spec {
20402051
Ok((_, _))
20412052
);
20422053
test!(
2043-
many1(module_item),
2054+
pair(data_declaration, many1(statement)),
20442055
r##"logic [7:0] mema [0:255]; // declares a memory array of 256 8-bit
20452056
// elements. The array indices are 0 to 255
20462057

@@ -2056,7 +2067,7 @@ mod spec {
20562067
Ok((_, _))
20572068
);
20582069
test!(
2059-
many1(module_item),
2070+
many1(statement),
20602071
r##"joe[9] = joe[8] + 1; // 4 byte add
20612072
joe[7][3:2] = joe[6][1:0]; // 2 byte copy"##,
20622073
Ok((_, _))
@@ -2086,7 +2097,7 @@ mod spec {
20862097
Ok((_, _))
20872098
);
20882099
test!(
2089-
many1(module_item),
2100+
pair(many1(data_declaration), many1(statement)),
20902101
r##"int A[2][3][4], B[2][3][4], C[5][4];
20912102
A[0][2] = B[1][1]; // assign a subarray composed of four ints
20922103
A[1] = B[0]; // assign a subarray composed of three arrays of
@@ -2101,28 +2112,28 @@ mod spec {
21012112
Ok((_, _))
21022113
);
21032114
test!(
2104-
many1(module_item),
2115+
pair(many1(data_declaration), statement),
21052116
r##"logic [63:0] data;
21062117
logic [7:0] byte2;
21072118
byte2 = data[23:16]; // an 8-bit part-select from data"##,
21082119
Ok((_, _))
21092120
);
21102121
test!(
2111-
many1(module_item),
2122+
pair(many1(data_declaration), statement),
21122123
r##"bit [3:0] [7:0] j; // j is a packed array
21132124
byte k;
21142125
k = j[2]; // select a single 8-bit element from j"##,
21152126
Ok((_, _))
21162127
);
21172128
test!(
2118-
many1(module_item),
2129+
pair(many1(data_declaration), statement),
21192130
r##"bit signed [31:0] busA [7:0] ; // unpacked array of 8 32-bit vectors
21202131
int busB [1:0]; // unpacked array of 2 integers
21212132
busB = busA[7:6]; // select a 2-vector slice from busA"##,
21222133
Ok((_, _))
21232134
);
21242135
test!(
2125-
many1(module_item),
2136+
pair(many1(data_declaration), statement),
21262137
r##"int i = bitvec[j +: k]; // k must be constant.
21272138
int a[x:y], b[y:z], e;
21282139
a = {b[c -: d], e}; // d must be constant"##,
@@ -2209,7 +2220,7 @@ mod spec {
22092220
Ok((_, _))
22102221
);
22112222
test!(
2212-
many1(module_item),
2223+
pair(many1(data_declaration), many1(statement)),
22132224
r##"int A[10:1]; // fixed-size array of 10 elements
22142225
int B[0:9]; // fixed-size array of 10 elements
22152226
int C[24:1]; // fixed-size array of 24 elements
@@ -2262,7 +2273,7 @@ mod spec {
22622273
Ok((_, _))
22632274
);
22642275
test!(
2265-
many1(module_item),
2276+
pair(many1(data_declaration), statement),
22662277
r##"string d[1:5] = '{ "a", "b", "c", "d", "e" };
22672278
string p[];
22682279
p = { d[1:3], "hello", d[4:5] };"##,
@@ -2624,7 +2635,7 @@ mod spec {
26242635
Ok((_, _))
26252636
);
26262637
test!(
2627-
many1(module_item),
2638+
pair(data_declaration, statement),
26282639
r##"Packet p; // declare a variable of class Packet
26292640
p = new; // initialize variable to a new allocated object
26302641
// of the class Packet"##,
@@ -2669,13 +2680,13 @@ mod spec {
26692680
Ok((_, _))
26702681
);
26712682
test!(
2672-
many1(module_item),
2683+
pair(data_declaration, statement),
26732684
r##"Packet p = new;
26742685
status = p.current_status();"##,
26752686
Ok((_, _))
26762687
);
26772688
test!(
2678-
many1(module_item),
2689+
statement,
26792690
r##"status = current_status(p);"##,
26802691
Ok((_, _))
26812692
);
@@ -2766,7 +2777,7 @@ mod spec {
27662777
Ok((_, _))
27672778
);
27682779
test!(
2769-
many1(module_item),
2780+
pair(data_declaration, statement),
27702781
r##"Packet p;
27712782
c = $fgetc( p.fileID );"##,
27722783
Ok((_, _))
@@ -2804,15 +2815,15 @@ mod spec {
28042815
Ok((_, _))
28052816
);
28062817
test!(many1(module_item), r##"Packet p1;"##, Ok((_, _)));
2807-
test!(many1(module_item), r##"p1 = new;"##, Ok((_, _)));
2818+
test!(statement, r##"p1 = new;"##, Ok((_, _)));
28082819
test!(
2809-
many1(module_item),
2820+
pair(data_declaration, statement),
28102821
r##"Packet p2;
28112822
p2 = p1;"##,
28122823
Ok((_, _))
28132824
);
28142825
test!(
2815-
many1(module_item),
2826+
pair(many1(data_declaration), many1(statement)),
28162827
r##"Packet p1;
28172828
Packet p2;
28182829
p1 = new;
@@ -3062,7 +3073,7 @@ mod spec {
30623073
Ok((_, _))
30633074
);
30643075
test!(
3065-
many1(module_item),
3076+
pair(many1(data_declaration), many1(statement)),
30663077
r##"EtherPacket ep = new; // extends BasePacket
30673078
TokenPacket tp = new; // extends BasePacket
30683079
GPSPacket gp = new; // extends EtherPacket
@@ -3502,7 +3513,7 @@ mod spec {
35023513
Ok((_, _))
35033514
);
35043515
test!(
3505-
many1(module_item),
3516+
statement,
35063517
r##"put_ref = new(); // illegal"##,
35073518
Ok((_, _))
35083519
);
@@ -4599,11 +4610,9 @@ mod spec {
45994610
Ok((_, _))
46004611
);
46014612
test!(
4602-
many1(module_item),
4603-
r##"initial begin
4604-
unpackedbits = '{2 {y}} ; // same as '{y, y}
4605-
int n[1:2][1:3] = '{2{'{3{y}}}}; // same as '{'{y,y,y},'{y,y,y}}
4606-
end"##,
4613+
pair(statement, data_declaration),
4614+
r##"unpackedbits = '{2 {y}} ; // same as '{y, y}
4615+
int n[1:2][1:3] = '{2{'{3{y}}}}; // same as '{'{y,y,y},'{y,y,y}}"##,
46074616
Ok((_, _))
46084617
);
46094618
test!(

0 commit comments

Comments
 (0)