Skip to content

Commit 99d2f72

Browse files
committed
Refactor CheckUses
1 parent a57636d commit 99d2f72

File tree

2 files changed

+43
-52
lines changed

2 files changed

+43
-52
lines changed

compiler/lib/src/main/scala/analysis/Analyzers/BasicUseAnalyzer.scala

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,28 @@ trait BasicUseAnalyzer extends TypeExpressionAnalyzer {
3535

3636
/** An implied constant use */
3737
def impliedConstantUse(a: Analysis, iu: ImpliedUse) =
38-
iu.annotateResult(exprNode(a, iu.asExprNode))
38+
for {
39+
a <- iu.annotateResult(exprNode(a, iu.asExprNode))
40+
_ <- impliedUse(a, iu, ImpliedUse.Kind.Constant)
41+
} yield a
3942

4043
/** An implied port use */
4144
def impliedPortUse(a: Analysis, iu: ImpliedUse) =
42-
iu.annotateResult(portUse(a, iu.asQualIdentNode, iu.name))
45+
for {
46+
a <- iu.annotateResult(portUse(a, iu.asQualIdentNode, iu.name))
47+
_ <- impliedUse(a, iu, ImpliedUse.Kind.Port)
48+
} yield a
4349

4450
/** An implied type use */
4551
def impliedTypeUse(a: Analysis, iu: ImpliedUse) =
46-
iu.annotateResult(typeUse(a, iu.asTypeNameNode, iu.name))
52+
for {
53+
a <- iu.annotateResult(typeUse(a, iu.asTypeNameNode, iu.name))
54+
_ <- impliedUse(a, iu, ImpliedUse.Kind.Type)
55+
} yield a
56+
57+
/** An implied use */
58+
def impliedUse(a: Analysis, iu: ImpliedUse, kind: ImpliedUse.Kind): Result =
59+
Right(a)
4760

4861
override def defComponentInstanceAnnotatedNode(a: Analysis, node: Ast.Annotated[AstNode[Ast.DefComponentInstance]]) = {
4962
val (_, node1, _) = node

compiler/lib/src/main/scala/analysis/CheckSemantics/CheckUses.scala

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,33 @@ object CheckUses extends BasicUseAnalyzer {
8181
visitExprNode(a, node)
8282
}
8383

84-
override def impliedConstantUse(a: Analysis, iu: ImpliedUse) =
85-
for {
86-
a <- super.impliedConstantUse(a, iu)
87-
_ <- checkImpliedUse(a, iu, "constant")
88-
} yield a
89-
90-
override def impliedPortUse(a: Analysis, iu: ImpliedUse) =
91-
for {
92-
a <- super.impliedPortUse(a, iu)
93-
_ <- checkImpliedUse(a, iu, "port")
94-
} yield a
95-
96-
override def impliedTypeUse(a: Analysis, iu: ImpliedUse) =
97-
for {
98-
a <- super.impliedTypeUse(a, iu)
99-
_ <- checkImpliedUse(a, iu, "type")
100-
} yield a
84+
// Check that an implied use (a) is not a member
85+
// of a def and (b) does not shadow the required def
86+
override def impliedUse(a: Analysis, iu: ImpliedUse, kind: ImpliedUse.Kind) = {
87+
val sym = a.useDefMap(iu.id)
88+
val symQualifiedName = a.getQualifiedName(sym).toString
89+
val iuName = iu.name.toString
90+
// Check that the name of the def matches the name of the use
91+
val result = if symQualifiedName == iuName
92+
// OK, they match
93+
then Right(a)
94+
else {
95+
val msg = if symQualifiedName.length < iuName.length
96+
// Definition has a shorter name: the use is a member of the definition
97+
then s"it has $iuName as a member"
98+
// Definition has a longer name: it shadows the required definition
99+
else s"it shadows $iuName here"
100+
Left(
101+
SemanticError.InvalidSymbol(
102+
symQualifiedName,
103+
Locations.get(iu.id),
104+
msg,
105+
sym.getLoc
106+
)
107+
)
108+
}
109+
iu.annotateResult(result)
110+
}
101111

102112
override def defComponentAnnotatedNode(a: Analysis, aNode: Ast.Annotated[AstNode[Ast.DefComponent]]) = {
103113
val (_, node, _) = aNode
@@ -186,36 +196,4 @@ object CheckUses extends BasicUseAnalyzer {
186196
}
187197
}
188198

189-
// Check that an implied use (a) is not a member
190-
// of a def and (b) does not shadow the required def
191-
private def checkImpliedUse(
192-
a: Analysis,
193-
iu: ImpliedUse,
194-
kind: String
195-
) = {
196-
val sym = a.useDefMap(iu.id)
197-
val symQualifiedName = a.getQualifiedName(sym).toString
198-
val iuName = iu.name.toString
199-
// Check that the name of the def matches the name of the use
200-
val result = if symQualifiedName == iuName
201-
// OK, they match
202-
then Right(())
203-
else {
204-
val msg = if symQualifiedName.length < iuName.length
205-
// Definition has a shorter name: the use is a member of the definition
206-
then s"it has $iuName as a member"
207-
// Definition has a longer name: it shadows the required definition
208-
else s"it shadows $iuName here"
209-
Left(
210-
SemanticError.InvalidSymbol(
211-
symQualifiedName,
212-
Locations.get(iu.id),
213-
msg,
214-
sym.getLoc
215-
)
216-
)
217-
}
218-
iu.annotateResult(result)
219-
}
220-
221199
}

0 commit comments

Comments
 (0)