From 23c02a4b0a0c3bddc6267da0a239a9640e1e5ddf Mon Sep 17 00:00:00 2001 From: Mingun Date: Tue, 1 Dec 2020 00:23:14 +0500 Subject: [PATCH] Fix non-exhaustive warnings in the EveryReadIsExpression and GoReads I'm not sure that Borrowed types is possible there but types allow them. As side effect, remove dead code --- .../languages/components/CommonReads.scala | 30 +++++++++++++++++++ .../components/EveryReadIsExpression.scala | 22 +++----------- .../struct/languages/components/GoReads.scala | 22 +++----------- 3 files changed, 38 insertions(+), 36 deletions(-) diff --git a/shared/src/main/scala/io/kaitai/struct/languages/components/CommonReads.scala b/shared/src/main/scala/io/kaitai/struct/languages/components/CommonReads.scala index ed36b47d7..98e57760b 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/components/CommonReads.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/components/CommonReads.scala @@ -111,4 +111,34 @@ trait CommonReads extends LanguageCompiler { */ def attrValidateAll(attr: AttrLikeSpec) = attr.valid.foreach(valid => attrValidate(attr.id, attr, valid)) + + /** + * Allocates new IO for type with known size + * + * @param id name of attribute for read + * @param bytes specifies properties of the returned sub-stream + * @param io name of the parent stream from which + * @param rep specifies the repeat pattern for the attribute + * + * @return string with allocated sub-stream name + */ + def allocateSubstream( + id: Identifier, + bytes: BytesType, + io: String, + rep: RepeatSpec, + ): String = { + // we have a fixed buffer, thus we shall create separate IO for it + val rawId = RawIdentifier(id) + + // Default endianness isn't required for the BytesType + attrParse2(rawId, bytes, io, rep, true, None) + + this match { + case thisStore: AllocateAndStoreIO => + thisStore.allocateIO(rawId, rep) + case thisLocal: AllocateIOLocalVar => + thisLocal.allocateIO(rawId, rep) + } + } } diff --git a/shared/src/main/scala/io/kaitai/struct/languages/components/EveryReadIsExpression.scala b/shared/src/main/scala/io/kaitai/struct/languages/components/EveryReadIsExpression.scala index 7f803a816..2d372031e 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/components/EveryReadIsExpression.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/components/EveryReadIsExpression.scala @@ -102,24 +102,10 @@ trait EveryReadIsExpression def attrUserTypeParse(id: Identifier, dataType: UserType, io: String, rep: RepeatSpec, defEndian: Option[FixedEndian], assignType: DataType): Unit = { val newIO = dataType match { case knownSizeType: UserTypeFromBytes => - // we have a fixed buffer, thus we shall create separate IO for it - val rawId = RawIdentifier(id) - val byteType = knownSizeType.bytes - - attrParse2(rawId, byteType, io, rep, true, defEndian) - - val extraType = rep match { - case NoRepeat => byteType - case _ => ArrayTypeInStream(byteType) - } - - this match { - case thisStore: AllocateAndStoreIO => - thisStore.allocateIO(rawId, rep) - case thisLocal: AllocateIOLocalVar => - thisLocal.allocateIO(rawId, rep) - } - case _: UserTypeInstream => + allocateSubstream(id, knownSizeType.bytes, io, rep) + case knownSizeType: CalcUserTypeFromBytes => + allocateSubstream(id, knownSizeType.bytes, io, rep) + case _: UserTypeInstream | _: CalcUserType => // no fixed buffer, just use regular IO io } diff --git a/shared/src/main/scala/io/kaitai/struct/languages/components/GoReads.scala b/shared/src/main/scala/io/kaitai/struct/languages/components/GoReads.scala index 17f1d99ae..a7cbaa2a6 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/components/GoReads.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/components/GoReads.scala @@ -112,24 +112,10 @@ trait GoReads extends CommonReads with ObjectOrientedLanguage with GoSwitchOps { def attrUserTypeParse(id: Identifier, dataType: UserType, io: String, rep: RepeatSpec, defEndian: Option[FixedEndian]): Unit = { val newIO = dataType match { case knownSizeType: UserTypeFromBytes => - // we have a fixed buffer, thus we shall create separate IO for it - val rawId = RawIdentifier(id) - val byteType = knownSizeType.bytes - - attrParse2(rawId, byteType, io, rep, true, defEndian) - - val extraType = rep match { - case NoRepeat => byteType - case _ => ArrayTypeInStream(byteType) - } - - this match { - case thisStore: AllocateAndStoreIO => - thisStore.allocateIO(rawId, rep) - case thisLocal: AllocateIOLocalVar => - thisLocal.allocateIO(rawId, rep) - } - case _: UserTypeInstream => + allocateSubstream(id, knownSizeType.bytes, io, rep) + case knownSizeType: CalcUserTypeFromBytes => + allocateSubstream(id, knownSizeType.bytes, io, rep) + case _: UserTypeInstream | _: CalcUserType => // no fixed buffer, just use regular IO io }