Skip to content

Commit 0183f34

Browse files
committed
Optimization of generic instantiation, use more local var
1 parent 9db2755 commit 0183f34

6 files changed

Lines changed: 88 additions & 33 deletions

File tree

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Boolean.lua‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ local Boolean = {
4747
debug.setmetatable(false, Boolean)
4848

4949
function Boolean.CompareToObj(this, v)
50-
if v == null then return 1 end
50+
if v == nil then return 1 end
5151
if type(v) ~= "boolean" then
5252
throw(ArgumentException("Arg_MustBeBoolean"))
5353
end

‎CSharp.lua/LuaAst/LuaCompilationUnitSyntax.cs‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
Copyright 2017 YANG Huan (sy.yanghuan@gmail.com).
33
44
Licensed under the Apache License, Version 2.0 (the "License");
@@ -23,6 +23,23 @@ limitations under the License.
2323
using System.Text;
2424

2525
namespace CSharpLua.LuaAst {
26+
public sealed class GenericUsingDeclare : IComparable<GenericUsingDeclare> {
27+
public LuaInvocationExpressionSyntax InvocationExpression;
28+
public string NewName;
29+
public List<string> ArgumentTypeNames;
30+
public bool IsFromCode;
31+
32+
public int CompareTo(GenericUsingDeclare other) {
33+
if (other.ArgumentTypeNames.Contains(NewName)) {
34+
return -1;
35+
}
36+
if (ArgumentTypeNames.Contains(other.NewName)) {
37+
return 1;
38+
}
39+
return NewName.CompareTo(other.NewName);
40+
}
41+
}
42+
2643
public sealed class LuaCompilationUnitSyntax : LuaSyntaxNode {
2744
private sealed class UsingDeclare : IComparable<UsingDeclare> {
2845
public string Prefix;
@@ -34,23 +51,6 @@ public int CompareTo(UsingDeclare other) {
3451
}
3552
}
3653

37-
private sealed class GenericUsingDeclare : IComparable<GenericUsingDeclare> {
38-
public LuaInvocationExpressionSyntax InvocationExpression;
39-
public string NewName;
40-
public List<string> ArgumentTypeNames;
41-
public bool IsFromCode;
42-
43-
public int CompareTo(GenericUsingDeclare other) {
44-
if (other.ArgumentTypeNames.Contains(NewName)) {
45-
return -1;
46-
}
47-
if (ArgumentTypeNames.Contains(other.NewName)) {
48-
return 1;
49-
}
50-
return NewName.CompareTo(other.NewName);
51-
}
52-
}
53-
5454
public string FilePath { get; }
5555
public readonly LuaSyntaxList<LuaStatementSyntax> Statements = new LuaSyntaxList<LuaStatementSyntax>();
5656
private LuaStatementListSyntax importAreaStatements = new LuaStatementListSyntax();

‎CSharp.lua/LuaAst/LuaTypeDeclarationSyntax.cs‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@ public abstract class LuaTypeDeclarationSyntax : LuaWrapFunctionStatementSynatx
4343
private List<LuaConstructorAdapterExpressionSyntax> ctors_ = new List<LuaConstructorAdapterExpressionSyntax>();
4444

4545
private List<LuaParameterSyntax> typeParameters_ = new List<LuaParameterSyntax>();
46+
private List<GenericUsingDeclare> genericUsingDeclares_ = new List<GenericUsingDeclare>();
47+
4648
private LuaTableInitializerExpression attributes_ = new LuaTableInitializerExpression();
4749
private LuaDocumentStatement document_ = new LuaDocumentStatement();
4850
public bool IsIgnoreExport => document_.HasIgnoreAttribute;
4951

50-
public LuaTypeDeclarationSyntax() {
51-
}
52-
5352
internal void AddStaticReadOnlyAssignmentName(LuaIdentifierNameSyntax name) {
5453
if (!staticAssignmentNames_.Contains(name)) {
5554
staticAssignmentNames_.Add(name);
@@ -84,6 +83,17 @@ internal void AddTypeParameters(IEnumerable<LuaParameterSyntax> typeParameters)
8483
typeParameters_.AddRange(typeParameters);
8584
}
8685

86+
internal void AddImport(LuaInvocationExpressionSyntax invocationExpression, string name, List<string> argumentTypeNames, bool isFromCode) {
87+
if (!genericUsingDeclares_.Exists(i => i.NewName == name)) {
88+
genericUsingDeclares_.Add(new GenericUsingDeclare() {
89+
InvocationExpression = invocationExpression,
90+
NewName = name,
91+
ArgumentTypeNames = argumentTypeNames,
92+
IsFromCode = isFromCode,
93+
});
94+
}
95+
}
96+
8797
internal void AddBaseTypes(IEnumerable<LuaExpressionSyntax> baseTypes, LuaSpeaicalGenericType genericArgument, List<LuaIdentifierNameSyntax> baseCopyFields) {
8898
bool hasLazyGenericArgument = false;
8999
if (genericArgument != null) {
@@ -188,6 +198,7 @@ public void AddField(LuaIdentifierNameSyntax name, LuaExpressionSyntax value, bo
188198
}
189199
LuaAssignmentExpressionSyntax assignment = new LuaAssignmentExpressionSyntax(name, value);
190200
staticInitStatements_.Add(new LuaExpressionStatementSyntax(assignment));
201+
staticAssignmentNames_.Add(name);
191202
}
192203
}
193204
} else {
@@ -198,7 +209,7 @@ public void AddField(LuaIdentifierNameSyntax name, LuaExpressionSyntax value, bo
198209
if (statements != null) {
199210
staticInitStatements_.AddRange(statements);
200211
}
201-
LuaAssignmentExpressionSyntax assignment = new LuaAssignmentExpressionSyntax(new LuaMemberAccessExpressionSyntax(LuaIdentifierNameSyntax.This, name), value);
212+
var assignment = new LuaAssignmentExpressionSyntax(new LuaMemberAccessExpressionSyntax(LuaIdentifierNameSyntax.This, name), value);
202213
staticInitStatements_.Add(new LuaExpressionStatementSyntax(assignment));
203214
}
204215
}
@@ -413,8 +424,18 @@ private void CheckAttributes() {
413424
}
414425
}
415426

427+
private void CheckGenericUsingDeclares(LuaBlockSyntax body) {
428+
if (genericUsingDeclares_.Count > 0) {
429+
genericUsingDeclares_.Sort();
430+
foreach (var import in genericUsingDeclares_) {
431+
body.AddStatement(new LuaLocalVariableDeclaratorSyntax(new LuaIdentifierNameSyntax(import.NewName), import.InvocationExpression));
432+
}
433+
}
434+
}
435+
416436
private void AddAllStatementsTo(LuaBlockSyntax body) {
417437
body.Statements.Add(local_);
438+
CheckGenericUsingDeclares(body);
418439
CheckStaticCtorFunction(body);
419440
CheckCtorsFunction(body);
420441
body.Statements.Add(methodList_);

‎CSharp.lua/LuaSyntaxGenerator.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,7 @@ internal LuaIdentifierNameSyntax GetTypeShortName(ISymbol symbol, LuaSyntaxNodeT
14401440
name = LuaIdentifierNameSyntax.Global.ValueText + '.' + name;
14411441
}
14421442
} else {
1443-
transfor.ImportTypeName(ref name, symbol);
1443+
transfor.ImportTypeName(ref name, typeSymbol);
14441444
}
14451445
}
14461446
return new LuaIdentifierNameSyntax(name);

‎CSharp.lua/LuaSyntaxNodeTransform.Helper.cs‎

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,35 @@ private bool IsLocalVarExistsInCurMethod(string name) {
582582
return false;
583583
}
584584

585-
internal void ImportTypeName(ref string name, ISymbol symbol) {
586-
if (!IsGetInheritTypeName) {
585+
private bool IsCurMethodTypeArgument(ITypeSymbol symbol) {
586+
var method = CurMethodInfoOrNull;
587+
if (method != null && method.Symbol.TypeArguments.Length > 0) {
588+
bool isMethodTypeArgument = method.Symbol.TypeArguments.Any(i => symbol.IsTypeParameterExists(i));
589+
if (isMethodTypeArgument) {
590+
return true;
591+
}
592+
}
593+
return false;
594+
}
595+
596+
private bool IsImportTypeNameEnale(INamedTypeSymbol symbol) {
597+
if (symbol.IsGenericType) {
598+
if (generator_.IsSealed(CurTypeSymbol)) {
599+
return true;
600+
}
601+
602+
if (IsCurMethodTypeArgument(symbol)) {
603+
return true;
604+
}
605+
606+
return false;
607+
}
608+
609+
return true;
610+
}
611+
612+
internal void ImportTypeName(ref string name, INamedTypeSymbol symbol) {
613+
if (IsImportTypeNameEnale(symbol)) {
587614
int pos = name.LastIndexOf('.');
588615
if (pos != -1) {
589616
string prefix = name.Substring(0, pos);
@@ -599,11 +626,18 @@ internal void ImportTypeName(ref string name, ISymbol symbol) {
599626
}
600627

601628
internal void ImportTypeName(ref LuaExpressionSyntax luaExpression, ITypeSymbol symbol) {
602-
if (!IsGetInheritTypeName && !symbol.IsTypeParameterExists()) {
629+
if (!IsGetInheritTypeName) {
603630
var invocationExpression = (LuaInvocationExpressionSyntax)luaExpression;
604631
string newName = GetGenericTypeImportName(invocationExpression, out var argumentTypeNames);
605632
if (!IsLocalVarExistsInCurMethod(newName)) {
606-
CurCompilationUnit.AddImport(invocationExpression, newName, argumentTypeNames, symbol.IsAbsoluteFromCode());
633+
if (!symbol.IsTypeParameterExists()) {
634+
CurCompilationUnit.AddImport(invocationExpression, newName, argumentTypeNames, symbol.IsAbsoluteFromCode());
635+
} else {
636+
if (IsCurMethodTypeArgument(symbol)) {
637+
return;
638+
}
639+
CurTypeDeclaration.TypeDeclaration.AddImport(invocationExpression, newName, argumentTypeNames, symbol.IsAbsoluteFromCode());
640+
}
607641
luaExpression = new LuaIdentifierNameSyntax(newName);
608642
}
609643
}

‎CSharp.lua/Utils.cs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -837,31 +837,31 @@ public static bool IsCollectionType(this INamedTypeSymbol symbol) {
837837
return false;
838838
}
839839

840-
public static bool IsTypeParameterExists(this ITypeSymbol symbol) {
840+
public static bool IsTypeParameterExists(this ITypeSymbol symbol, ITypeSymbol matchType = null) {
841841
switch (symbol.Kind) {
842842
case SymbolKind.ArrayType: {
843843
var arrayType = (IArrayTypeSymbol)symbol;
844-
if (arrayType.ElementType.IsTypeParameterExists()) {
844+
if (arrayType.ElementType.IsTypeParameterExists(matchType)) {
845845
return true;
846846
}
847847
break;
848848
}
849849
case SymbolKind.NamedType: {
850850
var nameTypeSymbol = (INamedTypeSymbol)symbol;
851851
foreach (var typeArgument in nameTypeSymbol.TypeArguments) {
852-
if (typeArgument.IsTypeParameterExists()) {
852+
if (typeArgument.IsTypeParameterExists(matchType)) {
853853
return true;
854854
}
855855
}
856856
if (symbol.ContainingType != null) {
857-
if (symbol.ContainingType.IsTypeParameterExists()) {
857+
if (symbol.ContainingType.IsTypeParameterExists(matchType)) {
858858
return true;
859859
}
860860
}
861861
break;
862862
}
863863
case SymbolKind.TypeParameter: {
864-
return true;
864+
return matchType == null || symbol == matchType;
865865
}
866866
}
867867

0 commit comments

Comments
 (0)