From db78a74647e690f807fda9dfad85da865d958824 Mon Sep 17 00:00:00 2001 From: Petr Pucil Date: Mon, 15 Apr 2024 21:13:31 +0200 Subject: [PATCH] C#: fix missing import of `List` for array `params` 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` --- .../struct/languages/CSharpCompiler.scala | 19 ++++++++++++++----- .../struct/translators/CSharpTranslator.scala | 4 ++-- 2 files changed, 16 insertions(+), 7 deletions(-) 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 8ad2a541d..b3570f1b6 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/CSharpCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/CSharpCompiler.scala @@ -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( @@ -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" @@ -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?" diff --git a/shared/src/main/scala/io/kaitai/struct/translators/CSharpTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/CSharpTranslator.scala index 8f8211a62..a105a1b00 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/CSharpTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/CSharpTranslator.scala @@ -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") @@ -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 = {