Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions DMCompiler/Compiler/DM/AST/DMAST.ObjectStatements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public DMASTProcDefinition(Location location, DreamPath path, DMASTDefinitionPar
}

ObjectPath = (path.Elements.Length > 1) ? path.FromElements(0, -2) : DreamPath.Root;
Name = path.LastElement;
Name = path.LastElement ?? throw new ArgumentException($"Proc path \"{path}\" is missing a name", nameof(path));
Parameters = parameters;
Body = body;
ReturnTypes = returnType;
Expand Down Expand Up @@ -103,7 +103,7 @@ public sealed class DMASTObjectVarOverride : DMASTStatement {

public DMASTObjectVarOverride(Location location, DreamPath path, DMASTExpression value) : base(location) {
ObjectPath = path.FromElements(0, -2);
VarName = path.LastElement;
VarName = path.LastElement ?? throw new ArgumentException($"Var override path \"{path}\" is missing a name", nameof(path));
Value = value;
}
}
3 changes: 3 additions & 0 deletions DMCompiler/Compiler/DM/DMParser.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using DMCompiler.Compiler.DMPreprocessor;
using System.Linq;
using DMCompiler.Compiler.DM.AST;
Expand Down Expand Up @@ -506,6 +507,7 @@ public DMASTFile File() {
return null;
}

