Skip to content

Commit

Permalink
Fix non-exhaustive warnings in the EveryReadIsExpression and GoReads
Browse files Browse the repository at this point in the history
Move `createSubstreamBuffered` from `EveryReadIsExpression` to `CommonReads`
to make it available for `GoReads`

I'm not sure that Borrowed types is possible there but types allow them.
As a side effect, remove dead code
  • Loading branch information
Mingun committed Sep 12, 2023
1 parent 5199ade commit 7080082
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.kaitai.struct.languages.components

import io.kaitai.struct.datatype._
import io.kaitai.struct.datatype.DataType.{SwitchType, UserTypeFromBytes, BytesType}
import io.kaitai.struct.datatype.DataType.{ArrayTypeInStream, SwitchType, UserTypeFromBytes, BytesType}
import io.kaitai.struct.exprlang.Ast
import io.kaitai.struct.format._

Expand Down Expand Up @@ -116,4 +116,38 @@ trait CommonReads extends LanguageCompiler {
*/
def attrValidateAll(attr: AttrLikeSpec) =
attr.valid.foreach(valid => attrValidate(attr.id, attr, valid))

/**
* Creates a substream by reading bytes that will comprise the stream first into a buffer in
* memory, and then wrapping that buffer as a new stream.
* @param id identifier of a member that this stream is for
* @param byteType underlying bytes type
* @param io parent stream to derive substream from
* @param rep repeat specification for underlying bytes type
* @param defEndian default endianness specification
* @return string reference to a freshly created substream
*/
def createSubstreamBuffered(
id: Identifier,
byteType: BytesType,
io: String,
rep: RepeatSpec,
defEndian: Option[FixedEndian]
): String = {
val rawId = RawIdentifier(id)

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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ trait EveryReadIsExpression
case knownSizeType: UserTypeFromBytes =>
// we have a fixed buffer, thus we shall create separate IO for it
createSubstream(id, knownSizeType.bytes, io, rep, defEndian)
case _: UserTypeInstream =>
case knownSizeType: CalcUserTypeFromBytes =>
// we have a fixed buffer, thus we shall create separate IO for it
createSubstream(id, knownSizeType.bytes, io, rep, defEndian)
case _: UserTypeInstream | _: CalcUserType =>
// no fixed buffer, just use regular IO
io
}
Expand Down Expand Up @@ -176,33 +179,6 @@ trait EveryReadIsExpression
None
)

/**
* Creates a substream by reading bytes that will comprise the stream first into a buffer in
* memory, and then wrapping that buffer as a new stream.
* @param id identifier of a member that this stream is for
* @param byteType underlying bytes type
* @param io parent stream to derive substream from
* @param rep repeat specification for underlying bytes type
* @param defEndian default endianness specification
* @return string reference to a freshly created substream
*/
def createSubstreamBuffered(id: Identifier, byteType: BytesType, io: String, rep: RepeatSpec, defEndian: Option[FixedEndian]): String = {
val rawId = RawIdentifier(id)

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)
}
}

def attrSwitchTypeParse(
id: Identifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
createSubstreamBuffered(id, knownSizeType.bytes, io, rep, defEndian)
case knownSizeType: CalcUserTypeFromBytes =>
createSubstreamBuffered(id, knownSizeType.bytes, io, rep, defEndian)
case _: UserTypeInstream | _: CalcUserType =>
// no fixed buffer, just use regular IO
io
}
Expand Down

0 comments on commit 7080082

Please sign in to comment.