From 8a573b876c5adb680f32090a15fe04891dc32e6d Mon Sep 17 00:00:00 2001 From: Jeremy Faivre Date: Sat, 2 Mar 2024 17:13:29 +0100 Subject: [PATCH] Do not use specific values for object, dynamic or string. Instead use CSInst or CSValue accordingly --- src/cscompiler/ast/CSType.hx | 17 ----------- src/cscompiler/components/CSCompiler_Type.hx | 30 ++++++++++++-------- 2 files changed, 18 insertions(+), 29 deletions(-) diff --git a/src/cscompiler/ast/CSType.hx b/src/cscompiler/ast/CSType.hx index af4ca3c..44334c9 100644 --- a/src/cscompiler/ast/CSType.hx +++ b/src/cscompiler/ast/CSType.hx @@ -35,23 +35,6 @@ enum CSType { **/ CSFunction(args: Array, ret: CSType); - /** - C# `object` type - **/ - CSObject; - - /** - C# `dynamic` type. See https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/reference-types#the-dynamic-type to see how it - is different from `object` type. (the gist of it: `dynamic` is similar to - Haxe's `Dynamic` type while `object` is more like Haxe's `Any` type). - **/ - CSDynamic; - - /** - A C# string type - **/ - CSString; - /** A C# value type (primitives like `int`, `bool`... or `struct` types) type. Optionally nullable (`int?` etc...) **/ diff --git a/src/cscompiler/components/CSCompiler_Type.hx b/src/cscompiler/components/CSCompiler_Type.hx index 5818017..939193f 100644 --- a/src/cscompiler/components/CSCompiler_Type.hx +++ b/src/cscompiler/components/CSCompiler_Type.hx @@ -48,10 +48,22 @@ class CSCompiler_Type extends CSCompiler_Base { ); } case TInst(clsRef, params): { - CSInst( - compileClassType(clsRef.get()), - compileTypeParams(params) - ); + final cls = clsRef.get(); + if (cls.hasMeta(':struct')) { + // When using @:struct meta, we are + // dealing with a C# `struct` value type + CSValue( + compileClassType(cls), + compileTypeParams(params), + false + ); + } + else { + CSInst( + compileClassType(cls), + compileTypeParams(params) + ); + } } case TType(_, _): { compile(Context.follow(type), pos); @@ -61,11 +73,11 @@ class CSCompiler_Type extends CSCompiler_Base { } case TAnonymous(anonRef): { // For now, we simply use `object` type. Might change later - CSObject; + CSInst('object', []); } case TDynamic(maybeType): { // TODO, returning `dynamic` type for now here - CSDynamic; + CSInst('dynamic', []); } case TLazy(callback): { compile(callback(), pos); @@ -132,12 +144,6 @@ class CSCompiler_Type extends CSCompiler_Base { type; case CSFunction(args, ret): type; - case CSObject: - type; - case CSDynamic: - type; - case CSString: - type; case CSValue(typePath, params, _): // Value types need to be explicitly nullable, // whereas other types are already implicitly