Skip to content

Commit

Permalink
Add support for unsigned type alias
Browse files Browse the repository at this point in the history
Without that assert macro does not working.
  • Loading branch information
kant2002 authored and ForNeVeR committed Sep 5, 2024
1 parent c13819f commit 052389f
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Cesium.CodeGen/Extensions/TypeSystemEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Ir/Expressions/TypeCastExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions Cesium.CodeGen/Ir/Types/CTypeSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions Cesium.IntegrationTests/type_casts.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,7 @@ int main(void)
(void) foo(1, 2);
(void) (1, foo(1, 2));

unsigned x = (unsigned)2;

return 42;
}

0 comments on commit 052389f

Please sign in to comment.