From 12cc7515972ab9a8b52745fd34d9ac8f8c70b1f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=86=E3=82=8C=E3=81=84=E3=81=97?= <57707826+ureishi@users.noreply.github.com> Date: Fri, 15 Mar 2024 18:29:41 +0900 Subject: [PATCH 1/2] add `sizeof` expression --- .../Compiler/Binder/BinderSyntaxVisitor.cs | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/Packages/com.vrchat.UdonSharp/Editor/Compiler/Binder/BinderSyntaxVisitor.cs b/Packages/com.vrchat.UdonSharp/Editor/Compiler/Binder/BinderSyntaxVisitor.cs index c7204d1c..13e498a1 100644 --- a/Packages/com.vrchat.UdonSharp/Editor/Compiler/Binder/BinderSyntaxVisitor.cs +++ b/Packages/com.vrchat.UdonSharp/Editor/Compiler/Binder/BinderSyntaxVisitor.cs @@ -346,6 +346,47 @@ public override BoundNode VisitTypeOfExpression(TypeOfExpressionSyntax node) return new BoundConstantExpression(type.UdonType.SystemType, Context.GetTypeSymbol(typeof(Type))); } + + public override BoundNode VisitSizeOfExpression(SizeOfExpressionSyntax node) + { + TypeSymbol type = GetTypeSymbol(node.Type); + + if (type.IsEnum) + type = Context.GetTypeSymbol(((INamedTypeSymbol)type.RoslynSymbol).EnumUnderlyingType); + + int size; + + if (type == Context.GetTypeSymbol(SpecialType.System_SByte)) + size = sizeof(sbyte); + else if (type == Context.GetTypeSymbol(SpecialType.System_Byte)) + size = sizeof(byte); + else if (type == Context.GetTypeSymbol(SpecialType.System_Int16)) + size = sizeof(short); + else if (type == Context.GetTypeSymbol(SpecialType.System_UInt16)) + size = sizeof(ushort); + else if (type == Context.GetTypeSymbol(SpecialType.System_Int32)) + size = sizeof(int); + else if (type == Context.GetTypeSymbol(SpecialType.System_UInt32)) + size = sizeof(uint); + else if (type == Context.GetTypeSymbol(SpecialType.System_Int64)) + size = sizeof(long); + else if (type == Context.GetTypeSymbol(SpecialType.System_UInt64)) + size = sizeof(ulong); + else if (type == Context.GetTypeSymbol(SpecialType.System_Char)) + size = sizeof(char); + else if (type == Context.GetTypeSymbol(SpecialType.System_Single)) + size = sizeof(float); + else if (type == Context.GetTypeSymbol(SpecialType.System_Double)) + size = sizeof(double); + else if (type == Context.GetTypeSymbol(SpecialType.System_Decimal)) + size = sizeof(decimal); + else if (type == Context.GetTypeSymbol(SpecialType.System_Boolean)) + size = sizeof(bool); + else + throw new NotSupportedException("Cannot use sizeof on types other than primitive or enumeration types", node.GetLocation()); + + return new BoundConstantExpression(size, Context.GetTypeSymbol(SpecialType.System_Int32)); + } private BoundExpression HandleNameOfExpression(InvocationExpressionSyntax node) { From cc989cecf86c0e1ea0e4a8f2664dc2393ac9ff3c Mon Sep 17 00:00:00 2001 From: ureishi Date: Sun, 24 Mar 2024 19:35:57 +0900 Subject: [PATCH 2/2] Update BinderSyntaxVisitor.cs https://github.com/vrchat-community/UdonSharp/pull/158#issuecomment-2016733904 --- .../Editor/Compiler/Binder/BinderSyntaxVisitor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Packages/com.vrchat.UdonSharp/Editor/Compiler/Binder/BinderSyntaxVisitor.cs b/Packages/com.vrchat.UdonSharp/Editor/Compiler/Binder/BinderSyntaxVisitor.cs index 13e498a1..50acf3bb 100644 --- a/Packages/com.vrchat.UdonSharp/Editor/Compiler/Binder/BinderSyntaxVisitor.cs +++ b/Packages/com.vrchat.UdonSharp/Editor/Compiler/Binder/BinderSyntaxVisitor.cs @@ -383,7 +383,7 @@ public override BoundNode VisitSizeOfExpression(SizeOfExpressionSyntax node) else if (type == Context.GetTypeSymbol(SpecialType.System_Boolean)) size = sizeof(bool); else - throw new NotSupportedException("Cannot use sizeof on types other than primitive or enumeration types", node.GetLocation()); + throw new NotSupportedException($"'{type.Name}' does not have a predefined size.", node.GetLocation()); return new BoundConstantExpression(size, Context.GetTypeSymbol(SpecialType.System_Int32)); }