From 052389f3b46dd3271dca652b524ba9af4519a020 Mon Sep 17 00:00:00 2001 From: Andrii Kurdiumov Date: Thu, 5 Sep 2024 18:01:33 +0500 Subject: [PATCH] Add support for unsigned type alias Without that assert macro does not working. --- Cesium.CodeGen/Extensions/TypeSystemEx.cs | 3 ++- Cesium.CodeGen/Ir/Expressions/TypeCastExpression.cs | 2 +- Cesium.CodeGen/Ir/Types/CTypeSystem.cs | 5 +++-- Cesium.IntegrationTests/type_casts.c | 2 ++ 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Cesium.CodeGen/Extensions/TypeSystemEx.cs b/Cesium.CodeGen/Extensions/TypeSystemEx.cs index ef9110a7..25e3f197 100644 --- a/Cesium.CodeGen/Extensions/TypeSystemEx.cs +++ b/Cesium.CodeGen/Extensions/TypeSystemEx.cs @@ -237,6 +237,7 @@ public static bool IsUnsignedInteger(this IType t) || t.IsEqualTo(CTypeSystem.Char) || t.IsEqualTo(CTypeSystem.UnsignedChar) || t.IsEqualTo(CTypeSystem.UnsignedShort) + || t.IsEqualTo(CTypeSystem.Unsigned) || t.IsEqualTo(CTypeSystem.UnsignedInt) || t.IsEqualTo(CTypeSystem.UnsignedLong) || t.IsEqualTo(CTypeSystem.NativeUInt); @@ -271,7 +272,7 @@ public static IType GetCommonNumericType(IType a, IType b) // Otherwise, if both operands have signed integer types or both have unsigned integer types, // the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank. var signedTypes = new[] { CTypeSystem.SignedChar, CTypeSystem.Short, CTypeSystem.Int, CTypeSystem.Long, CTypeSystem.NativeInt}; - var unsignedTypes = new[] { CTypeSystem.Char, CTypeSystem.UnsignedChar, CTypeSystem.UnsignedShort, CTypeSystem.UnsignedInt, CTypeSystem.UnsignedLong, CTypeSystem.NativeUInt }; + var unsignedTypes = new[] { CTypeSystem.Char, CTypeSystem.UnsignedChar, CTypeSystem.UnsignedShort, CTypeSystem.Unsigned, CTypeSystem.UnsignedInt, CTypeSystem.UnsignedLong, CTypeSystem.NativeUInt }; // TODO[#381]: Move NativeInt and NativeUInt accordingly or consider them properly based on the current architecture. var aSignedRank = RankOf(a, signedTypes); diff --git a/Cesium.CodeGen/Ir/Expressions/TypeCastExpression.cs b/Cesium.CodeGen/Ir/Expressions/TypeCastExpression.cs index 5ee1a9cb..9db18cae 100644 --- a/Cesium.CodeGen/Ir/Expressions/TypeCastExpression.cs +++ b/Cesium.CodeGen/Ir/Expressions/TypeCastExpression.cs @@ -57,7 +57,7 @@ public void EmitTo(IEmitScope scope) Add(OpCodes.Conv_U1); else if (TargetType.Equals(C.UnsignedShort)) Add(OpCodes.Conv_U2); - else if (TargetType.Equals(C.UnsignedInt)) + else if (TargetType.Equals(C.UnsignedInt) || TargetType.Equals(C.Unsigned)) Add(OpCodes.Conv_U4); else if (TargetType.Equals(C.UnsignedLong)) Add(OpCodes.Conv_U8); diff --git a/Cesium.CodeGen/Ir/Types/CTypeSystem.cs b/Cesium.CodeGen/Ir/Types/CTypeSystem.cs index 39cf4df6..73dfb905 100644 --- a/Cesium.CodeGen/Ir/Types/CTypeSystem.cs +++ b/Cesium.CodeGen/Ir/Types/CTypeSystem.cs @@ -14,6 +14,7 @@ internal static class CTypeSystem public static IType UnsignedShort { get; } = new PrimitiveType(PrimitiveTypeKind.UnsignedShort); public static IType Int { get; } = new PrimitiveType(PrimitiveTypeKind.Int); public static IType UnsignedInt { get; } = new PrimitiveType(PrimitiveTypeKind.UnsignedInt); + public static IType Unsigned { get; } = new PrimitiveType(PrimitiveTypeKind.Unsigned); public static IType Long { get; } = new PrimitiveType(PrimitiveTypeKind.Long); public static IType UnsignedLong { get; } = new PrimitiveType(PrimitiveTypeKind.UnsignedLong); public static IType CharPtr { get; } = new PrimitiveType(PrimitiveTypeKind.Char).MakePointerType(); @@ -46,7 +47,7 @@ public static bool IsConversionAvailable(IType type, IType targetType) return true; else if (targetType.Equals(UnsignedShort)) return true; - else if (targetType.Equals(UnsignedInt)) + else if (targetType.Equals(UnsignedInt) || targetType.Equals(UnsignedInt)) return true; else if (targetType.Equals(UnsignedLong)) return true; @@ -80,7 +81,7 @@ public static bool IsConversionRequired(IType type, IType targetType) return true; else if (targetType.Equals(UnsignedShort)) return true; - else if (targetType.Equals(UnsignedInt)) + else if (targetType.Equals(UnsignedInt) || targetType.Equals(UnsignedInt)) return true; else if (targetType.Equals(UnsignedLong)) return true; diff --git a/Cesium.IntegrationTests/type_casts.c b/Cesium.IntegrationTests/type_casts.c index 358f6581..6570a19e 100644 --- a/Cesium.IntegrationTests/type_casts.c +++ b/Cesium.IntegrationTests/type_casts.c @@ -50,5 +50,7 @@ int main(void) (void) foo(1, 2); (void) (1, foo(1, 2)); + unsigned x = (unsigned)2; + return 42; }