Skip to content

Commit

Permalink
Generate imports also for imported nested types
Browse files Browse the repository at this point in the history
..., not only for imported top-level types as before.

Fixes kaitai-io/kaitai_struct#703

Fixes the NestedTypesImport test in those target languages where the
necessary imports were missing (C++, JavaScript, Python)
  • Loading branch information
generalmimon committed Apr 7, 2024
1 parent 8fa8fdd commit b2ec9e5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
3 changes: 1 addition & 2 deletions shared/src/main/scala/io/kaitai/struct/ClassCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ class ClassCompiler(

def compileOpaqueClasses(topClass: ClassSpec) = {
TypeProcessor.getOpaqueClasses(topClass).foreach((classSpec) =>
if (classSpec != topClass)
lang.opaqueClassDeclaration(classSpec)
lang.opaqueClassDeclaration(classSpec)
)
}

Expand Down
10 changes: 5 additions & 5 deletions shared/src/main/scala/io/kaitai/struct/TypeProcessor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ object TypeProcessor {
def getOpaqueClasses(curClass: ClassSpec): Iterable[ClassSpec] = {
val res = mutable.Set[ClassSpec]()
curClass.seq.map((attr) =>
res ++= getOpaqueDataTypes(attr.dataType)
res ++= getOpaqueDataTypes(attr.dataType, curClass)
)
curClass.instances.foreach { case (_, inst) =>
inst match {
case pis: ParseInstanceSpec =>
res ++= getOpaqueDataTypes(pis.dataType)
res ++= getOpaqueDataTypes(pis.dataType, curClass)
case _ => None
}
}
Expand All @@ -28,17 +28,17 @@ object TypeProcessor {
res
}

def getOpaqueDataTypes(dataType: DataType): Iterable[ClassSpec] = {
def getOpaqueDataTypes(dataType: DataType, curClass: ClassSpec): Iterable[ClassSpec] = {
dataType match {
case ut: UserType =>
if (ut.isOpaque) {
if (ut.isExternal(curClass)) {
List(ut.classSpec.get)
} else {
List()
}
case st: SwitchType =>
st.cases.flatMap { case (_, ut) =>
getOpaqueDataTypes(ut)
getOpaqueDataTypes(ut, curClass)
}
case _ =>
// all other types are not opaque external user types
Expand Down

0 comments on commit b2ec9e5

Please sign in to comment.