From ceb97eec5b1a95d5281a8dbe2d6cd734f0d4ca55 Mon Sep 17 00:00:00 2001 From: Mingun Date: Fri, 27 Nov 2020 23:55:27 +0500 Subject: [PATCH] Remove redundant dataTypeByteSize and fix non-exhaustive warnings in CalculateSeqSize --- .../struct/precompile/CalculateSeqSizes.scala | 51 ++++++++++--------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/shared/src/main/scala/io/kaitai/struct/precompile/CalculateSeqSizes.scala b/shared/src/main/scala/io/kaitai/struct/precompile/CalculateSeqSizes.scala index 8b1ba308d..8425fc06e 100644 --- a/shared/src/main/scala/io/kaitai/struct/precompile/CalculateSeqSizes.scala +++ b/shared/src/main/scala/io/kaitai/struct/precompile/CalculateSeqSizes.scala @@ -78,43 +78,46 @@ object CalculateSeqSizes { * Determines how many bits occupies given data type. * * @param dataType data type to analyze - * @return number of bits or None, if it's impossible to determine a priori + * @return number of bits or [[DynamicSized]], if it's impossible to determine a priori */ def dataTypeBitsSize(dataType: DataType): Sized = { dataType match { case BitsType1(_) => FixedSized(1) - case BitsType(width, _) => FixedSized(width) + case CalcBooleanType => DynamicSized + case EnumType(_, basedOn) => dataTypeBitsSize(basedOn) + case ut: OwnedUserType => getSeqSize(ut.classSpec.get) - case _ => - dataTypeByteSize(dataType) match { - case FixedSized(x) => FixedSized(x * 8) - case otherSize => otherSize - } - } - } + case ut: OwnedUserTypeFromBytes => dataTypeBitsSize(ut.bytes) + case ut: BorrowedUserTypeFromBytes => dataTypeBitsSize(ut.bytes) + case _: StructType => DynamicSized + + case BitsType(width, _) => FixedSized(width) + case Int1Type(_) => FixedSized(8) + case IntMultiType(_, width, _) => FixedSized(width.width * 8) + case CalcIntType => DynamicSized + + case FloatMultiType(width, _) => FixedSized(width.width * 8) + case CalcFloatType => DynamicSized - /** - * Determines how many bytes occupies a given data type. - * - * @param dataType data type to analyze - * @return number of bytes or None, if it's impossible to determine a priori - */ - def dataTypeByteSize(dataType: DataType): Sized = { - dataType match { - case _: Int1Type => FixedSized(1) - case IntMultiType(_, width, _) => FixedSized(width.width) - case FloatMultiType(width, _) => FixedSized(width.width) case _: BytesEosType => DynamicSized case blt: BytesLimitType => blt.size.evaluateIntConst match { - case Some(x) => FixedSized(x.toInt) + case Some(x) => FixedSized(x.toInt * 8) case None => DynamicSized } case _: BytesTerminatedType => DynamicSized - case StrFromBytesType(basedOn, _) => dataTypeByteSize(basedOn) - case utb: OwnedUserTypeFromBytes => dataTypeByteSize(utb.bytes) - case cutb: BorrowedUserTypeFromBytes => dataTypeByteSize(cutb.bytes) + case BorrowedBytesType => DynamicSized + + case StrFromBytesType(basedOn, _) => dataTypeBitsSize(basedOn) + case CalcStrType => DynamicSized + case st: SwitchType => DynamicSized // FIXME: it's really possible get size if st.hasSize + + case OwnedKaitaiStreamType | BorrowedKaitaiStreamType => DynamicSized + + // TODO: Add special type or attribute to ArrayType for arrays of known size + case _: ArrayType => DynamicSized + case AnyType => DynamicSized } } }