Skip to content

Commit

Permalink
keep global unique
Browse files Browse the repository at this point in the history
  • Loading branch information
CppCXY committed Jun 18, 2024
1 parent e314bea commit e781f35
Show file tree
Hide file tree
Showing 19 changed files with 148 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ private void AddFuncParamCompletion(LuaCallArgListSyntax callArgList, CompleteCo
private void AddAliasParamCompletion(LuaNamedType namedType, CompleteContext context)
{
var originType = context.SemanticModel.Compilation.Db
.QueryAliasOriginTypes(namedType.Name)
.FirstOrDefault();
.QueryAliasOriginTypes(namedType.Name);
if (originType is LuaAggregateType aggregateType)
{
AddAggregateTypeCompletion(aggregateType, context);
Expand Down
4 changes: 1 addition & 3 deletions EmmyLua/CodeAnalysis/Common/IQueryableIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ public interface IQueryableIndex

IEnumerable<IDeclaration> QueryMembers(LuaType type);

IEnumerable<IDeclaration> QueryGlobals(string name);
IDeclaration? QueryGlobals(string name);

IEnumerable<LuaType> QuerySupers(string name);

IEnumerable<string> QuerySubTypes(string name);

IEnumerable<IDeclaration> QueryNamedTypeDefinitions(string name);

IEnumerable<LuaType> QueryAliasOriginTypes(string name);

IEnumerable<IDeclaration> QueryGenericParams(string name);

NamedTypeKind QueryNamedTypeKind(string name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ private void AnalyzeAssignStatDeclaration(LuaAssignStatSyntax luaAssignStat)
);
AddReference(ReferenceKind.Definition, declaration, nameExpr);
AnalyzeDeclarationDoc(declaration, luaAssignStat);
WorkspaceIndex.AddGlobal(DocumentId, name.RepresentText, declaration);

var unResolveDeclaration =
new UnResolvedDeclaration(declaration, relatedExpr, ResolveState.UnResolvedType);
AddUnResolved(unResolveDeclaration);
Expand All @@ -624,6 +624,8 @@ private void AnalyzeAssignStatDeclaration(LuaAssignStatSyntax luaAssignStat)
}
}

WorkspaceIndex.AddGlobal(DocumentId, unResolveDeclaration.IsTypeDeclaration,
name.RepresentText, declaration);
AddDeclaration(nameExpr.Position, declaration);
}
else
Expand Down Expand Up @@ -713,7 +715,7 @@ private void AnalyzeMethodDeclaration(LuaFuncStatSyntax luaFuncStat)
DeclarationFeature.Global
);
AnalyzeDeclarationDoc(declaration, luaFuncStat);
WorkspaceIndex.AddGlobal(DocumentId, name2.RepresentText, declaration);
WorkspaceIndex.AddGlobal(DocumentId, true, name2.RepresentText, declaration);
AddDeclaration(luaFuncStat.NameExpr.Position, declaration);
var unResolved = new UnResolvedDeclaration(declaration, new LuaExprRef(closureExpr),
ResolveState.UnResolvedType);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using EmmyLua.CodeAnalysis.Compilation.Declaration;
using EmmyLua.CodeAnalysis.Compilation.Infer;
using EmmyLua.CodeAnalysis.Compilation.Search;
using EmmyLua.CodeAnalysis.Compilation.Type;
using EmmyLua.CodeAnalysis.Syntax.Node.SyntaxNodes;
Expand Down Expand Up @@ -98,24 +97,20 @@ private void ResolveType(UnResolved unResolved)
for (var i = 0; i < unResolvedForRangeParameter.ParameterLuaDeclarations.Count; i++)
{
var parameter = unResolvedForRangeParameter.ParameterLuaDeclarations[i];
if (parameter.Info.DeclarationType is null)
if (parameter.Info.DeclarationType is LuaVariableRefType luaVariableRefType)
{
parameter.Info = parameter.Info with
{
DeclarationType = multiReturnType.GetElementType(i)
};
Context.Compilation.Db.WorkspaceIndex.AddIdRelatedType(luaVariableRefType.Id,
multiReturnType.GetElementType(i));
}
}
}
else if (unResolvedForRangeParameter.ParameterLuaDeclarations.FirstOrDefault() is
{ } firstDeclaration)
{
if (firstDeclaration.Info.DeclarationType is null)
if (firstDeclaration.Info.DeclarationType is LuaVariableRefType luaVariableRefType)
{
firstDeclaration.Info = firstDeclaration.Info with
{
DeclarationType = returnType
};
Context.Compilation.Db.WorkspaceIndex.AddIdRelatedType(luaVariableRefType.Id,
returnType);
}
}
}
Expand Down
47 changes: 47 additions & 0 deletions EmmyLua/CodeAnalysis/Compilation/Index/UniqueIndexStorage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using EmmyLua.CodeAnalysis.Document;

