Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update C# ADT alias render to use type names instead of wire ids. #521

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,17 @@ mixin TIfNode {
+ TypeInfo
}

// Aliased (with reserved keywords as aliases)
adt AliasedAdt {
EventData as event
PublicData as public
}

data EventData {
}

data PublicData {
}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,33 +111,33 @@ class CSharpTranslator(ts: Typespace, options: CSharpTranslatorOptions) extends
// it has no ambiguous name with regards to the rest. However, methods for adt will
// have the same name making it ambiguous, so we bail out here by providing
// an FQN
val nonambName = if (needsFQN.isDefined && needsFQN.get.usingName == "") CSharpType(member.typeId).renderType(true) else s"_${member.wireId}"
val nonambName = if (needsFQN.isDefined && needsFQN.get.usingName == "") CSharpType(member.typeId).renderType(true) else s"_${member.typename}"
val operators =
s""" public static explicit operator $nonambName(${member.wireId} m) {
s""" public static explicit operator $nonambName(${member.typename} m) {
| return m.Value;
| }
|
| public static explicit operator ${member.wireId}($nonambName m) {
| return new ${member.wireId}(m);
| public static explicit operator ${member.typename}($nonambName m) {
| return new ${member.typename}(m);
| }
""".stripMargin

val operatorsDummy =
s""" // We would normally want to have an operator, but unfortunately if it is an interface,
| // it will fail on "user-defined conversions to or from an interface are not allowed".
| // public static explicit operator $nonambName(${member.wireId} m) {
| // public static explicit operator $nonambName(${member.typename} m) {
| // return m.Value;
| // }
| //
| // public static explicit operator ${member.wireId}($nonambName m) {
| // return new ${member.wireId}(m);
| // public static explicit operator ${member.typename}($nonambName m) {
| // return new ${member.typename}(m);
| // }
""".stripMargin

// val memberType = CSharpType(member.typeId)
s"""public sealed class ${member.wireId}: $adtName {
s"""public sealed class ${member.typename}: $adtName {
| public $nonambName Value { get; private set; }
| public ${member.wireId}($nonambName value) {
| public ${member.typename}($nonambName value) {
| this.Value = value;
| }
|
Expand All @@ -151,7 +151,7 @@ class CSharpTranslator(ts: Typespace, options: CSharpTranslatorOptions) extends
}

protected def renderAdtUsings(m: AdtMember)(implicit im: CSharpImports, ts: Typespace): String = {
s"using _${m.wireId} = ${CSharpType(m.typeId).renderType(true)};"
s"using _${m.typename} = ${CSharpType(m.typeId).renderType(true)};"
}

protected def renderAdtImpl(adtName: String, members: List[AdtMember], renderUsings: Boolean = true)(implicit im: CSharpImports, ts: Typespace): String = {
Expand All @@ -162,7 +162,7 @@ class CSharpTranslator(ts: Typespace, options: CSharpTranslatorOptions) extends
|${ext.preModelEmit(ctx, adt)}
|public abstract class $adtName {
| public interface I${adtName}Visitor {
|${members.map(m => s" void Visit(${m.wireId} visitor);").mkString("\n")}
|${members.map(m => s" void Visit(${m.typename} visitor);").mkString("\n")}
| }
|
| public abstract void Visit(I${adtName}Visitor visitor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import izumi.fundamentals.platform.language.Quirks.discard
import izumi.fundamentals.platform.strings.IzString._
import izumi.idealingua.model.common.TypeId._
import izumi.idealingua.model.common.{Generic, Primitive, TypeId}
import izumi.idealingua.model.problems.IDLException
import izumi.idealingua.model.il.ast.typed.DefMethod
import izumi.idealingua.model.il.ast.typed.DefMethod.Output.{Alternative, Singular}
import izumi.idealingua.model.il.ast.typed.TypeDef._
import izumi.idealingua.model.problems.IDLException
import izumi.idealingua.model.typespace.Typespace
import izumi.idealingua.translator.tocsharp.types.{CSharpClass, CSharpField, CSharpType}
import izumi.idealingua.translator.tocsharp.{CSTContext, CSharpImports}
Expand Down Expand Up @@ -412,9 +412,9 @@ object JsonNetExtension extends CSharpTranslatorExtension {
| public override void WriteJson(JsonWriter writer, ${i.id.name} al, JsonSerializer serializer) {
| writer.WriteStartObject();
|${i.alternatives
.map(m => s"""if (al is ${i.id.name}.${m.wireId}) {
.map(m => s"""if (al is ${i.id.name}.${m.typename}) {
| writer.WritePropertyName("${m.wireId}");
| var v = (al as ${i.id.name}.${m.wireId}).Value;
| var v = (al as ${i.id.name}.${m.typename}).Value;
|${renderSerialize(m.typeId, "v").shift(4)}
|} else""".stripMargin).mkString("\n").shift(8)}
| {
Expand All @@ -430,7 +430,7 @@ object JsonNetExtension extends CSharpTranslatorExtension {
|${i.alternatives
.map(m => s"""case "${m.wireId}": {
| var v = serializer.Deserialize<${CSharpType(m.typeId).renderType(true)}>(kv.Value.CreateReader());
| return new ${i.id.name}.${m.wireId}(v);
| return new ${i.id.name}.${m.typename}(v);
|}
""".stripMargin).mkString("\n").shift(12)}
| default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ object NUnitExtension extends CSharpTranslatorExtension {
val code =
s"""public static class ${name}TestHelper {
| public static $name Create() {
| return new $name.${adt.wireId}($testValue);
| return new $name.${adt.typename}($testValue);
| }
|}
|
Expand Down
Loading