Skip to content

Commit c56a91b

Browse files
committed
disambiguate struct names that shadow module names
1 parent 7808819 commit c56a91b

File tree

5 files changed

+52
-14
lines changed

5 files changed

+52
-14
lines changed

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
declarations tagged with an attribute
2323
* Fixed stray attributes producing invalid nested output when attached to
2424
inlined interfaces and interface-bounds modules
25+
* Fixed conversion of struct variables that shadow their module's name
2526
* Fixed `` `resetall `` not resetting the `` `default_nettype ``
2627

2728
### Other Enhancements

‎src/Convert/Struct.hs‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -380,15 +380,15 @@ convertLHS l = do
380380
-- try expression conversion by looking at the *innermost* type first
381381
convertSubExpr :: Scopes Type -> Expr -> (Type, Expr)
382382
convertSubExpr scopes (Dot e x) =
383-
if isntStruct subExprType then
383+
if isntStruct subExprType || isHier then
384384
fallbackType scopes $ Dot e' x
385385
else if structIsntReady subExprType then
386386
(fieldType, Dot e' x)
387387
else
388388
(fieldType, undottedWithSign)
389389
where
390390
(subExprType, e') = convertSubExpr scopes e
391-
(fieldType, bounds, dims) = lookupFieldInfo scopes subExprType e' x
391+
(isHier, fieldType, bounds, dims) = lookupFieldInfo scopes subExprType e' x
392392
base = fst bounds
393393
len = rangeSize bounds
394394
undotted = if null dims || rangeSize (head dims) == RawNum 1
@@ -403,7 +403,7 @@ convertSubExpr scopes (Dot e x) =
403403
else undotted
404404

405405
convertSubExpr scopes (Range (Dot e x) NonIndexed rOuter) =
406-
if isntStruct subExprType then
406+
if isntStruct subExprType || isHier then
407407
(UnknownType, orig')
408408
else if structIsntReady subExprType then
409409
(replaceInnerTypeRange NonIndexed rOuter' fieldType, orig')
@@ -420,7 +420,7 @@ convertSubExpr scopes (Range (Dot e x) NonIndexed rOuter) =
420420
(_, roRight') = convertSubExpr scopes roRight
421421
rOuter' = (roLeft', roRight')
422422
orig' = Range (Dot e' x) NonIndexed rOuter'
423-
(fieldType, bounds, dims) = lookupFieldInfo scopes subExprType e' x
423+
(isHier, fieldType, bounds, dims) = lookupFieldInfo scopes subExprType e' x
424424
[dim] = dims
425425
rangeLeft = ( BinOp Sub (fst bounds) $ BinOp Sub (fst dim) roLeft'
426426
, BinOp Sub (fst bounds) $ BinOp Sub (fst dim) roRight' )
@@ -429,7 +429,7 @@ convertSubExpr scopes (Range (Dot e x) NonIndexed rOuter) =
429429
undotted = Range e' NonIndexed $
430430
endianCondRange dim rangeLeft rangeRight
431431
convertSubExpr scopes (Range (Dot e x) mode (baseO, lenO)) =
432-
if isntStruct subExprType then
432+
if isntStruct subExprType || isHier then
433433
(UnknownType, orig')
434434
else if structIsntReady subExprType then
435435
(replaceInnerTypeRange mode (baseO', lenO') fieldType, orig')
@@ -444,7 +444,7 @@ convertSubExpr scopes (Range (Dot e x) mode (baseO, lenO)) =
444444
(_, baseO') = convertSubExpr scopes baseO
445445
(_, lenO') = convertSubExpr scopes lenO
446446
orig' = Range (Dot e' x) mode (baseO', lenO')
447-
(fieldType, bounds, dims) = lookupFieldInfo scopes subExprType e' x
447+
(isHier, fieldType, bounds, dims) = lookupFieldInfo scopes subExprType e' x
448448
[dim] = dims
449449
baseLeft = BinOp Sub (fst bounds) $ BinOp Sub (fst dim) baseO'
450450
baseRight = BinOp Add (snd bounds) $ BinOp Sub (snd dim) baseO'
@@ -463,7 +463,7 @@ convertSubExpr scopes (Range e mode (left, right)) =
463463
(_, right') = convertSubExpr scopes right
464464
r' = (left', right')
465465
convertSubExpr scopes (Bit (Dot e x) i) =
466-
if isntStruct subExprType then
466+
if isntStruct subExprType || isHier then
467467
(dropInnerTypeRange backupType, orig')
468468
else if structIsntReady subExprType then
469469
(dropInnerTypeRange fieldType, orig')
@@ -477,7 +477,7 @@ convertSubExpr scopes (Bit (Dot e x) i) =
477477
(_, i') = convertSubExpr scopes i
478478
(backupType, _) = fallbackType scopes $ Dot e' x
479479
orig' = Bit (Dot e' x) i'
480-
(fieldType, bounds, dims) = lookupFieldInfo scopes subExprType e' x
480+
(isHier, fieldType, bounds, dims) = lookupFieldInfo scopes subExprType e' x
481481
[dim] = dims
482482
left = BinOp Sub (fst bounds) $ BinOp Sub (fst dim) i'
483483
right = BinOp Add (snd bounds) $ BinOp Sub (snd dim) i'
@@ -536,20 +536,22 @@ isntStruct = (== Nothing) . getFields
536536

537537
-- get the field type, flattened bounds, and original type dimensions
538538
lookupFieldInfo :: Scopes Type -> Type -> Expr -> Identifier
539-
-> (Type, Range, [Range])
539+
-> (Bool, Type, Range, [Range])
540540
lookupFieldInfo scopes struct base fieldName =
541541
if maybeFieldType == Nothing
542-
then scopedError scopes $ "field '" ++ fieldName ++ "' not found in "
543-
++ show struct ++ ", in expression "
544-
++ show (Dot base fieldName)
545-
else (fieldType, bounds, dims)
542+
then (isHier, err, err, err)
543+
else (False, fieldType, bounds, dims)
546544
where
547545
Just fields = getFields struct
548546
maybeFieldType = lookup fieldName $ map swap fields
549547
Just fieldType = maybeFieldType
550548
dims = snd $ typeRanges fieldType
551549
Just (_, unstructRanges) = convertStruct struct
552550
Just bounds = lookup fieldName unstructRanges
551+
err = scopedError scopes $ "field '" ++ fieldName ++ "' not found in "
552+
++ show struct ++ ", in expression "
553+
++ show (Dot base fieldName)
554+
isHier = lookupElem scopes (Dot base fieldName) /= Nothing
553555

554556
-- attempts to convert based on the assignment-like contexts of TF arguments
555557
convertCall :: Scopes Type -> Expr -> Args -> Args

‎src/Convert/TypeOf.hs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,11 @@ isStringParam _ = return False
159159
-- checks if referring to part.wire is needlessly explicit
160160
unneededModuleScope :: Identifier -> Identifier -> ST Bool
161161
unneededModuleScope part wire = do
162+
partDetails <- lookupElemM part
162163
accessesLocal <- localAccessesM wire
163-
if accessesLocal == accessesTop then
164+
if partDetails /= Nothing then
165+
return False
166+
else if accessesLocal == accessesTop then
164167
return True
165168
else if head accessesLocal == head accessesTop then do
166169
details <- lookupElemM wire

‎test/core/struct_hier_shadow.sv‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package P;
2+
localparam [31:0] L = 8;
3+
typedef struct packed {
4+
logic [L + L[0] + L[1:0] + L[0+:1] - 1:0] x;
5+
} S;
6+
endpackage
7+
module top;
8+
P::S top;
9+
logic [2:0] x;
10+
assign x = '0;
11+
assign top.x = '1;
12+
initial begin
13+
#1;
14+
$display("%b %b", x, top.x);
15+
$display("%b %b", x[0], top.x[0]);
16+
$display("%b %b", x[1:0], top.x[1:0]);
17+
$display("%b %b", x[0+:1], top.x[0+:1]);
18+
end
19+
endmodule

‎test/core/struct_hier_shadow.v‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module top;
2+
wire [7:0] top;
3+
wire [2:0] x;
4+
assign x = 0;
5+
assign top = 8'hFF;
6+
initial begin
7+
#1;
8+
$display("%b %b", x, top);
9+
$display("%b %b", x[0], top[0]);
10+
$display("%b %b", x[1:0], top[1:0]);
11+
$display("%b %b", x[0+:1], top[0+:1]);
12+
end
13+
endmodule

0 commit comments

Comments
 (0)