Bläddra i källkod

Added Compiler.Identifier.GetSourceName

Brian Fiete 1 år sedan
förälder
incheckning
79a67c2d0c

+ 26 - 0
BeefLibs/corlib/src/Compiler.bf

@@ -8,6 +8,32 @@ namespace System
 {
 {
 	static class Compiler
 	static class Compiler
 	{
 	{
+		public class Identifier
+		{
+			public static HashSet<String> sReservedNameSet = new .() ~ delete _;
+
+			const String[?] cReservedNames = .("abstract", "alignof", "alloctype", "append", "as", "asm", "base", "break",
+				"case", "catch", "checked", "class", "comptype", "const", "continue", "decltype", "default", "defer", "delegate",
+				"delete", "do", "else", "enum", "explicit", "extension", "extern", "false", "finally", "fixed", "for", "function",
+				"if", "implicit", "in", "interface", "internal", "is", "isconst", "mixin", "namespace", "new", "null", "nullable",
+				"offsetof", "operator", "out", "override", "params", "private", "protected", "public", "readonly", "ref",
+				"rettype", "return", "scope", "sealed", "sizeof", "static", "strideof", "struct", "switch", "this", "true", "try",
+				"typealias", "typeof", "unchecked", "using", "var", "virtual", "volatile", "where", "while");
+
+			public static this()
+			{
+				for (var name in cReservedNames)
+					sReservedNameSet.Add(name);
+			}
+
+			public static void GetSourceName(StringView name, String outStr)
+			{
+				if (sReservedNameSet.ContainsAlt(name))
+					outStr.Append('@');
+				outStr.Append(name);
+			}
+		}
+
 		public abstract class Generator
 		public abstract class Generator
 		{
 		{
 			public enum Flags
 			public enum Flags

+ 6 - 2
BeefLibs/corlib/src/Enum.bf

@@ -609,7 +609,7 @@ namespace System
 							entryTableCode.Append(", \"");
 							entryTableCode.Append(", \"");
 							entryTableCode.Append(fieldName);
 							entryTableCode.Append(fieldName);
 							entryTableCode.Append("\", .");
 							entryTableCode.Append("\", .");
-							entryTableCode.Append(fieldInfo.Name);
+							fieldInfo.GetSourceName(entryTableCode);
 							entryTableCode.Append(", ");
 							entryTableCode.Append(", ");
 							entryTableCode.Append((nextCases[caseIdx] == -1) ? "false" : "true");
 							entryTableCode.Append((nextCases[caseIdx] == -1) ? "false" : "true");
 							entryTableCode.Append(")");
 							entryTableCode.Append(")");
@@ -622,7 +622,11 @@ namespace System
 							if (caseHasPayload)
 							if (caseHasPayload)
 								code.AppendF($"\t\t\t\tmatchIdx = {caseIdx};\n");
 								code.AppendF($"\t\t\t\tmatchIdx = {caseIdx};\n");
 							else
 							else
-								code.AppendF($"\t\t\t\treturn .Ok(.{fieldInfo.Name});\n");
+							{
+								code.Append("\t\t\t\treturn .Ok(.");
+								fieldInfo.GetSourceName(code);
+								code.Append(");\n");
+							}
 						}
 						}
 						else
 						else
 						{
 						{

+ 5 - 0
BeefLibs/corlib/src/Reflection/FieldInfo.bf

@@ -38,6 +38,11 @@ namespace System.Reflection
 			mFieldData.mCustomAttributesIdx :
 			mFieldData.mCustomAttributesIdx :
 			-1;
 			-1;
 
 
+		public void GetSourceName(String outStr)
+		{
+			Compiler.Identifier.GetSourceName(Name, outStr);
+		}
+
 	    public Result<void, Error> SetValue(Object obj, Object value)
 	    public Result<void, Error> SetValue(Object obj, Object value)
 	    {    
 	    {    
 	        void* dataAddr = ((uint8*)Internal.UnsafeCastToPtr(obj));
 	        void* dataAddr = ((uint8*)Internal.UnsafeCastToPtr(obj));