Skip to content

Commit

Permalink
C#: fix missing import of List for array params
Browse files Browse the repository at this point in the history
Fixes test ImportsParamsDefArrayUsertypeImported for C# targets other
than `netcore6.0` where it already passes, i.e. for
`csharp/net48-windows-x64` and
`csharp/netcore{2.2.103,3.0.100}-linux-x86_64`
  • Loading branch information
generalmimon committed Apr 15, 2024
1 parent 32227fc commit db78a74
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,12 @@ class CSharpCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)

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

def kaitaiType2NativeType(attrType: DataType): String =
CSharpCompiler.kaitaiType2NativeType(importList, attrType)

def kaitaiType2NativeTypeNullable(t: DataType, isNullable: Boolean): String =
CSharpCompiler.kaitaiType2NativeTypeNullable(importList, t, isNullable)

override def ksErrorName(err: KSError): String = CSharpCompiler.ksErrorName(err)

override def attrValidateExpr(
Expand Down Expand Up @@ -620,7 +626,7 @@ object CSharpCompiler extends LanguageCompilerStatic
* @param attrType KS data type
* @return .NET data type
*/
def kaitaiType2NativeType(attrType: DataType): String = {
def kaitaiType2NativeType(importList: ImportList, attrType: DataType): String = {
attrType match {
case Int1Type(false) => "byte"
case IntMultiType(false, Width2, _) => "ushort"
Expand Down Expand Up @@ -651,14 +657,17 @@ object CSharpCompiler extends LanguageCompilerStatic
case t: UserType => types2class(t.name)
case EnumType(name, _) => types2class(name)

case at: ArrayType => s"List<${kaitaiType2NativeType(at.elType)}>"
case at: ArrayType => {
importList.add("System.Collections.Generic")
s"List<${kaitaiType2NativeType(importList, at.elType)}>"
}

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

def kaitaiType2NativeTypeNullable(t: DataType, isNullable: Boolean): String = {
val r = kaitaiType2NativeType(t)
def kaitaiType2NativeTypeNullable(importList: ImportList, t: DataType, isNullable: Boolean): String = {
val r = kaitaiType2NativeType(importList, t)
if (isNullable) {
t match {
case _: NumericType | _: BooleanType => s"$r?"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import io.kaitai.struct.languages.CSharpCompiler

class CSharpTranslator(provider: TypeProvider, importList: ImportList) extends BaseTranslator(provider) {
override def doArrayLiteral(t: DataType, value: Seq[expr]): String = {
val nativeType = CSharpCompiler.kaitaiType2NativeType(t)
val nativeType = CSharpCompiler.kaitaiType2NativeType(importList, t)
val commaStr = value.map((v) => translate(v)).mkString(", ")

importList.add("System.Collections.Generic")
Expand Down Expand Up @@ -89,7 +89,7 @@ class CSharpTranslator(provider: TypeProvider, importList: ImportList) extends B
override def doIfExp(condition: expr, ifTrue: expr, ifFalse: expr): String =
s"(${translate(condition)} ? ${translate(ifTrue)} : ${translate(ifFalse)})"
override def doCast(value: Ast.expr, typeName: DataType): String =
s"((${CSharpCompiler.kaitaiType2NativeType(typeName)}) (${translate(value)}))"
s"((${CSharpCompiler.kaitaiType2NativeType(importList, typeName)}) (${translate(value)}))"

// Predefined methods of various types
override def strToInt(s: expr, base: expr): String = {
Expand Down

0 comments on commit db78a74

Please sign in to comment.