From 6a955d19f743ed7e74c31fee2ef1d4112ee286f7 Mon Sep 17 00:00:00 2001 From: yanghuan Date: Wed, 24 Jan 2024 16:59:23 +0800 Subject: [PATCH] fix #469 --- CSharp.lua/LuaSyntaxNodeTransform.cs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/CSharp.lua/LuaSyntaxNodeTransform.cs b/CSharp.lua/LuaSyntaxNodeTransform.cs index c797a38a..5b074975 100644 --- a/CSharp.lua/LuaSyntaxNodeTransform.cs +++ b/CSharp.lua/LuaSyntaxNodeTransform.cs @@ -400,7 +400,7 @@ private void BuildTypeDeclaration(INamedTypeSymbol typeSymbol, TypeDeclarationSy BuildBaseTypes(typeSymbol, typeDeclaration, node.BaseList.Types, false); } - CheckRecordParameterCtor(typeSymbol, node, typeDeclaration); + CheckParameterCtor(typeSymbol, node, typeDeclaration); BuildTypeMembers(typeDeclaration, node); CheckTypeDeclaration(typeSymbol, typeDeclaration, attributes); @@ -458,28 +458,29 @@ private void CheckTypeDeclaration(INamedTypeSymbol typeSymbol, LuaTypeDeclaratio } } - private void CheckRecordParameterCtor(INamedTypeSymbol typeSymbol, TypeDeclarationSyntax node, LuaTypeDeclarationSyntax typeDeclaration) { - if (typeSymbol.IsRecord) { - var recordDeclaration = (RecordDeclarationSyntax)node; - if (recordDeclaration.ParameterList != null) { - BuildRecordParameterCtor(typeSymbol, typeDeclaration, recordDeclaration); - } + private void CheckParameterCtor(INamedTypeSymbol typeSymbol, TypeDeclarationSyntax node, LuaTypeDeclarationSyntax typeDeclaration) { + if (node.ParameterList != null) { + BuildParameterCtor(typeSymbol, typeDeclaration, node); } } - private void BuildRecordParameterCtor(INamedTypeSymbol typeSymbol, LuaTypeDeclarationSyntax typeDeclaration, RecordDeclarationSyntax recordDeclaration) { - var parameterList = recordDeclaration.ParameterList.Accept(this); + private void BuildParameterCtor(INamedTypeSymbol typeSymbol, LuaTypeDeclarationSyntax typeDeclaration, TypeDeclarationSyntax node) { + bool isRecord = node.IsKind(SyntaxKind.RecordDeclaration); + var parameterList = node!.ParameterList.Accept(this); var function = new LuaConstructorAdapterExpressionSyntax(); function.AddParameter(LuaIdentifierNameSyntax.This); function.AddParameters(parameterList.Parameters); - function.AddStatements(parameterList.Parameters.Select(i => LuaIdentifierNameSyntax.This.MemberAccess(i).Assignment(i).ToStatementSyntax())); typeDeclaration.AddCtor(function, false); var ctor = typeSymbol.InstanceConstructors.First(); int index = 0; foreach (var p in ctor.Parameters) { - var expression = GetFieldValueExpression(p.Type, null, out bool isLiteral, out _); - if (expression != null) { - typeDeclaration.AddField(parameterList.Parameters[index], expression, p.Type.IsImmutable() && isLiteral, false, false, true, null, false, false); + if (isRecord || !typeSymbol.GetMembers($"<{p.Name}>P").IsEmpty) { + var parameterName = parameterList.Parameters[index]; + function.AddStatement(LuaIdentifierNameSyntax.This.MemberAccess(parameterName).Assignment(parameterName)); + var expression = GetFieldValueExpression(p.Type, null, out bool isLiteral, out _); + if (expression != null) { + typeDeclaration.AddField(parameterList.Parameters[index], expression, p.Type.IsImmutable() && isLiteral, false, false, true, null, false, false); + } } ++index; }