Skip to content

Commit

Permalink
optimize binary op overload infer
Browse files Browse the repository at this point in the history
  • Loading branch information
CppCXY committed May 20, 2024
1 parent dcc55d7 commit 9d4e74b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 31 deletions.
9 changes: 2 additions & 7 deletions EmmyLua/CodeAnalysis/Compilation/Index/Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void Remove(LuaDocumentId documentId)
{
return _storage.TryGetValue(key, out var entry)
? entry.Files.Values.SelectMany(it => it)
: Enumerable.Empty<(TSyntaxElement, TElement)>();
: [];
}
}

Expand Down Expand Up @@ -187,11 +187,6 @@ private void RemoveOperator(LuaDocumentId documentId)

public IEnumerable<TypeOperator> GetTypeOperators(string typeName)
{
if (TypeOperators.TryGetValue(typeName, out var entry))
{
return entry.Files.Values.SelectMany(it => it);
}

return Enumerable.Empty<TypeOperator>();
return TypeOperators.TryGetValue(typeName, out var entry) ? entry.Files.Values.SelectMany(it => it) : [];
}
}
13 changes: 12 additions & 1 deletion EmmyLua/CodeAnalysis/Compilation/Infer/ExpressionInfer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,18 @@ private static LuaType GuessBinaryMathType(LuaBinaryExprSyntax binaryExpr, Opera
{
return bop.Ret;
}
return leftTy;
var bop2 = context.GetBestMatchedBinaryOperator(opKind, rightTy, leftTy);
if (bop2 is not null)
{
return bop2.Ret;
}

if (leftTy.Equals(Builtin.Integer) && rightTy.Equals(Builtin.Integer))
{
return Builtin.Integer;
}

return Builtin.Number;
}

private static LuaType InferClosureExpr(LuaClosureExprSyntax closureExpr, SearchContext context)
Expand Down
24 changes: 1 addition & 23 deletions EmmyLua/CodeAnalysis/Document/LuaDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,11 @@

namespace EmmyLua.CodeAnalysis.Document;

public readonly struct LuaDocumentId(int id)
public readonly record struct LuaDocumentId(int Id)
{
public static LuaDocumentId VirtualDocumentId { get; } = new(0);

public int Id { get; } = id;

public bool IsVirtual => Id == 0;

public override int GetHashCode()
{
return Id.GetHashCode();
}

public static bool operator ==(LuaDocumentId left, LuaDocumentId right)
{
return left.Id == right.Id;
}

public static bool operator !=(LuaDocumentId left, LuaDocumentId right)
{
return !(left == right);
}

public override string ToString()
{
return Id.ToString();
}
}

public sealed class LuaDocument
Expand Down

0 comments on commit 9d4e74b

Please sign in to comment.