Skip to content

Commit

Permalink
Rename methods by replacing "opaque" with "external"
Browse files Browse the repository at this point in the history
This is to reflect the new meaning of these methods since
b2ec9e5, when they were updated to
process all external types from the perspective of the current type, not
only opaque types.
  • Loading branch information
generalmimon committed Apr 7, 2024
1 parent a03420c commit 128e179
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 22 deletions.
8 changes: 4 additions & 4 deletions shared/src/main/scala/io/kaitai/struct/ClassCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ClassCompiler(

override def compile: CompileLog.SpecSuccess = {
lang.fileHeader(topClassName.head)
compileOpaqueClasses(topClass)
compileExternalClasses(topClass)
compileClass(topClass)
lang.fileFooter(topClassName.head)

Expand All @@ -29,9 +29,9 @@ class ClassCompiler(
)
}

def compileOpaqueClasses(topClass: ClassSpec) = {
TypeProcessor.getOpaqueClasses(topClass).foreach((classSpec) =>
lang.opaqueClassDeclaration(classSpec)
def compileExternalClasses(topClass: ClassSpec) = {
TypeProcessor.getExternalClasses(topClass).foreach((classSpec) =>
lang.externalClassDeclaration(classSpec)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class NimClassCompiler(

override def compile: CompileLog.SpecSuccess = {
lang.fileHeader(classNameFlattened(topClass))
compileOpaqueClasses(topClass)
compileExternalClasses(topClass)

// if there are any enums at all maybe we can detect it and not generate this template
// nimlang.enumTemplate
Expand Down
14 changes: 7 additions & 7 deletions shared/src/main/scala/io/kaitai/struct/TypeProcessor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ import io.kaitai.struct.format._
import scala.collection.mutable

object TypeProcessor {
def getOpaqueClasses(curClass: ClassSpec): Iterable[ClassSpec] = {
def getExternalClasses(curClass: ClassSpec): Iterable[ClassSpec] = {
val res = mutable.Set[ClassSpec]()
curClass.seq.map((attr) =>
res ++= getOpaqueDataTypes(attr.dataType, curClass)
res ++= getExternalDataTypes(attr.dataType, curClass)
)
curClass.instances.foreach { case (_, inst) =>
inst match {
case pis: ParseInstanceSpec =>
res ++= getOpaqueDataTypes(pis.dataType, curClass)
res ++= getExternalDataTypes(pis.dataType, curClass)
case _ => None
}
}

// Traverse all nested types recursively
curClass.types.foreach { case (_, nestedType) =>
res ++= getOpaqueClasses(nestedType)
res ++= getExternalClasses(nestedType)
}

res
}

def getOpaqueDataTypes(dataType: DataType, curClass: ClassSpec): Iterable[ClassSpec] = {
def getExternalDataTypes(dataType: DataType, curClass: ClassSpec): Iterable[ClassSpec] = {
dataType match {
case ut: UserType =>
if (ut.isExternal(curClass)) {
Expand All @@ -38,10 +38,10 @@ object TypeProcessor {
}
case st: SwitchType =>
st.cases.flatMap { case (_, ut) =>
getOpaqueDataTypes(ut, curClass)
getExternalDataTypes(ut, curClass)
}
case _ =>
// all other types are not opaque external user types
// all other types are not external user types
List()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class CppCompiler(
}
}

override def opaqueClassDeclaration(classSpec: ClassSpec): Unit = {
override def externalClassDeclaration(classSpec: ClassSpec): Unit = {
classForwardDeclaration(classSpec.name)
importListHdr.addLocal(outFileNameHeader(classSpec.name.head))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class JavaScriptCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
out.puts("});")
}

override def opaqueClassDeclaration(classSpec: ClassSpec): Unit = {
override def externalClassDeclaration(classSpec: ClassSpec): Unit = {
val className = type2class(classSpec.name.head)
importList.add(s"./$className")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class LuaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
override def outImports(topClass: ClassSpec) =
importList.toList.mkString("", "\n", "\n")

override def opaqueClassDeclaration(classSpec: ClassSpec): Unit =
override def externalClassDeclaration(classSpec: ClassSpec): Unit =
importList.add("require(\"" + classSpec.name.head + "\")")

override def fileHeader(topClassName: String): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class NimCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
out.puts
}

override def opaqueClassDeclaration(classSpec: ClassSpec): Unit =
override def externalClassDeclaration(classSpec: ClassSpec): Unit =
importList.add(classSpec.name.head)
override def innerEnums = false
override val translator: NimTranslator = new NimTranslator(typeProvider, importList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class PerlCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
out.puts("1;")
}

override def opaqueClassDeclaration(classSpec: ClassSpec): Unit =
override def externalClassDeclaration(classSpec: ClassSpec): Unit =
importList.add(type2class(classSpec.name.head))

override def classHeader(name: List[String]): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class PythonCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
out.puts
}

override def opaqueClassDeclaration(classSpec: ClassSpec): Unit = {
override def externalClassDeclaration(classSpec: ClassSpec): Unit = {
val name = classSpec.name.head
importList.add(
if (config.pythonPackage.nonEmpty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class RustCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
out.puts
}

override def opaqueClassDeclaration(classSpec: ClassSpec): Unit = {
override def externalClassDeclaration(classSpec: ClassSpec): Unit = {
val name = type2class(classSpec.name.last)
val pkg = type2classAbs(classSpec.name)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ abstract class LanguageCompiler(
def fileFooter(topClassName: String): Unit = {}

/**
* Outputs declaration of "opaque class", i.e. class that will be referred to in this file, but
* Outputs declaration of "external class", i.e. class that will be referred to in this file, but
* not declared here. Some languages require either a "forward declaration" in this case, or a
* statement to import that class, or something similar. Called once per each opaque class.
* statement to import that class, or something similar. Called once per each external class.
* @param classSpec
*/
def opaqueClassDeclaration(classSpec: ClassSpec): Unit = {}
def externalClassDeclaration(classSpec: ClassSpec): Unit = {}

def classDoc(name: List[String], doc: DocSpec): Unit = {}
def classHeader(name: List[String]): Unit
Expand Down

0 comments on commit 128e179

Please sign in to comment.