Skip to content

Commit

Permalink
#22: Forward full instances data to compilers
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Jan 11, 2020
1 parent eee30fd commit c76750e
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 57 deletions.
4 changes: 2 additions & 2 deletions shared/src/main/scala/io/kaitai/struct/ClassCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ class ClassCompiler(

if (!lang.innerDocstrings)
compileInstanceDoc(instName, instSpec)
lang.instanceHeader(className, instName, dataType, instSpec.isNullable)
lang.instanceHeader(className, instName, instSpec)
if (lang.innerDocstrings)
compileInstanceDoc(instName, instSpec)
lang.instanceCheckCacheAndReturn(instName, dataType)
Expand All @@ -344,7 +344,7 @@ class ClassCompiler(
}

def compileInstanceDeclaration(instName: InstanceIdentifier, instSpec: InstanceSpec): Unit =
lang.instanceDeclaration(instName, instSpec.dataTypeComposite, instSpec.isNullable)
lang.instanceDeclaration(instName, instSpec)

def compileEnum(curClass: ClassSpec, enumColl: EnumSpec): Unit =
lang.enumDeclaration(curClass.name, enumColl.name.last, enumColl.sortedSeq)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class GoClassCompiler(

if (!instSpec.doc.isEmpty)
lang.attributeDoc(instName, instSpec.doc)
lang.instanceHeader(className, instName, dataType, instSpec.isNullable)
lang.instanceHeader(className, instName, instSpec)
lang.instanceCheckCacheAndReturn(instName, dataType)

instSpec match {
Expand Down
12 changes: 6 additions & 6 deletions shared/src/main/scala/io/kaitai/struct/RustClassCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ class RustClassCompiler(

// Basic struct declaration
lang.classHeader(curClass.name)

compileAttrDeclarations(curClass.seq ++ extraAttrs)
curClass.instances.foreach { case (instName, instSpec) =>
compileInstanceDeclaration(instName, instSpec)
}

// Constructor = Read() function
compileReadFunction(curClass)

compileInstances(curClass)

compileAttrReaders(curClass.seq ++ extraAttrs)
Expand All @@ -63,9 +63,9 @@ class RustClassCompiler(
case Some(fe: FixedEndian) => Some(fe)
case _ => None
}

lang.readHeader(defEndian, false)

compileSeq(curClass.seq, defEndian)
lang.classConstructorFooter
}
Expand All @@ -85,7 +85,7 @@ class RustClassCompiler(

if (!instSpec.doc.isEmpty)
lang.attributeDoc(instName, instSpec.doc)
lang.instanceHeader(className, instName, dataType, instSpec.isNullable)
lang.instanceHeader(className, instName, instSpec)
lang.instanceCheckCacheAndReturn(instName, dataType)

instSpec match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,13 +479,13 @@ class CSharpCompiler(val typeProvider: ClassTypeProvider, config: RuntimeConfig)

//</editor-fold>

override def instanceDeclaration(attrName: InstanceIdentifier, attrType: DataType, isNullable: Boolean): Unit = {
out.puts(s"private bool ${flagForInstName(attrName)};")
out.puts(s"private ${kaitaiType2NativeTypeNullable(attrType, isNullable)} ${privateMemberName(attrName)};")
override def instanceDeclaration(instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = {
out.puts(s"private bool ${flagForInstName(instName)};")
out.puts(s"private ${kaitaiType2NativeTypeNullable(instSpec.dataTypeComposite, instSpec.isNullable)} ${privateMemberName(instName)};")
}

override def instanceHeader(className: String, instName: InstanceIdentifier, dataType: DataType, isNullable: Boolean): Unit = {
out.puts(s"public ${kaitaiType2NativeTypeNullable(dataType, isNullable)} ${publicMemberName(instName)}")
override def instanceHeader(className: String, instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = {
out.puts(s"public ${kaitaiType2NativeTypeNullable(instSpec.dataTypeComposite, instSpec.isNullable)} ${publicMemberName(instName)}")
out.puts("{")
out.inc
out.puts("get")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -817,19 +817,19 @@ class CppCompiler(

override def switchBytesOnlyAsRaw = true

override def instanceDeclaration(attrName: InstanceIdentifier, attrType: DataType, isNullable: Boolean): Unit = {
override def instanceDeclaration(instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = {
ensureMode(PrivateAccess)
outHdr.puts(s"bool ${calculatedFlagForName(attrName)};")
outHdr.puts(s"${kaitaiType2NativeType(attrType)} ${privateMemberName(attrName)};")
declareNullFlag(attrName, isNullable)
outHdr.puts(s"bool ${calculatedFlagForName(instName)};")
outHdr.puts(s"${kaitaiType2NativeType(instSpec.dataTypeComposite)} ${privateMemberName(instName)};")
declareNullFlag(instName, instSpec.isNullable)
}

override def instanceHeader(className: List[String], instName: InstanceIdentifier, dataType: DataType, isNullable: Boolean): Unit = {
override def instanceHeader(className: List[String], instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = {
ensureMode(PublicAccess)
outHdr.puts(s"${kaitaiType2NativeType(dataType.asNonOwning)} ${publicMemberName(instName)}();")
outHdr.puts(s"${kaitaiType2NativeType(instSpec.dataTypeComposite.asNonOwning)} ${publicMemberName(instName)}();")

outSrc.puts
outSrc.puts(s"${kaitaiType2NativeType(dataType.asNonOwning, true)} ${types2class(className)}::${publicMemberName(instName)}() {")
outSrc.puts(s"${kaitaiType2NativeType(instSpec.dataTypeComposite.asNonOwning, true)} ${types2class(className)}::${publicMemberName(instName)}() {")
outSrc.inc
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,15 +441,15 @@ class GoCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
out.inc
}

override def instanceDeclaration(attrName: InstanceIdentifier, attrType: DataType, isNullable: Boolean): Unit = {
out.puts(s"${calculatedFlagForName(attrName)} bool")
out.puts(s"${idToStr(attrName)} ${kaitaiType2NativeType(attrType)}")
override def instanceDeclaration(instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = {
out.puts(s"${calculatedFlagForName(instName)} bool")
out.puts(s"${idToStr(instName)} ${kaitaiType2NativeType(instSpec.dataTypeComposite)}")
}

override def instanceHeader(className: List[String], instName: InstanceIdentifier, dataType: DataType, isNullable: Boolean): Unit = {
out.puts(s"func (this *${types2class(className)}) ${publicMemberName(instName)}() (v ${kaitaiType2NativeType(dataType)}, err error) {")
override def instanceHeader(className: List[String], instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = {
out.puts(s"func (this *${types2class(className)}) ${publicMemberName(instName)}() (v ${kaitaiType2NativeType(instSpec.dataTypeComposite)}, err error) {")
out.inc
translator.returnRes = Some(dataType match {
translator.returnRes = Some(instSpec.dataTypeComposite match {
case _: IntType => "0"
case _: BooleanType => "false"
case _: StrType => "\"\""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -615,12 +615,12 @@ class JavaCompiler(val typeProvider: ClassTypeProvider, config: RuntimeConfig)

//</editor-fold>

override def instanceDeclaration(attrName: InstanceIdentifier, attrType: DataType, isNullable: Boolean): Unit = {
out.puts(s"private ${kaitaiType2JavaTypeBoxed(attrType)} ${idToStr(attrName)};")
override def instanceDeclaration(instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = {
out.puts(s"private ${kaitaiType2JavaTypeBoxed(instSpec.dataTypeComposite)} ${idToStr(instName)};")
}

override def instanceHeader(className: String, instName: InstanceIdentifier, dataType: DataType, isNullable: Boolean): Unit = {
out.puts(s"public ${kaitaiType2JavaTypeBoxed(dataType)} ${idToStr(instName)}() {")
override def instanceHeader(className: String, instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = {
out.puts(s"public ${kaitaiType2JavaTypeBoxed(instSpec.dataTypeComposite)} ${idToStr(instName)}() {")
out.inc
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ class JavaScriptCompiler(val typeProvider: ClassTypeProvider, config: RuntimeCon

//</editor-fold>

override def instanceHeader(className: List[String], instName: InstanceIdentifier, dataType: DataType, isNullable: Boolean): Unit = {
override def instanceHeader(className: List[String], instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = {
out.puts(s"Object.defineProperty(${type2class(className.last)}.prototype, '${publicMemberName(instName)}', {")
out.inc
out.puts("get: function() {")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class LuaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
override def alignToByte(io: String): Unit =
out.puts(s"$io:align_to_byte()")

override def instanceHeader(className: List[String], instName: InstanceIdentifier, dataType: DataType, isNullable: Boolean): Unit = {
override def instanceHeader(className: List[String], instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = {
out.puts(s"${types2class(className)}.property.${publicMemberName(instName)} = {}")
out.puts(s"function ${types2class(className)}.property.${publicMemberName(instName)}:get()")
out.inc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ class PHPCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)

override def switchEnd(): Unit = universalFooter

override def instanceHeader(className: List[String], instName: InstanceIdentifier, dataType: DataType, isNullable: Boolean): Unit = {
override def instanceHeader(className: List[String], instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = {
out.puts(s"public function ${idToStr(instName)}() {")
out.inc
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ class PerlCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
def onComparisonExpr(condition: Ast.expr) =
Ast.expr.Compare(Ast.expr.Name(Ast.identifier("_on")), Ast.cmpop.Eq, condition)

override def instanceHeader(className: List[String], instName: InstanceIdentifier, dataType: DataType, isNullable: Boolean): Unit = {
override def instanceHeader(className: List[String], instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = {
out.puts
out.puts(s"sub ${instName.name} {")
out.inc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ class PythonCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)

override def switchEnd(): Unit = {}

override def instanceHeader(className: String, instName: InstanceIdentifier, dataType: DataType, isNullable: Boolean): Unit = {
override def instanceHeader(className: String, instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = {
out.puts("@property")
out.puts(s"def ${publicMemberName(instName)}(self):")
out.inc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ class RubyCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
override def switchEnd(): Unit =
out.puts("end")

override def instanceHeader(className: String, instName: InstanceIdentifier, dataType: DataType, isNullable: Boolean): Unit = {
override def instanceHeader(className: String, instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = {
out.puts(s"def ${instName.name}")
out.inc
}
Expand Down
30 changes: 15 additions & 15 deletions shared/src/main/scala/io/kaitai/struct/languages/RustCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class RustCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
override def opaqueClassDeclaration(classSpec: ClassSpec): Unit = {
val name = type2class(classSpec.name.last)
val pkg = type2classAbs(classSpec.name)

importList.add(s"$pkg::$name")
}

Expand All @@ -77,7 +77,7 @@ class RustCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)

out.puts(s"impl KaitaiStruct for ${type2class(name)} {")
out.inc

// Parameter names
val pIo = paramName(IoIdentifier)
val pParent = paramName(ParentIdentifier)
Expand Down Expand Up @@ -113,7 +113,7 @@ class RustCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
}

override def runReadCalc(): Unit = {

}

override def readHeader(endian: Option[FixedEndian], isEmpty: Boolean) = {
Expand Down Expand Up @@ -146,7 +146,7 @@ class RustCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
}

override def attributeReader(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = {

}

override def universalDoc(doc: DocSpec): Unit = {
Expand Down Expand Up @@ -422,8 +422,8 @@ class RustCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)

override def switchEnd(): Unit = universalFooter

override def instanceDeclaration(attrName: InstanceIdentifier, attrType: DataType, isNullable: Boolean): Unit = {
out.puts(s" pub ${idToStr(attrName)}: Option<${kaitaiType2NativeType(attrType)}>,")
override def instanceDeclaration(instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = {
out.puts(s" pub ${idToStr(instName)}: Option<${kaitaiType2NativeType(instSpec.dataTypeComposite)}>,")
}

override def instanceDeclHeader(className: List[String]): Unit = {
Expand All @@ -435,8 +435,8 @@ class RustCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
out.inc
}

override def instanceHeader(className: List[String], instName: InstanceIdentifier, dataType: DataType, isNullable: Boolean): Unit = {
out.puts(s"fn ${idToStr(instName)}(&mut self) -> ${kaitaiType2NativeType(dataType)} {")
override def instanceHeader(className: List[String], instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = {
out.puts(s"fn ${idToStr(instName)}(&mut self) -> ${kaitaiType2NativeType(instSpec.dataTypeComposite)} {")
out.inc
}

Expand All @@ -458,7 +458,7 @@ class RustCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)

out.puts(s"enum $enumClass {")
out.inc

enumColl.foreach { case (id, label) =>
universalDoc(label.doc)
out.puts(s"${value2Const(label.name)},")
Expand Down Expand Up @@ -494,7 +494,7 @@ class RustCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
override def localTemporaryName(id: Identifier): String = s"$$_t_${idToStr(id)}"

override def paramName(id: Identifier): String = s"${idToStr(id)}"

def kaitaiType2NativeType(attrType: DataType): String = {
attrType match {
case Int1Type(false) => "u8"
Expand Down Expand Up @@ -523,7 +523,7 @@ class RustCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
case Some(cs) => s"Box<${type2class(cs.name)}>"
case None => s"Box<${type2class(t.name)}>"
}

case t: EnumType => t.enumSpec match {
case Some(cs) => s"Box<${type2class(cs.name)}>"
case None => s"Box<${type2class(t.name)}>"
Expand All @@ -533,11 +533,11 @@ class RustCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)

case KaitaiStreamType => s"Option<Box<KaitaiStream>>"
case KaitaiStructType | CalcKaitaiStructType => s"Option<Box<KaitaiStruct>>"

case st: SwitchType => kaitaiType2NativeType(st.combinedType)
}
}

def kaitaiType2Default(attrType: DataType): String = {
attrType match {
case Int1Type(false) => "0"
Expand Down Expand Up @@ -569,12 +569,12 @@ class RustCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)

case KaitaiStreamType => "None"
case KaitaiStructType => "None"

case _: SwitchType => ""
// TODO
}
}

def type2class(names: List[String]) = types2classRel(names)

def type2classAbs(names: List[String]) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ abstract class LanguageCompiler(
def instanceDeclHeader(className: List[String]): Unit = {}
def instanceClear(instName: InstanceIdentifier): Unit = {}
def instanceSetCalculated(instName: InstanceIdentifier): Unit = {}
def instanceDeclaration(attrName: InstanceIdentifier, attrType: DataType, isNullable: Boolean): Unit = attributeDeclaration(attrName, attrType, isNullable)
def instanceHeader(className: List[String], instName: InstanceIdentifier, dataType: DataType, isNullable: Boolean): Unit
def instanceDeclaration(instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = attributeDeclaration(instName, instSpec.dataTypeComposite, instSpec.isNullable)
def instanceHeader(className: List[String], instName: InstanceIdentifier, instSpec: InstanceSpec): Unit
def instanceFooter: Unit
def instanceCheckCacheAndReturn(instName: InstanceIdentifier, dataType: DataType): Unit
def instanceReturn(instName: InstanceIdentifier, attrType: DataType): Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ trait NoNeedForFullClassPath {
classConstructorHeader(name.last, parentType, rootClassName.last, isHybrid, params)
def classConstructorHeader(name: String, parentType: DataType, rootClassName: String, isHybrid: Boolean, params: List[ParamDefSpec]): Unit

def instanceHeader(className: List[String], instName: InstanceIdentifier, dataType: DataType, isNullable: Boolean): Unit =
instanceHeader(className.last, instName, dataType, isNullable)
def instanceHeader(className: String, instName: InstanceIdentifier, dataType: DataType, isNullable: Boolean): Unit
def instanceHeader(className: List[String], instName: InstanceIdentifier, instSpec: InstanceSpec): Unit =
instanceHeader(className.last, instName, instSpec)
def instanceHeader(className: String, instName: InstanceIdentifier, instSpec: InstanceSpec): Unit

def enumDeclaration(curClass: List[String], enumName: String, enumColl: Seq[(Long, EnumValueSpec)]): Unit =
enumDeclaration(curClass.last, enumName, enumColl.map((x) => (x._1, x._2.name)))
Expand Down

0 comments on commit c76750e

Please sign in to comment.