namespace EmmyLua.CodeAnalysis.Compilation.Index;

public class UniqueIndexStorage<TKey, TStubElement> where TKey : notnull
{
record struct ElementIndex(LuaDocumentId DocumentId, TStubElement Element);

private readonly Dictionary<TKey, ElementIndex> _indexMap = new();

public void Update(LuaDocumentId documentId, TKey key, TStubElement element)
{
_indexMap[key] = new ElementIndex(documentId, element);
}

public void Remove(LuaDocumentId documentId)
{
var waitRemove = new List<TKey>();
foreach (var (key, element) in _indexMap)
{
if (element.DocumentId == documentId)
{
waitRemove.Add(key);
}
}

foreach (var key in waitRemove)
{
_indexMap.Remove(key);
}
}

public TStubElement? Query(TKey key)
{
if (_indexMap.TryGetValue(key, out var element))
{
return element.Element;
}

return default;
}

public IEnumerable<TStubElement> QueryAll() =>
_indexMap.Values.Select(it => it.Element);

public bool ContainsKey(TKey key) => _indexMap.ContainsKey(key);
}
57 changes: 40 additions & 17 deletions EmmyLua/CodeAnalysis/Compilation/Index/WorkspaceIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ public class WorkspaceIndex : IQueryableIndex
{
public IndexStorage<string, LuaDeclaration> TypeMembers { get; } = new();

public IndexStorage<SyntaxElementId, LuaDeclaration> DocumentVariableMembers { get; } = new();
private IndexStorage<SyntaxElementId, LuaDeclaration> DocumentVariableMembers { get; } = new();

public IndexStorage<string, LuaDeclaration> GlobalVariableMembers { get; } = new();
private IndexStorage<string, LuaDeclaration> GlobalVariableMembers { get; } = new();

public InFiledDictionary<SyntaxElementId, string> ParentType { get; } = new();
public UniqueIndexStorage<SyntaxElementId, string> ParentType { get; } = new();

private IndexStorage<string, LuaDeclaration> Globals { get; } = new();
private UniqueIndexStorage<string, LuaDeclaration> Globals { get; } = new();

private IndexStorage<string, LuaType> Supers { get; } = new();

private IndexStorage<string, string> SubTypes { get; } = new();

private IndexStorage<string, LuaDeclaration> NamedTypeDefinition { get; } = new();

public InFiledDictionary<SyntaxElementId, LuaType> IdRelatedType { get; } = new();
public UniqueIndexStorage<SyntaxElementId, LuaType> IdRelatedType { get; } = new();

public IndexStorage<string, LuaType> GlobalRelationTypes { get; } = new();
public UniqueIndexStorage<string, LuaType> GlobalRelationTypes { get; } = new();

private IndexStorage<string, LuaType> AliasTypes { get; } = new();
private UniqueIndexStorage<string, LuaType> AliasTypes { get; } = new();

private IndexStorage<string, LuaDeclaration> GenericParams { get; } = new();

Expand Down Expand Up @@ -93,7 +93,7 @@ public void AddMember(LuaDocumentId documentId, LuaType type, LuaDeclaration lua
var name = namedType.Name;
if (name == "global")
{
AddGlobal(documentId, luaDeclaration.Name, luaDeclaration);
AddGlobal(documentId, false, luaDeclaration.Name, luaDeclaration);
return;
}

Expand Down Expand Up @@ -127,7 +127,7 @@ public void AddMember(LuaDocumentId documentId, LuaType type, LuaDeclaration lua
private void AddNamedTypeMember(LuaDocumentId documentId, string name, LuaDeclaration luaDeclaration)
{
TypeMembers.Add(documentId, name, luaDeclaration);
ParentType.Add(documentId, luaDeclaration.UniqueId, name);
ParentType.Update(documentId, luaDeclaration.UniqueId, name);
}

private void AddLocalVariableMember(SyntaxElementId id, LuaDeclaration luaDeclaration)
Expand All @@ -140,9 +140,20 @@ private void AddGlobalVariableMember(LuaDocumentId documentId, string name, LuaD
GlobalVariableMembers.Add(documentId, name, luaDeclaration);
}

public void AddGlobal(LuaDocumentId documentId, string name, LuaDeclaration luaDeclaration)
public void AddGlobal(LuaDocumentId documentId, bool forceDefine, string name, LuaDeclaration luaDeclaration)
{
Globals.Add(documentId, name, luaDeclaration);
if (forceDefine) // forceUpdate
{
Globals.Update(documentId, name, luaDeclaration);
return;
}

if (Globals.Query(name) is not null)
{
return;
}

Globals.Update(documentId, name, luaDeclaration);
}

public void AddSuper(LuaDocumentId documentId, string name, LuaType type)
Expand All @@ -168,17 +179,29 @@ public void AddAlias(LuaDocumentId documentId, string name, LuaType baseType,
LuaDeclaration declaration)
{
AddTypeDefinition(documentId, name, declaration);
AliasTypes.Add(documentId, name, baseType);
AliasTypes.Update(documentId, name, baseType);
}

public void AddIdRelatedType(SyntaxElementId id, LuaType relatedType)
{
IdRelatedType.Add(id.DocumentId, id, relatedType);
IdRelatedType.Update(id.DocumentId, id, relatedType);
}

public void AddGlobalRelationType(LuaDocumentId documentId, string name, LuaType type)
{
GlobalRelationTypes.Add(documentId, name, type);
if (type.Equals(Builtin.Unknown))
{
return;
}

if (GlobalRelationTypes.Query(name) is { } oldType)
{
GlobalRelationTypes.Update(documentId, name, oldType.Union(type));
}
else
{
GlobalRelationTypes.Update(documentId, name, type);
}
}

public void AddEnum(LuaDocumentId documentId, string name, LuaType? baseType,
Expand Down Expand Up @@ -303,7 +326,7 @@ public IEnumerable<IDeclaration> QueryMembers(LuaType type)
return [];
}

public IEnumerable<IDeclaration> QueryGlobals(string name)
public IDeclaration? QueryGlobals(string name)
{
return Globals.Query(name);
}
Expand All @@ -323,7 +346,7 @@ public IEnumerable<IDeclaration> QueryNamedTypeDefinitions(string name)
return NamedTypeDefinition.Query(name);
}

public IEnumerable<LuaType> QueryAliasOriginTypes(string name)
public LuaType? QueryAliasOriginTypes(string name)
{
return AliasTypes.Query(name);
}
Expand Down Expand Up @@ -388,6 +411,6 @@ public IEnumerable<LuaDeclaration> QueryDocumentLocalDeclarations(LuaDocumentId

public LuaType? QueryRelatedGlobalType(string name)
{
return GlobalRelationTypes.Query(name).FirstOrDefault();
return GlobalRelationTypes.Query(name);
}
}
38 changes: 3 additions & 35 deletions EmmyLua/CodeAnalysis/Compilation/Infer/ExpressionInfer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,41 +25,9 @@ public static LuaType InferExpr(LuaExprSyntax expr, SearchContext context)
};
}

