-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for
final
on object vars (#2159)
- Loading branch information
Showing
7 changed files
with
157 additions
and
128 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
Content.Tests/DMProject/Tests/Statements/VarDecl/cant_override_final.dm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// COMPILE ERROR OD0407 | ||
|
||
/datum | ||
var/final/foo = 1 | ||
|
||
/datum/a | ||
foo = 2 // Can't override a final var |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,146 +1,157 @@ | ||
namespace DMCompiler.Compiler.DM { | ||
public abstract class VarDeclInfo { | ||
public DreamPath? TypePath; | ||
public string VarName; | ||
namespace DMCompiler.Compiler.DM; | ||
|
||
///<summary>Marks whether the variable is /global/ or /static/. (These are seemingly interchangeable keywords in DM and so are under this same boolean)</summary> | ||
public bool IsStatic; | ||
internal abstract class VarDeclInfo { | ||
public DreamPath? TypePath; | ||
public string VarName; | ||
|
||
public bool IsConst; | ||
public bool IsList; | ||
} | ||
///<summary>Marks whether the variable is /global/ or /static/. (These are seemingly interchangeable keywords in DM and so are under this same boolean)</summary> | ||
public bool IsStatic; | ||
|
||
public sealed class ProcVarDeclInfo : VarDeclInfo | ||
{ | ||
public ProcVarDeclInfo(DreamPath path) | ||
{ | ||
string[] elements = path.Elements; | ||
var readIdx = 0; | ||
List<string> currentPath = new(); | ||
if (elements[readIdx] == "var") | ||
{ | ||
readIdx++; | ||
} | ||
while (readIdx < elements.Length - 1) | ||
{ | ||
var elem = elements[readIdx]; | ||
if (elem == "static" || elem == "global") | ||
{ | ||
public bool IsConst; | ||
public bool IsFinal; | ||
public bool IsList; | ||
} | ||
|
||
internal sealed class ProcVarDeclInfo : VarDeclInfo { | ||
public ProcVarDeclInfo(DreamPath path) { | ||
string[] elements = path.Elements; | ||
var readIdx = 0; | ||
List<string> currentPath = new(); | ||
if (elements[readIdx] == "var") { | ||
readIdx++; | ||
} | ||
|
||
while (readIdx < elements.Length - 1) { | ||
var elem = elements[readIdx]; | ||
switch (elem) { | ||
case "static": | ||
case "global": | ||
IsStatic = true; | ||
} | ||
else if (elem == "const") | ||
{ | ||
break; | ||
case "const": | ||
IsConst = true; | ||
} | ||
else if (elem == "list") | ||
{ | ||
break; | ||
case "final": | ||
IsFinal = true; | ||
break; | ||
case "list": | ||
IsList = true; | ||
} | ||
else | ||
{ | ||
break; | ||
default: | ||
currentPath.Add(elem); | ||
} | ||
readIdx += 1; | ||
} | ||
if (currentPath.Count > 0) | ||
{ | ||
TypePath = new DreamPath(DreamPath.PathType.Absolute, currentPath.ToArray()); | ||
} | ||
else | ||
{ | ||
TypePath = null; | ||
break; | ||
} | ||
VarName = elements[elements.Length - 1]; | ||
|
||
readIdx += 1; | ||
} | ||
|
||
if (currentPath.Count > 0) { | ||
TypePath = new DreamPath(DreamPath.PathType.Absolute, currentPath.ToArray()); | ||
} else { | ||
TypePath = null; | ||
} | ||
|
||
VarName = elements[^1]; | ||
} | ||
} | ||
|
||
public sealed class ObjVarDeclInfo : VarDeclInfo | ||
{ | ||
public DreamPath ObjectPath; | ||
public bool IsTmp; | ||
|
||
public ObjVarDeclInfo(DreamPath path) | ||
{ | ||
string[] elements = path.Elements; | ||
var readIdx = 0; | ||
List<string> currentPath = new(); | ||
while (readIdx < elements.Length && elements[readIdx] != "var") | ||
{ | ||
currentPath.Add(elements[readIdx]); | ||
readIdx += 1; | ||
} | ||
ObjectPath = new DreamPath(path.Type, currentPath.ToArray()); | ||
if (ObjectPath.Elements.Length == 0) // Variables declared in the root scope are inherently static. | ||
{ | ||
IsStatic = true; | ||
} | ||
currentPath.Clear(); | ||
internal sealed class ObjVarDeclInfo : VarDeclInfo { | ||
public DreamPath ObjectPath; | ||
public readonly bool IsTmp; | ||
|
||
public ObjVarDeclInfo(DreamPath path) { | ||
string[] elements = path.Elements; | ||
var readIdx = 0; | ||
List<string> currentPath = new(); | ||
while (readIdx < elements.Length && elements[readIdx] != "var") { | ||
currentPath.Add(elements[readIdx]); | ||
readIdx += 1; | ||
while (readIdx < elements.Length - 1) | ||
{ | ||
var elem = elements[readIdx]; | ||
if (elem == "static" || elem == "global") | ||
{ | ||
} | ||
|
||
ObjectPath = new DreamPath(path.Type, currentPath.ToArray()); | ||
if (ObjectPath.Elements.Length == 0) { // Variables declared in the root scope are inherently static. | ||
IsStatic = true; | ||
} | ||
|
||
currentPath.Clear(); | ||
readIdx += 1; | ||
while (readIdx < elements.Length - 1) { | ||
var elem = elements[readIdx]; | ||
switch (elem) { | ||
case "static": | ||
case "global": | ||
IsStatic = true; | ||
} | ||
else if (elem == "const") | ||
{ | ||
break; | ||
case "const": | ||
IsConst = true; | ||
} | ||
else if (elem == "list") | ||
{ | ||
break; | ||
case "final": | ||
IsFinal = true; | ||
break; | ||
case "list": | ||
IsList = true; | ||
} | ||
else if (elem == "tmp") | ||
{ | ||
break; | ||
case "tmp": | ||
IsTmp = true; | ||
} | ||
else | ||
{ | ||
break; | ||
default: | ||
currentPath.Add(elem); | ||
} | ||
readIdx += 1; | ||
} | ||
if (currentPath.Count > 0) | ||
{ | ||
TypePath = new DreamPath(DreamPath.PathType.Absolute, currentPath.ToArray()); | ||
break; | ||
} | ||
else | ||
{ | ||
TypePath = null; | ||
} | ||
VarName = elements[elements.Length - 1]; | ||
|
||
readIdx += 1; | ||
} | ||
|
||
if (currentPath.Count > 0) { | ||
TypePath = new DreamPath(DreamPath.PathType.Absolute, currentPath.ToArray()); | ||
} else { | ||
TypePath = null; | ||
} | ||
|
||
VarName = elements[^1]; | ||
} | ||
} | ||
|
||
public sealed class ProcParameterDeclInfo : VarDeclInfo { | ||
public ProcParameterDeclInfo(DreamPath path) { | ||
string[] elements = path.Elements; | ||
var readIdx = 0; | ||
List<string> currentPath = new(); | ||
if (elements[readIdx] == "var") { | ||
readIdx++; | ||
} | ||
while (readIdx < elements.Length - 1) { | ||
var elem = elements[readIdx]; | ||
if (elem == "static" || elem == "global") { | ||
internal sealed class ProcParameterDeclInfo : VarDeclInfo { | ||
public ProcParameterDeclInfo(DreamPath path) { | ||
string[] elements = path.Elements; | ||
var readIdx = 0; | ||
List<string> currentPath = new(); | ||
if (elements[readIdx] == "var") { | ||
readIdx++; | ||
} | ||
|
||
while (readIdx < elements.Length - 1) { | ||
var elem = elements[readIdx]; | ||
switch (elem) { | ||
case "static": | ||
case "global": | ||
//No effect | ||
} else if (elem == "const") { | ||
break; | ||
case "const": | ||
//TODO: Parameters can be constant | ||
//If they are they can't be assigned to but still cannot be used in const-only contexts (such as switch cases) | ||
} else if (elem == "list") { | ||
break; | ||
case "final": | ||
IsFinal = true; | ||
break; | ||
case "list": | ||
IsList = true; | ||
} else { | ||
break; | ||
default: | ||
currentPath.Add(elem); | ||
} | ||
readIdx += 1; | ||
break; | ||
} | ||
if (currentPath.Count > 0) { | ||
TypePath = new DreamPath(DreamPath.PathType.Absolute, currentPath.ToArray()); | ||
} else { | ||
TypePath = null; | ||
} | ||
VarName = elements[elements.Length - 1]; | ||
|
||
readIdx += 1; | ||
} | ||
|
||
if (currentPath.Count > 0) { | ||
TypePath = new DreamPath(DreamPath.PathType.Absolute, currentPath.ToArray()); | ||
} else { | ||
TypePath = null; | ||
} | ||
|
||
VarName = elements[^1]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters