diff --git a/shared/src/main/scala/io/kaitai/struct/ClassCompiler.scala b/shared/src/main/scala/io/kaitai/struct/ClassCompiler.scala index 3de1fc6c8..f4d3826f2 100644 --- a/shared/src/main/scala/io/kaitai/struct/ClassCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/ClassCompiler.scala @@ -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) @@ -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) diff --git a/shared/src/main/scala/io/kaitai/struct/GoClassCompiler.scala b/shared/src/main/scala/io/kaitai/struct/GoClassCompiler.scala index 9c5af1597..4efebc6b1 100644 --- a/shared/src/main/scala/io/kaitai/struct/GoClassCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/GoClassCompiler.scala @@ -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 { diff --git a/shared/src/main/scala/io/kaitai/struct/RustClassCompiler.scala b/shared/src/main/scala/io/kaitai/struct/RustClassCompiler.scala index a148abc43..3c75039e2 100644 --- a/shared/src/main/scala/io/kaitai/struct/RustClassCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/RustClassCompiler.scala @@ -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) @@ -63,9 +63,9 @@ class RustClassCompiler( case Some(fe: FixedEndian) => Some(fe) case _ => None } - + lang.readHeader(defEndian, false) - + compileSeq(curClass.seq, defEndian) lang.classConstructorFooter } @@ -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 { diff --git a/shared/src/main/scala/io/kaitai/struct/languages/CSharpCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/CSharpCompiler.scala index 905de245d..c356c0265 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/CSharpCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/CSharpCompiler.scala @@ -479,13 +479,13 @@ class CSharpCompiler(val typeProvider: ClassTypeProvider, config: RuntimeConfig) // - 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") diff --git a/shared/src/main/scala/io/kaitai/struct/languages/CppCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/CppCompiler.scala index 901eadd88..fc32492f4 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/CppCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/CppCompiler.scala @@ -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 } diff --git a/shared/src/main/scala/io/kaitai/struct/languages/GoCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/GoCompiler.scala index ed965db3e..b00d41722 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/GoCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/GoCompiler.scala @@ -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 => "\"\"" diff --git a/shared/src/main/scala/io/kaitai/struct/languages/JavaCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/JavaCompiler.scala index d48ac11d1..3cea44586 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/JavaCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/JavaCompiler.scala @@ -615,12 +615,12 @@ class JavaCompiler(val typeProvider: ClassTypeProvider, config: RuntimeConfig) // - 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 } diff --git a/shared/src/main/scala/io/kaitai/struct/languages/JavaScriptCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/JavaScriptCompiler.scala index 3810ae767..3a916f420 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/JavaScriptCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/JavaScriptCompiler.scala @@ -487,7 +487,7 @@ class JavaScriptCompiler(val typeProvider: ClassTypeProvider, config: RuntimeCon // - 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() {") diff --git a/shared/src/main/scala/io/kaitai/struct/languages/LuaCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/LuaCompiler.scala index 34d774810..6c538404a 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/LuaCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/LuaCompiler.scala @@ -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 diff --git a/shared/src/main/scala/io/kaitai/struct/languages/PHPCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/PHPCompiler.scala index 14212d406..07f420292 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/PHPCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/PHPCompiler.scala @@ -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 } diff --git a/shared/src/main/scala/io/kaitai/struct/languages/PerlCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/PerlCompiler.scala index d2735bc2b..687eb6630 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/PerlCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/PerlCompiler.scala @@ -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 diff --git a/shared/src/main/scala/io/kaitai/struct/languages/PythonCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/PythonCompiler.scala index fd1d65d7c..cebccf72f 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/PythonCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/PythonCompiler.scala @@ -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 diff --git a/shared/src/main/scala/io/kaitai/struct/languages/RubyCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/RubyCompiler.scala index 838f403ad..5b48413e9 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/RubyCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/RubyCompiler.scala @@ -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 } diff --git a/shared/src/main/scala/io/kaitai/struct/languages/RustCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/RustCompiler.scala index 73969ac78..f9599a935 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/RustCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/RustCompiler.scala @@ -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") } @@ -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) @@ -113,7 +113,7 @@ class RustCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig) } override def runReadCalc(): Unit = { - + } override def readHeader(endian: Option[FixedEndian], isEmpty: Boolean) = { @@ -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 = { @@ -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 = { @@ -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 } @@ -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)},") @@ -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" @@ -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)}>" @@ -533,11 +533,11 @@ class RustCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig) case KaitaiStreamType => s"Option>" case KaitaiStructType | CalcKaitaiStructType => s"Option>" - + case st: SwitchType => kaitaiType2NativeType(st.combinedType) } } - + def kaitaiType2Default(attrType: DataType): String = { attrType match { case Int1Type(false) => "0" @@ -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]) = diff --git a/shared/src/main/scala/io/kaitai/struct/languages/components/LanguageCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/components/LanguageCompiler.scala index 229cbdabd..fb3797aaa 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/components/LanguageCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/components/LanguageCompiler.scala @@ -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 diff --git a/shared/src/main/scala/io/kaitai/struct/languages/components/NoNeedForFullClassPath.scala b/shared/src/main/scala/io/kaitai/struct/languages/components/NoNeedForFullClassPath.scala index 63be66119..4e427eab5 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/components/NoNeedForFullClassPath.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/components/NoNeedForFullClassPath.scala @@ -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)))