[return: NotNullIfNotNull(nameof(expression))]
private DMASTExpression? ParseScopeIdentifier(DMASTExpression? expression) {
do {
var identifier = Identifier();
Expand Down Expand Up @@ -1265,6 +1267,7 @@ private DMASTProcStatement For() {
if (Check(TokenType.DM_In)) {
Whitespace();
DMASTExpression? listExpr = Expression();
RequireExpression(ref listExpr);
Whitespace();
Consume(TokenType.DM_RightParenthesis, "Expected ')' in for after expression 2");
ExtraColonPeriod();
Expand Down
10 changes: 5 additions & 5 deletions DMCompiler/Compiler/DMPreprocessor/DMPreprocessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ public void DefineMacro(string key, string value) {

// NB: Pushes files to a stack, so call in reverse order if you are
// including multiple files.
public void IncludeFile(string includeDir, string file, bool isDMStandard, Location? includedFrom = null) {
string filePath = Path.Combine(includeDir, file);
public void IncludeFile(string? includeDir, string file, bool isDMStandard, Location? includedFrom = null) {
string filePath = Path.Combine(includeDir ?? string.Empty, file);
filePath = filePath.Replace('\\', Path.DirectorySeparatorChar);
filePath = Path.GetFullPath(filePath); // Strips out path operators

Expand Down Expand Up @@ -243,7 +243,7 @@ public void IncludeFile(string includeDir, string file, bool isDMStandard, Locat
}
}

public void PreprocessFile(string includeDir, string file, bool isDMStandard) {
public void PreprocessFile(string? includeDir, string file, bool isDMStandard) {
file = file.Replace('\\', '/');

_lexerStack.Push(new DMPreprocessorLexer(compiler, includeDir, file, isDMStandard));
Expand Down Expand Up @@ -275,7 +275,7 @@ private void HandleIncludeDirective(Token includeToken) {

DMPreprocessorLexer currentLexer = _lexerStack.Peek();
string file = Path.Combine(Path.GetDirectoryName(currentLexer.File.Replace('\\', Path.DirectorySeparatorChar)), includedFileToken.ValueAsString());
string directory = currentLexer.IncludeDirectory;
string? directory = currentLexer.IncludeDirectory;

IncludeFile(directory, file, includeToken.Location.InDMStandard, includedFrom: includeToken.Location);
}
Expand Down Expand Up @@ -307,7 +307,7 @@ private void HandleDefineDirective(Token defineToken) {
}

DMPreprocessorLexer currentLexer = _lexerStack.Peek();
string dir = Path.Combine(currentLexer.IncludeDirectory, dirTokenValue);
string dir = Path.Combine(currentLexer.IncludeDirectory ?? string.Empty, dirTokenValue);
compiler.AddResourceDirectory(dir, dirToken.Location);

// In BYOND it goes on to set the FILE_DIR macro's value to the added directory
Expand Down
8 changes: 4 additions & 4 deletions DMCompiler/Compiler/DMPreprocessor/DMPreprocessorLexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace DMCompiler.Compiler.DMPreprocessor;
internal sealed class DMPreprocessorLexer {
private static readonly StringBuilder TokenTextBuilder = new();

public readonly string IncludeDirectory;
public readonly string? IncludeDirectory;
public readonly string File;

private readonly DMCompiler _compiler;
Expand All @@ -23,7 +23,7 @@ internal sealed class DMPreprocessorLexer {
private int _previousLine = 1, _previousColumn;
private readonly Queue<Token> _pendingTokenQueue = new(); // TODO: Possible to remove this?

public DMPreprocessorLexer(DMCompiler compiler, string includeDirectory, string file, string source) {
public DMPreprocessorLexer(DMCompiler compiler, string? includeDirectory, string file, string source) {
_compiler = compiler;
IncludeDirectory = includeDirectory;
File = file;
Expand All @@ -32,12 +32,12 @@ public DMPreprocessorLexer(DMCompiler compiler, string includeDirectory, string
Advance();
}

public DMPreprocessorLexer(DMCompiler compiler, string includeDirectory, string file, bool isDMStandard) {
public DMPreprocessorLexer(DMCompiler compiler, string? includeDirectory, string file, bool isDMStandard) {
_compiler = compiler;
IncludeDirectory = includeDirectory;
File = file;

_source = new StreamReader(Path.Combine(includeDirectory, file), Encoding.UTF8);
_source = new StreamReader(Path.Combine(includeDirectory ?? string.Empty, file), Encoding.UTF8);
_isDMStandard = isDMStandard;
Advance();
}
Expand Down
4 changes: 3 additions & 1 deletion DMCompiler/DM/Builders/DMASTFolder.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using DMCompiler.Compiler.DM.AST;

namespace DMCompiler.DM.Builders;
Expand Down Expand Up @@ -122,7 +123,8 @@ public void FoldAst(DMASTNode? ast) {
}
}

private DMASTExpression FoldExpression(DMASTExpression? expression) {
[return: NotNullIfNotNull(nameof(expression))]
private DMASTExpression? FoldExpression(DMASTExpression? expression) {
if (expression is DMASTUnary unary) {
unary.Value = FoldExpression(unary.Value);
} else if (expression is DMASTBinary binary) {
Expand Down
13 changes: 7 additions & 6 deletions DMCompiler/DM/Builders/DMProcBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

if (parameter.Value != null) { //Parameter has a default value
string afterDefaultValueCheck = proc.NewLabelName();
DMReference parameterRef = proc.GetLocalVariableReference(parameterName);
DMReference parameterRef = proc.GetLocalVariableReference(parameterName, parameter.Location);

//Don't set parameter to default if not null
proc.PushReferenceValue(parameterRef);
Expand Down Expand Up @@ -197,7 +197,7 @@
}

value.EmitPushValue(ExprContext);
proc.Assign(proc.GetLocalVariableReference(varDeclaration.Name));
proc.Assign(proc.GetLocalVariableReference(varDeclaration.Name, varDeclaration.Location));
proc.Pop();
}

Expand Down Expand Up @@ -275,10 +275,10 @@
if (statementFor.Expression1 is DMASTVarDeclExpression decl) {
outputExpr = new DMASTIdentifier(decl.Location, decl.DeclPath.Path.LastElement!);
} else {
outputExpr = statementFor.Expression1;

Check warning on line 278 in DMCompiler/DM/Builders/DMProcBuilder.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Converting null literal or possible null value to non-nullable type.
}

var keyVar = _exprBuilder.Create(outputExpr);

Check warning on line 281 in DMCompiler/DM/Builders/DMProcBuilder.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Possible null reference argument for parameter 'expression' in 'DMExpression DMExpressionBuilder.Create(DMASTExpression expression, DreamPath? inferredPath = null)'.
valueVar = _exprBuilder.Create(dmastIn.LHS);

switch (keyVar) {
Expand Down Expand Up @@ -318,7 +318,7 @@
switch (statementFor.Expression1) {
case DMASTAssign {LHS: DMASTVarDeclExpression decl, RHS: DMASTExpressionInRange range}: {
var initializer = statementFor.Expression1 != null ? _exprBuilder.Create(statementFor.Expression1) : null;
var identifier = new DMASTIdentifier(decl.Location, decl.DeclPath.Path.LastElement);

Check warning on line 321 in DMCompiler/DM/Builders/DMProcBuilder.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Possible null reference argument for parameter 'identifier' in 'DMASTIdentifier.DMASTIdentifier(Location location, string identifier)'.
var outputVar = _exprBuilder.Create(identifier);

var start = _exprBuilder.Create(range.StartRange);
Expand All @@ -338,7 +338,7 @@

DMASTExpression outputExpr;
if (decl != null) {
outputExpr = new DMASTIdentifier(exprRange.Value.Location, decl.DeclPath.Path.LastElement);

Check warning on line 341 in DMCompiler/DM/Builders/DMProcBuilder.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Possible null reference argument for parameter 'identifier' in 'DMASTIdentifier.DMASTIdentifier(Location location, string identifier)'.
} else {
outputExpr = exprRange.Value;
}
Expand Down Expand Up @@ -396,10 +396,11 @@
}
proc.EndScope();

IEnumerable<DMASTVarDeclExpression> FindVarDecls(DMASTExpression expr) {
if (expr is DMASTVarDeclExpression p) {
IEnumerable<DMASTVarDeclExpression> FindVarDecls(DMASTExpression? expr) {
if (expr is null)
yield break;
if (expr is DMASTVarDeclExpression p)
yield return p;
}

foreach (var leaf in expr.Leaves()) {
foreach(var decl in FindVarDecls(leaf)) {
Expand Down Expand Up @@ -863,7 +864,7 @@
compiler.Emit(WarningCode.DuplicateVariable, param.Location, $"Duplicate var {param.Name}");
}

proc.StartTry(catchLabel, proc.GetLocalVariableReference(param.Name));
proc.StartTry(catchLabel, proc.GetLocalVariableReference(param.Name, param.Location));
} else {
if (tryCatch.CatchParameter != null)
compiler.Emit(WarningCode.InvalidVarDefinition, tryCatch.CatchParameter.Location,
Expand Down
7 changes: 6 additions & 1 deletion DMCompiler/DM/DMProc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,14 @@ public bool TryAddLocalConstVariable(string name, DreamPath? type, Constant valu
return null;
}

public DMReference GetLocalVariableReference(string name) {
public DMReference GetLocalVariableReference(string name, Location loc) {
LocalVariable? local = GetLocalVariable(name);

if (local is null) {
_compiler.Emit(WarningCode.InvalidReference, loc, $"Attempted to reference invalid local var \"{name}\"");
return DMReference.Invalid;
}

return local.IsParameter ? DMReference.CreateArgument(local.Id) : DMReference.CreateLocal(local.Id);
}

Expand Down
9 changes: 5 additions & 4 deletions DMCompiler/DMCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,12 @@ public void AddResourceDirectory(string dir, Location loc) {
return null;
}

string includeDir = Path.GetDirectoryName(files[i]);
string? includeDir = Path.GetDirectoryName(files[i]);
string fileName = Path.GetFileName(files[i]);

preproc.IncludeFile(includeDir, fileName, false);
compiler.AddResourceDirectory(includeDir, Location.Internal);
if (includeDir is not null)
compiler.AddResourceDirectory(includeDir, Location.Internal);
}

// Adds the root of the DM project to FILE_DIR
Expand Down Expand Up @@ -160,8 +161,8 @@ public void AddResourceDirectory(string dir, Location loc) {
result.Append(t.Text);
}

string outputDir = Path.GetDirectoryName(Settings.Files[0]);
string outputPath = Path.Combine(outputDir, "preprocessor_dump.dm");
string? outputDir = Path.GetDirectoryName(Settings.Files[0]);
string outputPath = Path.Combine(outputDir ?? string.Empty, "preprocessor_dump.dm");

File.WriteAllText(outputPath, result.ToString());
Console.WriteLine($"Preprocessor output dumped to {outputPath}");
Expand Down
Loading