Skip to content

Commit ceb97ee

Browse files
committed
Remove redundant dataTypeByteSize and fix non-exhaustive warnings in CalculateSeqSize
1 parent 3fc4af9 commit ceb97ee

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

shared/src/main/scala/io/kaitai/struct/precompile/CalculateSeqSizes.scala

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,43 +78,46 @@ object CalculateSeqSizes {
7878
* Determines how many bits occupies given data type.
7979
*
8080
* @param dataType data type to analyze
81-
* @return number of bits or None, if it's impossible to determine a priori
81+
* @return number of bits or [[DynamicSized]], if it's impossible to determine a priori
8282
*/
8383
def dataTypeBitsSize(dataType: DataType): Sized = {
8484
dataType match {
8585
case BitsType1(_) => FixedSized(1)
86-
case BitsType(width, _) => FixedSized(width)
86+
case CalcBooleanType => DynamicSized
87+
8788
case EnumType(_, basedOn) => dataTypeBitsSize(basedOn)
89+
8890
case ut: OwnedUserType => getSeqSize(ut.classSpec.get)
89-
case _ =>
90-
dataTypeByteSize(dataType) match {
91-
case FixedSized(x) => FixedSized(x * 8)
92-
case otherSize => otherSize
93-
}
94-
}
95-
}
91+
case ut: OwnedUserTypeFromBytes => dataTypeBitsSize(ut.bytes)
92+
case ut: BorrowedUserTypeFromBytes => dataTypeBitsSize(ut.bytes)
93+
case _: StructType => DynamicSized
94+
95+
case BitsType(width, _) => FixedSized(width)
96+
case Int1Type(_) => FixedSized(8)
97+
case IntMultiType(_, width, _) => FixedSized(width.width * 8)
98+
case CalcIntType => DynamicSized
99+
100+
case FloatMultiType(width, _) => FixedSized(width.width * 8)
101+
case CalcFloatType => DynamicSized
96102

97-
/**
98-
* Determines how many bytes occupies a given data type.
99-
*
100-
* @param dataType data type to analyze
101-
* @return number of bytes or None, if it's impossible to determine a priori
102-
*/
103-
def dataTypeByteSize(dataType: DataType): Sized = {
104-
dataType match {
105-
case _: Int1Type => FixedSized(1)
106-
case IntMultiType(_, width, _) => FixedSized(width.width)
107-
case FloatMultiType(width, _) => FixedSized(width.width)
108103
case _: BytesEosType => DynamicSized
109104
case blt: BytesLimitType => blt.size.evaluateIntConst match {
110-
case Some(x) => FixedSized(x.toInt)
105+
case Some(x) => FixedSized(x.toInt * 8)
111106
case None => DynamicSized
112107
}
113108
case _: BytesTerminatedType => DynamicSized
114-
case StrFromBytesType(basedOn, _) => dataTypeByteSize(basedOn)
115-
case utb: OwnedUserTypeFromBytes => dataTypeByteSize(utb.bytes)
116-
case cutb: BorrowedUserTypeFromBytes => dataTypeByteSize(cutb.bytes)
109+
case BorrowedBytesType => DynamicSized
110+
111+
case StrFromBytesType(basedOn, _) => dataTypeBitsSize(basedOn)
112+
case CalcStrType => DynamicSized
113+
117114
case st: SwitchType => DynamicSized // FIXME: it's really possible get size if st.hasSize
115+
116+
case OwnedKaitaiStreamType | BorrowedKaitaiStreamType => DynamicSized
117+
118+
// TODO: Add special type or attribute to ArrayType for arrays of known size
119+
case _: ArrayType => DynamicSized
120+
case AnyType => DynamicSized
118121
}
119122
}
120123
}

0 commit comments

Comments
 (0)