private static LuaType UnwrapType(LuaType luaType, SearchContext context)
{
switch (luaType)
{
case LuaVariableRefType variableRefType:
{
var ty = context.Compilation.Db.QueryTypeFromId(variableRefType.Id);
if (ty is not null)
{
return ty;
}

return variableRefType;
}
case GlobalNameType globalNameType:
{
var ty = context.Compilation.Db.QueryRelatedGlobalType(globalNameType.Name);
if (ty is not null)
{
return ty;
}

return globalNameType;
}

default:
{
return luaType;
}
}
}

private static LuaType InferUnaryExpr(LuaUnaryExprSyntax unaryExpr, SearchContext context)
{
var exprTy = UnwrapType(context.Infer(unaryExpr.Expression), context);
var exprTy = context.Infer(unaryExpr.Expression).UnwrapType(context);
var opKind = TypeOperatorKindHelper.ToTypeOperatorKind(unaryExpr.Operator);
var op = context.GetBestMatchedUnaryOperator(opKind, exprTy);

Expand Down Expand Up @@ -126,8 +94,8 @@ private static LuaType GuessAndOrType(LuaBinaryExprSyntax binaryExpr, OperatorKi
private static LuaType GuessBinaryMathType(LuaBinaryExprSyntax binaryExpr, OperatorKind.BinaryOperator op,
SearchContext context)
{
var leftTy = UnwrapType(context.Infer(binaryExpr.LeftExpr), context);
var rightTy = UnwrapType(context.Infer(binaryExpr.RightExpr), context);
var leftTy = context.Infer(binaryExpr.LeftExpr).UnwrapType(context);
var rightTy = context.Infer(binaryExpr.RightExpr).UnwrapType(context);
var opKind = TypeOperatorKindHelper.ToTypeOperatorKind(op);
var bop = context.GetBestMatchedBinaryOperator(opKind, leftTy, rightTy);
if (bop is not null)
Expand Down
19 changes: 11 additions & 8 deletions EmmyLua/CodeAnalysis/Compilation/Infer/GenericInfer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using EmmyLua.CodeAnalysis.Common;
using EmmyLua.CodeAnalysis.Compilation.Search;
using EmmyLua.CodeAnalysis.Compilation.Search;
using EmmyLua.CodeAnalysis.Compilation.Type;
using EmmyLua.CodeAnalysis.Syntax.Node.SyntaxNodes;

Expand All @@ -14,13 +13,16 @@ public static void InferInstantiateByExpr(
Dictionary<string, LuaType> genericReplace,
SearchContext context)
{
if (type is LuaTemplateType templateType && expr is LuaLiteralExprSyntax {Literal: LuaStringToken {} stringToken})
if (type is LuaTemplateType templateType && expr is LuaLiteralExprSyntax
{
Literal: LuaStringToken { } stringToken
})
{
TemplateTypeInstantiateByString(templateType, stringToken, genericParameter, genericReplace, context);
return;
}

var exprType = context.Infer(expr);
var exprType = context.InferAndUnwrap(expr);
InferInstantiateByType(type, exprType, genericParameter, genericReplace, context);
}

Expand All @@ -32,7 +34,7 @@ public static void InferInstantiateByExpandTypeAndExprs(
SearchContext context
)
{
InferInstantiateByExpandTypeAndTypes(expandType, expr.Select(context.Infer), genericParameter, genericReplace,
InferInstantiateByExpandTypeAndTypes(expandType, expr.Select(context.InferAndUnwrap), genericParameter, genericReplace,
context);
}

Expand Down Expand Up @@ -173,7 +175,7 @@ private static void GenericTableExprInstantiate(
keyType = keyType.Union(Builtin.String);
}

var fieldValueType = context.Infer(fieldSyntax.Value);
var fieldValueType = context.InferAndUnwrap(fieldSyntax.Value);
valueType = valueType.Union(fieldValueType);
}

Expand Down Expand Up @@ -216,7 +218,7 @@ private static void ArrayTypeInstantiateByType(
{
if (field.IsValue)
{
var fieldValueType = context.Infer(field.Value);
var fieldValueType = context.InferAndUnwrap(field.Value);
valueType = valueType.Union(fieldValueType);
}
}
Expand Down Expand Up @@ -296,7 +298,8 @@ private static void TupleTypeInstantiateByType(
else
{
var rightElementType = tupleType2.TupleDeclaration[i].Type;
InferInstantiateByType(leftElementType, rightElementType, genericParameter, genericReplace, context);
InferInstantiateByType(leftElementType, rightElementType, genericParameter, genericReplace,
context);
}
}
}
Expand Down
Loading

0 comments on commit e781f35

Please sign in to comment.