Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add opendream_noconstfold, add it to some world vars #2097

Merged
merged 3 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DMCompiler/Compiler/DM/DMParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion DMCompiler/DM/Builders/DMExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions DMCompiler/DM/DMValueType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions DMCompiler/DM/DMVariable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 2 additions & 8 deletions DMCompiler/DM/Expressions/Dereference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,14 +303,8 @@

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);
Dismissed Show dismissed Hide dismissed
}

constant = null;
Expand Down
4 changes: 2 additions & 2 deletions DMCompiler/DM/Expressions/LValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions DMCompiler/DMStandard/Types/World.dm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
Loading