From 7d5cdb233aeaa0d0e6fe292e1ec38865c1df2129 Mon Sep 17 00:00:00 2001 From: wixoaGit Date: Fri, 15 Nov 2024 23:04:42 -0500 Subject: [PATCH 1/3] Add `opendream_noconstfold`, add it to some world vars --- DMCompiler/Compiler/DM/DMParser.cs | 1 + DMCompiler/DM/Builders/DMExpressionBuilder.cs | 2 +- DMCompiler/DM/DMValueType.cs | 1 + DMCompiler/DM/DMVariable.cs | 3 +++ DMCompiler/DM/Expressions/Dereference.cs | 10 ++-------- DMCompiler/DM/Expressions/LValue.cs | 4 ++-- 6 files changed, 10 insertions(+), 11 deletions(-) diff --git a/DMCompiler/Compiler/DM/DMParser.cs b/DMCompiler/Compiler/DM/DMParser.cs index d48d13a707..d3eba47deb 100644 --- a/DMCompiler/Compiler/DM/DMParser.cs +++ b/DMCompiler/Compiler/DM/DMParser.cs @@ -2801,6 +2801,7 @@ private DMValueType SingleAsType(out DreamPath? path, bool allowPath = false) { case "path": return DMValueType.Path; case "opendream_unimplemented": return DMValueType.Unimplemented; case "opendream_compiletimereadonly": return DMValueType.CompiletimeReadonly; + case "opendream_noconstfold": return DMValueType.NoConstFold; default: Emit(WarningCode.BadToken, typeToken.Location, $"Invalid value type '{typeToken.Text}'"); return 0; diff --git a/DMCompiler/DM/Builders/DMExpressionBuilder.cs b/DMCompiler/DM/Builders/DMExpressionBuilder.cs index 1209be9f80..369fe28f43 100644 --- a/DMCompiler/DM/Builders/DMExpressionBuilder.cs +++ b/DMCompiler/DM/Builders/DMExpressionBuilder.cs @@ -952,7 +952,7 @@ private DMExpression BuildDereference(DMASTDereference deref, DreamPath? inferre } if (property == null && fromObject.GetGlobalVariableId(field) is { } globalId) { - property = ObjectTree.Globals [globalId]; + property = ObjectTree.Globals[globalId]; expr = new GlobalField(expr.Location, property.Type, globalId, property.ValType); diff --git a/DMCompiler/DM/DMValueType.cs b/DMCompiler/DM/DMValueType.cs index 09c1b0b157..61f23652aa 100644 --- a/DMCompiler/DM/DMValueType.cs +++ b/DMCompiler/DM/DMValueType.cs @@ -28,6 +28,7 @@ public enum DMValueType { //Byond here be dragons Unimplemented = 0x4000, // Marks that a method or property is not implemented. Throws a compiler warning if accessed. CompiletimeReadonly = 0x8000, // Marks that a property can only ever be read from, never written to. This is a const-ier version of const, for certain standard values like list.type + NoConstFold = 0x10000 // Marks that a const var cannot be const-folded during compile } /// diff --git a/DMCompiler/DM/DMVariable.cs b/DMCompiler/DM/DMVariable.cs index ece888fe82..374b71a5d1 100644 --- a/DMCompiler/DM/DMVariable.cs +++ b/DMCompiler/DM/DMVariable.cs @@ -14,6 +14,9 @@ internal sealed class DMVariable { public DMExpression? Value; public DMComplexValueType ValType; + public bool CanConstFold => (IsConst || ValType.MatchesType(DMValueType.CompiletimeReadonly)) && + !ValType.MatchesType(DMValueType.NoConstFold); + public DMVariable(DreamPath? type, string name, bool isGlobal, bool isConst, bool isTmp, DMComplexValueType? valType = null) { Type = type; Name = name; diff --git a/DMCompiler/DM/Expressions/Dereference.cs b/DMCompiler/DM/Expressions/Dereference.cs index e350f13a52..06338f2468 100644 --- a/DMCompiler/DM/Expressions/Dereference.cs +++ b/DMCompiler/DM/Expressions/Dereference.cs @@ -303,14 +303,8 @@ public override bool TryAsConstant(DMCompiler compiler, [NotNullWhen(true)] out if (operation is FieldOperation fieldOperation && prevPath is not null && compiler.DMObjectTree.TryGetDMObject(prevPath.Value, out var obj)) { var variable = obj.GetVariable(fieldOperation.Identifier); - if (variable != null) { - if (variable.IsConst) - return variable.Value.TryAsConstant(compiler, out constant); - if (variable.ValType.IsCompileTimeReadOnly) { - variable.Value.TryAsConstant(compiler, out constant!); - return true; // MUST be true. - } - } + if (variable is { CanConstFold: true }) + return variable.Value.TryAsConstant(compiler, out constant); } constant = null; diff --git a/DMCompiler/DM/Expressions/LValue.cs b/DMCompiler/DM/Expressions/LValue.cs index b454833f35..a32902f4f9 100644 --- a/DMCompiler/DM/Expressions/LValue.cs +++ b/DMCompiler/DM/Expressions/LValue.cs @@ -144,7 +144,7 @@ public override DMReference EmitReference(ExpressionContext ctx, string endLabel public override string GetNameof(ExpressionContext ctx) => variable.Name; public override bool TryAsConstant(DMCompiler compiler, [NotNullWhen(true)] out Constant? constant) { - if (variable is { IsConst: true, Value: not null }) { + if (variable is { CanConstFold: true, Value: not null }) { return variable.Value.TryAsConstant(compiler, out constant); } @@ -181,7 +181,7 @@ public override string GetNameof(ExpressionContext ctx) { public override bool TryAsConstant(DMCompiler compiler, [NotNullWhen(true)] out Constant? constant) { DMVariable global = compiler.DMObjectTree.Globals[Id]; - if (global.IsConst) { + if (global.CanConstFold) { return global.Value.TryAsConstant(compiler, out constant); } From ab7188fa95632f4e810f47aa6506724dd3a6cc86 Mon Sep 17 00:00:00 2001 From: wixoaGit Date: Fri, 15 Nov 2024 23:08:34 -0500 Subject: [PATCH 2/3] Actually add it to world vars --- DMCompiler/DMStandard/Types/World.dm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DMCompiler/DMStandard/Types/World.dm b/DMCompiler/DMStandard/Types/World.dm index 39f7242d4c..609109c1d8 100644 --- a/DMCompiler/DMStandard/Types/World.dm +++ b/DMCompiler/DMStandard/Types/World.dm @@ -42,7 +42,7 @@ var/sleep_offline = 0 - var/system_type + var/const/system_type as opendream_noconstfold var/map_cpu = 0 as opendream_unimplemented var/hub as opendream_unimplemented @@ -56,7 +56,7 @@ // An OpenDream read-only var that tells you what port Topic() is listening on // Remove OPENDREAM_TOPIC_PORT_EXISTS if this is ever removed - var/const/opendream_topic_port + var/const/opendream_topic_port as opendream_noconstfold proc/New() proc/Del() From 27c90fbe711ea18056422dbabec04c3ec2a67038 Mon Sep 17 00:00:00 2001 From: wixoaGit Date: Fri, 15 Nov 2024 23:16:30 -0500 Subject: [PATCH 3/3] oops --- DMCompiler/DM/DMVariable.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DMCompiler/DM/DMVariable.cs b/DMCompiler/DM/DMVariable.cs index 374b71a5d1..2ff3b649e8 100644 --- a/DMCompiler/DM/DMVariable.cs +++ b/DMCompiler/DM/DMVariable.cs @@ -14,8 +14,8 @@ internal sealed class DMVariable { public DMExpression? Value; public DMComplexValueType ValType; - public bool CanConstFold => (IsConst || ValType.MatchesType(DMValueType.CompiletimeReadonly)) && - !ValType.MatchesType(DMValueType.NoConstFold); + public bool CanConstFold => (IsConst || ValType.Type.HasFlag(DMValueType.CompiletimeReadonly)) && + !ValType.Type.HasFlag(DMValueType.NoConstFold); public DMVariable(DreamPath? type, string name, bool isGlobal, bool isConst, bool isTmp, DMComplexValueType? valType = null) { Type = type;