Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix alias type value conversion & cycles #631

Merged
merged 5 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,12 @@ trait TypeExpressionAnalyzer

override def defActionAnnotatedNode(a: Analysis, aNode: Ast.Annotated[AstNode[Ast.DefAction]]) = {
val (_, node, _) = aNode
val data = node.data
opt(typeNameNode)(a, node.data.typeName)
}

override def defAliasTypeAnnotatedNode(a: Analysis, node: Ast.Annotated[AstNode[Ast.DefAliasType]]) = {
val (_, node1, _) = node
val data = node1.data
typeNameNode(a, data.typeName)
typeNameNode(a, node1.data.typeName)
}

override def defArrayAnnotatedNode(a: Analysis, node: Ast.Annotated[AstNode[Ast.DefArray]]) = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ object CheckUseDefCycles extends UseAnalyzer {
override def constantUse(a: Analysis, node: AstNode[Ast.Expr], use: Name.Qualified) =
visitUse(a, node, use)

override def defAliasTypeAnnotatedNode(a: Analysis, node: Ast.Annotated[AstNode[Ast.DefAliasType]]) = {
val symbol = Symbol.AliasType(node)
visitDefPost(a, symbol, node, super.defAliasTypeAnnotatedNode)
}

override def defArrayAnnotatedNode(a: Analysis, node: Ast.Annotated[AstNode[Ast.DefArray]]) = {
val symbol = Symbol.Array(node)
visitDefPost(a, symbol, node, super.defArrayAnnotatedNode)
Expand Down Expand Up @@ -56,6 +61,7 @@ object CheckUseDefCycles extends UseAnalyzer {

private def visitDefPre(a: Analysis, symbol: Symbol): Result = {
symbol match {
case Symbol.AliasType(node) => defAliasTypeAnnotatedNode(a, node)
case Symbol.Array(node) => defArrayAnnotatedNode(a, node)
case Symbol.Constant(node) => defConstantAnnotatedNode(a, node)
case Symbol.Enum(node) => defEnumAnnotatedNode(a, node)
Expand Down
26 changes: 13 additions & 13 deletions compiler/lib/src/main/scala/analysis/Semantics/Value.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ sealed trait Value {

/** Convert this value to a type */
final def convertToType(t: Type): Option[Value] = {
if (Type.areIdentical(getType, t))
if (Type.areIdentical(getType, t.getUnderlyingType))
Some(this)
else convertToDistinctType(t)
}
Expand Down Expand Up @@ -125,7 +125,7 @@ object Value {
}

override def convertToDistinctType(t: Type) =
t match {
t.getUnderlyingType match {
case Type.PrimitiveInt(kind1) => Some(PrimitiveInt(value, kind1))
case Type.Integer => Some(Integer(value))
case Type.Float(kind1) => Some(Float(value.doubleValue, kind1))
Expand Down Expand Up @@ -185,7 +185,7 @@ object Value {
}

override def convertToDistinctType(t: Type) =
t match {
t.getUnderlyingType match {
case Type.PrimitiveInt(kind1) => Some(PrimitiveInt(value, kind1))
case Type.Integer => Some(Integer(value))
case Type.Float(kind1) => Some(Float(value.doubleValue, kind1))
Expand Down Expand Up @@ -229,7 +229,7 @@ object Value {
override def isZero = (Math.abs(value) < Float.EPSILON)

override def convertToDistinctType(t: Type) =
t match {
t.getUnderlyingType match {
case Type.PrimitiveInt(kind1) => Some(PrimitiveInt(value.intValue, kind1))
case Type.Integer => Some(Integer(value.intValue))
case Type.Float(kind1) => Some(Float(value, kind1))
Expand Down Expand Up @@ -258,7 +258,7 @@ object Value {
/** Boolean values */
case class Boolean(value: scala.Boolean) extends Value {

override def convertToDistinctType(t: Type) = promoteToAggregate(t)
override def convertToDistinctType(t: Type) = promoteToAggregate(t.getUnderlyingType)

override def getType = Type.Boolean

Expand All @@ -270,7 +270,7 @@ object Value {
case class String(value: java.lang.String) extends Value {

override def convertToDistinctType(t: Type) =
t match {
t.getUnderlyingType match {
case Type.String(_) => Some(this)
case _ => promoteToAggregate(t)
}
Expand All @@ -288,8 +288,8 @@ object Value {
def convertElements(in: List[Value], t: Type, out: List[Value]): Option[List[Value]] =
in match {
case Nil => Some(out.reverse)
case head :: tail => head.convertToType(t) match {
case Some(v) => convertElements(tail, t, v :: out)
case head :: tail => head.convertToType(t.getUnderlyingType) match {
case Some(v) => convertElements(tail, t.getUnderlyingType, v :: out)
case None => None
}
}
Expand All @@ -307,7 +307,7 @@ object Value {
}

override def convertToDistinctType(t: Type) =
t match {
t.getUnderlyingType match {
case anonArrayType : Type.AnonArray => convertToAnonArray(anonArrayType)
case arrayType : Type.Array => convertToArray(arrayType)
case _ => None
Expand Down Expand Up @@ -340,7 +340,7 @@ object Value {
anonArray.convertToArray(arrayType)

override def convertToDistinctType(t: Type) =
t match {
t.getUnderlyingType match {
case anonArrayType : Type.AnonArray => convertToAnonArray(anonArrayType)
case arrayType : Type.Array => convertToArray(arrayType)
case _ => None
Expand All @@ -363,7 +363,7 @@ object Value {
def convertToRepType: PrimitiveInt = PrimitiveInt(value._2, t.repType.kind)

override def convertToDistinctType(t: Type) =
convertToRepType.convertToDistinctType(t) match {
convertToRepType.convertToDistinctType(t.getUnderlyingType) match {
case Some(v) => Some(v)
case None => promoteToAggregate(t)
}
Expand Down Expand Up @@ -407,7 +407,7 @@ object Value {
}

override def convertToDistinctType(t: Type) =
t match {
t.getUnderlyingType match {
case anonStructType : Type.AnonStruct => convertToAnonStruct(anonStructType)
case structType : Type.Struct => convertToStruct(structType)
case _ => None
Expand Down Expand Up @@ -445,7 +445,7 @@ object Value {
def convertToStruct(structType: Type.Struct): Option[Value.Struct] =
anonStruct.convertToStruct(structType)
override def convertToDistinctType(t: Type) =
t match {
t.getUnderlyingType match {
case anonStructType : Type.AnonStruct => convertToAnonStruct(anonStructType)
case structType : Type.Struct => convertToStruct(structType)
case _ => None
Expand Down
9 changes: 9 additions & 0 deletions compiler/tools/fpp-check/test/cycle/alias.fpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type TStructCycle = SSCycle


struct SSCycle {
F1: string
F2: U32
F3: bool
F4: TStructCycle
}
7 changes: 7 additions & 0 deletions compiler/tools/fpp-check/test/cycle/alias.ref.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fpp-check
[ local path prefix ]/compiler/tools/fpp-check/test/cycle/alias.fpp:1.1
type TStructCycle = SSCycle
^
error: encountered a use-def cycle:
use SSCycle at [ local path prefix ]/compiler/tools/fpp-check/test/cycle/alias.fpp:1.21 refers to definition SSCycle at [ local path prefix ]/compiler/tools/fpp-check/test/cycle/alias.fpp:4.1
use TStructCycle at [ local path prefix ]/compiler/tools/fpp-check/test/cycle/alias.fpp:8.9 refers to definition TStructCycle at [ local path prefix ]/compiler/tools/fpp-check/test/cycle/alias.fpp:1.1
1 change: 1 addition & 0 deletions compiler/tools/fpp-check/test/cycle/tests.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
tests="
alias
array
constant_1
constant_2
Expand Down
34 changes: 34 additions & 0 deletions compiler/tools/fpp-check/test/type/alias_type_ok.fpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
enum E1T {
E1 = 0
E2 = 1
E3 = 3
}

struct SS {
F1: string
F2: U32
F3: bool
# F4: TStruct
}

type TU = U32
type TS = string size 20
type TE = E1T
type TB = bool
type TStruct = SS

constant B = false

array A1 = [2] TB default [B, B]
array A2 = [2] TE default [E1T.E1, E1T.E2]
array A3 = [2] TU default [1, 2]
array A4 = [2] TS default ["1", "2"]
array A5 = [2] TStruct default [{
F1 = "test",
F2 = 1,
F3 = false,
}, {
F1 = "test2",
F2 = 2,
F3 = true,
}]
Empty file.
1 change: 1 addition & 0 deletions compiler/tools/fpp-check/test/type/tests.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
tests="
alias_type_ok
string_size_negative
string_size_not_numeric
string_size_too_large
Expand Down
Loading