Skip to content

Commit 3e3d23e

Browse files
committed
optimization ObjectCreationExpression with Initializer
1 parent f429dbe commit 3e3d23e

9 files changed

Lines changed: 95 additions & 63 deletions

File tree

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Core.lua‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ enumMetatable.__index = enumMetatable
163163
local interfaceMetatable = { __kind__ = "I", __default__ = emptyFn, __index = false }
164164
interfaceMetatable.__index = interfaceMetatable
165165

166+
local function applyAttributes(cls)
167+
local attributes = rawget(cls, "__attributes__")
168+
if attributes ~= nil then
169+
cls.__attributes__ = attributes(global)
170+
end
171+
end
172+
166173
local function setBase(cls, kind)
167174
cls.__index = cls
168175
cls.__call = new
@@ -199,11 +206,7 @@ local function setBase(cls, kind)
199206
setmetatable(cls, Object)
200207
end
201208
end
202-
203-
local attributes = rawget(cls, "__attributes__")
204-
if attributes ~= nil then
205-
cls.__attributes__ = attributes(global)
206-
end
209+
applyAttributes(cls)
207210
end
208211

209212
local function staticCtorSetBase(cls)
@@ -291,17 +294,11 @@ local function def(name, kind, cls, generic)
291294
cls.__interfaces__ = extends
292295
cls.__inherits__ = nil
293296
end
294-
local attributes = cls.__attributes__
295-
if attributes ~= nil then
296-
cls.__attributes__ = attributes(global)
297-
end
298297
setmetatable(cls, interfaceMetatable)
298+
applyAttributes(cls)
299299
elseif kind == "E" then
300-
local attributes = cls.__attributes__
301-
if attributes ~= nil then
302-
cls.__attributes__ = attributes(global)
303-
end
304300
setmetatable(cls, enumMetatable)
301+
applyAttributes(cls)
305302
else
306303
assert(false, kind)
307304
end
@@ -844,7 +841,7 @@ function System.usingX(f, ...)
844841
return status, ret
845842
end
846843

847-
function System.create(t, f)
844+
function System.apply(t, f)
848845
f(t)
849846
return t
850847
end

‎CSharp.lua/CoreSystem.Lua/Sample/test.lua‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ local function testAsync()
327327
async:await(t)
328328
async:await(t)
329329
System.Console.WriteLine(("Delay {0}"):Format(t:getStatus():ToEnumString(System.TaskStatus)))
330-
end, this)
330+
end, nil, this)
331331
end
332332
return {
333333
f = f,

‎CSharp.lua/LuaAst/LuaBlockSyntax.cs‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,27 @@ namespace CSharpLua.LuaAst {
2424
public class LuaBlockSyntax : LuaStatementSyntax {
2525
public string OpenBraceToken { get; set; }
2626
public string CloseBraceToken { get; set; }
27-
public LuaSyntaxList<LuaStatementSyntax> Statements { get; } = new LuaSyntaxList<LuaStatementSyntax>();
27+
public readonly LuaSyntaxList<LuaStatementSyntax> Statements = new LuaSyntaxList<LuaStatementSyntax>();
2828

2929
internal override void Render(LuaRenderer renderer) {
3030
renderer.Render(this);
3131
}
3232

3333
internal void AddLocalArea(LuaIdentifierNameSyntax name) {
34-
var localArea = Statements.FirstOrDefault() as LuaLocalAreaSyntax;
35-
if (localArea == null) {
34+
if (!(Statements.FirstOrDefault() is LuaLocalAreaSyntax localArea)) {
3635
localArea = new LuaLocalAreaSyntax();
3736
Statements.Insert(0, localArea);
3837
}
3938
localArea.Variables.Add(name);
4039
}
40+
41+
internal void AddStatement(LuaStatementSyntax statement) {
42+
Statements.Add(statement);
43+
}
44+
45+
internal void AddStatement(LuaExpressionSyntax expression) {
46+
Statements.Add(expression.ToStatement());
47+
}
4148
}
4249

4350
public sealed class LuaBlockStatementSyntax : LuaBlockSyntax {

‎CSharp.lua/LuaAst/LuaIdentifierNameSyntax.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class LuaIdentifierNameSyntax : LuaExpressionSyntax {
4444
public static readonly LuaIdentifierNameSyntax Array = new LuaIdentifierNameSyntax("System.Array");
4545
public static readonly LuaIdentifierNameSyntax ArrayEmpty = new LuaIdentifierNameSyntax("System.Array.Empty");
4646
public static readonly LuaIdentifierNameSyntax MultiArray = new LuaIdentifierNameSyntax("System.MultiArray");
47-
public static readonly LuaIdentifierNameSyntax Create = new LuaIdentifierNameSyntax("System.create");
47+
public static readonly LuaIdentifierNameSyntax Apply = new LuaIdentifierNameSyntax("System.apply");
4848
public static readonly LuaIdentifierNameSyntax Add = new LuaIdentifierNameSyntax("Add");
4949
public static readonly LuaIdentifierNameSyntax StaticCtor = new LuaIdentifierNameSyntax("__staticCtor__");
5050
public static readonly LuaIdentifierNameSyntax Init = new LuaIdentifierNameSyntax("__init__");

‎CSharp.lua/LuaAst/LuaTableInitializerExpression.cs‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ public sealed class LuaSingleTableItemSyntax : LuaTableItemSyntax {
4040
public LuaExpressionSyntax Expression { get; }
4141

4242
public LuaSingleTableItemSyntax(LuaExpressionSyntax expression) {
43-
if (expression == null) {
44-
throw new ArgumentNullException(nameof(expression));
45-
}
46-
Expression = expression;
43+
Expression = expression ?? throw new ArgumentNullException(nameof(expression));
4744
}
4845

4946
internal override void Render(LuaRenderer renderer) {
@@ -102,14 +99,8 @@ public sealed class LuaTableIndexAccessExpressionSyntax : LuaExpressionSyntax {
10299
public string CloseBracketToken => Tokens.CloseBracket;
103100

104101
public LuaTableIndexAccessExpressionSyntax(LuaExpressionSyntax expression, LuaExpressionSyntax index) {
105-
if (expression == null) {
106-
throw new ArgumentNullException(nameof(expression));
107-
}
108-
if (index == null) {
109-
throw new ArgumentNullException(nameof(index));
110-
}
111-
Expression = expression;
112-
Index = index;
102+
Expression = expression ?? throw new ArgumentNullException(nameof(expression));
103+
Index = index ?? throw new ArgumentNullException(nameof(index));
113104
}
114105

115106
internal override void Render(LuaRenderer renderer) {

‎CSharp.lua/LuaSyntaxNodeTransform.Helper.cs‎

Lines changed: 2 additions & 2 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");
@@ -744,7 +744,7 @@ public override LuaSyntaxNode VisitAttribute(AttributeSyntax node) {
744744
}
745745

746746
PopFunction();
747-
return new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.Create, invocation, function);
747+
return new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.Apply, invocation, function);
748748
}
749749
} else {
750750
return invocation;

‎CSharp.lua/LuaSyntaxNodeTransform.Object.cs‎

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,26 @@ public override LuaSyntaxNode VisitObjectCreationExpression(ObjectCreationExpres
7777
if (node.Initializer == null) {
7878
return creationExpression;
7979
} else {
80-
var functionExpression = BuildObjectInitializerExpression(node.Initializer);
81-
return new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.Create, creationExpression, functionExpression);
80+
if (CurBlockOrNull != null) {
81+
var temp = GetTempIdentifier(node);
82+
CurBlock.AddStatement(new LuaLocalVariableDeclaratorSyntax(temp, creationExpression));
83+
FillObjectInitializerExpression(temp, node.Initializer);
84+
return !node.Parent.IsKind(SyntaxKind.ExpressionStatement) ? temp : LuaExpressionSyntax.EmptyExpression;
85+
} else {
86+
LuaFunctionExpressionSyntax function = new LuaFunctionExpressionSyntax();
87+
PushFunction(function);
88+
blocks_.Push(function.Body);
89+
var temp = GetTempIdentifier(node);
90+
function.AddParameter(temp);
91+
FillObjectInitializerExpression(temp, node.Initializer);
92+
blocks_.Pop();
93+
PopFunction();
94+
return new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.Apply, creationExpression, function);
95+
}
8296
}
8397
}
8498

85-
private LuaFunctionExpressionSyntax BuildObjectInitializerExpression(InitializerExpressionSyntax node) {
86-
LuaFunctionExpressionSyntax function = new LuaFunctionExpressionSyntax();
87-
PushFunction(function);
88-
var temp = GetTempIdentifier(node);
89-
function.AddParameter(temp);
99+
private void FillObjectInitializerExpression(LuaIdentifierNameSyntax temp, InitializerExpressionSyntax node) {
90100
foreach (var expression in node.Expressions) {
91101
if (expression.IsKind(SyntaxKind.SimpleAssignmentExpression)) {
92102
AssignmentExpressionSyntax assignment = (AssignmentExpressionSyntax)expression;
@@ -99,11 +109,11 @@ private LuaFunctionExpressionSyntax BuildObjectInitializerExpression(Initializer
99109
LuaInvocationExpressionSyntax invocation = new LuaInvocationExpressionSyntax(memberAccess);
100110
invocation.ArgumentList.Arguments.AddRange(argumentList.Arguments);
101111
invocation.AddArgument(right);
102-
function.AddStatement(invocation);
112+
CurBlock.AddStatement(invocation);
103113
} else {
104114
var memberAccess = BuildFieldOrPropertyMemberAccessExpression(temp, (LuaExpressionSyntax)left, false);
105115
var assignmentExpression = BuildLuaSimpleAssignmentExpression(memberAccess, right);
106-
function.AddStatement(assignmentExpression);
116+
CurBlock.AddStatement(assignmentExpression);
107117
}
108118
} else {
109119
LuaMemberAccessExpressionSyntax memberAccess = new LuaMemberAccessExpressionSyntax(temp, LuaIdentifierNameSyntax.Add, true);
@@ -118,12 +128,9 @@ private LuaFunctionExpressionSyntax BuildObjectInitializerExpression(Initializer
118128
var value = (LuaExpressionSyntax)expression.Accept(this);
119129
invocation.AddArgument(value);
120130
}
121-
function.AddStatement(invocation);
131+
CurBlock.AddStatement(invocation);
122132
}
123133
}
124-
125-
PopFunction();
126-
return function;
127134
}
128135

129136
public override LuaSyntaxNode VisitAnonymousObjectMemberDeclarator(AnonymousObjectMemberDeclaratorSyntax node) {

‎CSharp.lua/LuaSyntaxNodeTransform.cs‎

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,18 @@ private List<LuaIdentifierNameSyntax> GetBaseCopyFields(BaseTypeSyntax baseType)
255255
if (baseType != null) {
256256
var baseTypeSymbol = semanticModel_.GetTypeInfo(baseType.Type).Type;
257257
if (baseTypeSymbol.TypeKind == TypeKind.Class && baseTypeSymbol.SpecialType != SpecialType.System_Object) {
258-
if(!baseTypeSymbol.GetMembers("Finalize").IsEmpty) {
258+
if (baseTypeSymbol.IsMemberExists("Finalize", true)) {
259259
return new List<LuaIdentifierNameSyntax>() { LuaIdentifierNameSyntax.__GC };
260260
}
261261
}
262262
}
263263
return null;
264264
}
265265

266+
private bool IsBaseTypeNotSystemObject(LuaExpressionSyntax baseTypeExpression) {
267+
return !(baseTypeExpression is LuaIdentifierNameSyntax name && name.ValueText == LuaIdentifierNameSyntax.Object.ValueText);
268+
}
269+
266270
private void BuildTypeDeclaration(INamedTypeSymbol typeSymbol, TypeDeclarationSyntax node, LuaTypeDeclarationSyntax typeDeclaration) {
267271
typeDeclarations_.Push(new TypeDeclarationInfo(typeSymbol, typeDeclaration));
268272

@@ -278,15 +282,19 @@ private void BuildTypeDeclaration(INamedTypeSymbol typeSymbol, TypeDeclarationSy
278282
var baseTypes = new List<LuaExpressionSyntax>();
279283
foreach (var baseType in node.BaseList.Types) {
280284
var baseTypeName = BuildInheritTypeName(baseType);
281-
baseTypes.Add(baseTypeName);
282-
CheckBaseTypeGenericKind(ref hasExtendSelf, typeSymbol, baseType);
285+
if (IsBaseTypeNotSystemObject(baseTypeName)) {
286+
baseTypes.Add(baseTypeName);
287+
CheckBaseTypeGenericKind(ref hasExtendSelf, typeSymbol, baseType);
288+
}
283289
}
284290

285-
var genericArgument = CheckSpeaicalGenericArgument(typeSymbol);
286-
var baseCopyFields = GetBaseCopyFields(node.BaseList.Types.FirstOrDefault());
287-
typeDeclaration.AddBaseTypes(baseTypes, genericArgument, baseCopyFields);
288-
if (hasExtendSelf && !typeSymbol.HasStaticCtor()) {
289-
typeDeclaration.SetStaticCtorEmpty();
291+
if (baseTypes.Count > 0) {
292+
var genericArgument = CheckSpeaicalGenericArgument(typeSymbol);
293+
var baseCopyFields = GetBaseCopyFields(node.BaseList.Types.FirstOrDefault());
294+
typeDeclaration.AddBaseTypes(baseTypes, genericArgument, baseCopyFields);
295+
if (hasExtendSelf && !typeSymbol.HasStaticCtor()) {
296+
typeDeclaration.SetStaticCtorEmpty();
297+
}
290298
}
291299
}
292300

@@ -391,14 +399,19 @@ internal void AcceptPartialType(PartialTypeDeclaration major, List<PartialTypeDe
391399
foreach (var baseType in baseTypes) {
392400
semanticModel_ = generator_.GetSemanticModel(baseType.SyntaxTree);
393401
var baseTypeName = BuildInheritTypeName(baseType);
394-
baseTypeExpressions.Add(baseTypeName);
395-
CheckBaseTypeGenericKind(ref hasExtendSelf, major.Symbol, baseType);
396-
}
397-
var genericArgument = CheckSpeaicalGenericArgument(major.Symbol);
398-
var baseCopyFields = GetBaseCopyFields(baseTypes.FirstOrDefault());
399-
major.TypeDeclaration.AddBaseTypes(baseTypeExpressions, genericArgument, baseCopyFields);
400-
if (hasExtendSelf && !major.Symbol.HasStaticCtor()) {
401-
major.TypeDeclaration.SetStaticCtorEmpty();
402+
if (IsBaseTypeNotSystemObject(baseTypeName)) {
403+
baseTypeExpressions.Add(baseTypeName);
404+
CheckBaseTypeGenericKind(ref hasExtendSelf, major.Symbol, baseType);
405+
}
406+
}
407+
408+
if (baseTypeExpressions.Count > 0) {
409+
var genericArgument = CheckSpeaicalGenericArgument(major.Symbol);
410+
var baseCopyFields = GetBaseCopyFields(baseTypes.FirstOrDefault());
411+
major.TypeDeclaration.AddBaseTypes(baseTypeExpressions, genericArgument, baseCopyFields);
412+
if (hasExtendSelf && !major.Symbol.HasStaticCtor()) {
413+
major.TypeDeclaration.SetStaticCtorEmpty();
414+
}
402415
}
403416
}
404417

‎CSharp.lua/Utils.cs‎

Lines changed: 18 additions & 1 deletion
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");
@@ -895,6 +895,23 @@ public static bool IsAbsoluteFromCode(this ITypeSymbol symbol) {
895895
return false;
896896
}
897897

898+
public static bool IsMemberExists(this ITypeSymbol symbol, string memberName, bool inherit = false) {
899+
if (inherit) {
900+
while (true) {
901+
if (!symbol.GetMembers(memberName).IsEmpty) {
902+
return true;
903+
}
904+
905+
symbol = symbol.BaseType;
906+
if (symbol == null || symbol.SpecialType == SpecialType.System_Object) {
907+
return false;
908+
}
909+
}
910+
} else {
911+
return !symbol.GetMembers(memberName).IsEmpty;
912+
}
913+
}
914+
898915
public static LuaExpressionStatementSyntax ToStatement(this LuaExpressionSyntax expression) {
899916
return new LuaExpressionStatementSyntax(expression);
900917
}

0 commit comments

Comments
 (0)