@@ -27,12 +27,11 @@ limitations under the License.
2727
2828namespace CSharpLua {
2929 public sealed partial class LuaSyntaxNodeTransform : CSharpSyntaxVisitor < LuaSyntaxNode > {
30- public const int kStringConstInlineCount = 15 ;
30+ public const int kStringConstInlineCount = 27 ;
3131
3232 private sealed class MethodInfo {
3333 public IMethodSymbol Symbol { get ; }
3434 public IList < LuaExpressionSyntax > RefOrOutParameters { get ; }
35-
3635 public List < LuaIdentifierNameSyntax > InliningReturnVars { get ; set ; }
3736 public bool IsInlining => InliningReturnVars != null ;
3837 public bool HasYield ;
@@ -375,14 +374,15 @@ private void BuildTypeDeclaration(INamedTypeSymbol typeSymbol, TypeDeclarationSy
375374 BuildBaseTypes ( typeSymbol , typeDeclaration , node . BaseList . Types , false ) ;
376375 }
377376
378- BuildTypeMembers ( typeDeclaration , node ) ;
379- CheckTypeDeclaration ( typeSymbol , typeDeclaration , attributes ) ;
380-
377+ CheckRecordParameterCtor ( typeSymbol , node , typeDeclaration ) ;
378+ BuildTypeMembers ( typeDeclaration , node ) ;
379+ CheckTypeDeclaration ( typeSymbol , typeDeclaration , attributes , node ) ;
380+
381381 typeDeclarations_ . Pop ( ) ;
382382 CurCompilationUnit . AddTypeDeclarationCount ( ) ;
383383 }
384384
385- private void CheckTypeDeclaration ( INamedTypeSymbol typeSymbol , LuaTypeDeclarationSyntax typeDeclaration , List < LuaExpressionSyntax > attributes ) {
385+ private void CheckTypeDeclaration ( INamedTypeSymbol typeSymbol , LuaTypeDeclarationSyntax typeDeclaration , List < LuaExpressionSyntax > attributes , BaseTypeDeclarationSyntax node ) {
386386 if ( typeDeclaration . IsNoneCtros ) {
387387 var baseTypeSymbol = typeSymbol . BaseType ;
388388 if ( baseTypeSymbol != null ) {
@@ -408,8 +408,11 @@ private void CheckTypeDeclaration(INamedTypeSymbol typeSymbol, LuaTypeDeclaratio
408408 typeDeclaration . AddCtor ( function , true ) ;
409409 }
410410
411- if ( typeSymbol . BaseType != null && typeSymbol . BaseType . SpecialType == SpecialType . System_Object && typeSymbol . IsRecordType ( ) ) {
412- typeDeclaration . AddBaseTypes ( LuaIdentifierNameSyntax . RecordType . ArrayOf ( GetRecordInerfaceTypeName ( typeSymbol ) ) , null , null ) ;
411+ if ( typeSymbol . IsRecordType ( ) ) {
412+ if ( typeSymbol . BaseType != null && typeSymbol . BaseType . SpecialType == SpecialType . System_Object ) {
413+ typeDeclaration . AddBaseTypes ( LuaIdentifierNameSyntax . RecordType . ArrayOf ( GetRecordInerfaceTypeName ( typeSymbol ) ) , null , null ) ;
414+ }
415+ BuildRecordMembers ( typeSymbol , typeDeclaration ) ;
413416 }
414417
415418 if ( typeDeclaration . IsIgnoreExport ) {
@@ -425,6 +428,42 @@ private void CheckTypeDeclaration(INamedTypeSymbol typeSymbol, LuaTypeDeclaratio
425428 }
426429 }
427430
431+ private void CheckRecordParameterCtor ( INamedTypeSymbol typeSymbol , TypeDeclarationSyntax node , LuaTypeDeclarationSyntax typeDeclaration ) {
432+ if ( typeSymbol . IsRecordType ( ) ) {
433+ var recordDeclaration = ( RecordDeclarationSyntax ) node ;
434+ if ( recordDeclaration . ParameterList != null ) {
435+ BuildRecordParameterCtor ( typeSymbol , typeDeclaration , recordDeclaration ) ;
436+ }
437+ }
438+ }
439+
440+ private void BuildRecordParameterCtor ( INamedTypeSymbol typeSymbol , LuaTypeDeclarationSyntax typeDeclaration , RecordDeclarationSyntax recordDeclaration ) {
441+ var parameterList = recordDeclaration . ParameterList . Accept < LuaParameterListSyntax > ( this ) ;
442+ var function = new LuaConstructorAdapterExpressionSyntax ( ) ;
443+ function . AddParameter ( LuaIdentifierNameSyntax . This ) ;
444+ function . AddParameters ( parameterList . Parameters ) ;
445+ function . AddStatements ( parameterList . Parameters . Select ( i => LuaIdentifierNameSyntax . This . MemberAccess ( i ) . Assignment ( i ) . ToStatementSyntax ( ) ) ) ;
446+ typeDeclaration . AddCtor ( function , false ) ;
447+ var ctor = typeSymbol . InstanceConstructors . First ( ) ;
448+ int index = 0 ;
449+ foreach ( var p in ctor . Parameters ) {
450+ var expression = GetFieldValueExpression ( p . Type , null , out bool isLiteral , out _ ) ;
451+ if ( expression != null ) {
452+ typeDeclaration . AddField ( parameterList . Parameters [ index ] , expression , p . Type . IsImmutable ( ) && isLiteral , false , false , true , null , false ) ;
453+ }
454+ ++ index ;
455+ }
456+ }
457+
458+ private void BuildRecordMembers ( INamedTypeSymbol typeSymbol , LuaTypeDeclarationSyntax typeDeclaration ) {
459+ var propertys = typeSymbol . GetMembers ( ) . OfType < IPropertySymbol > ( ) . Skip ( 1 ) ;
460+ var exprssions = new List < LuaExpressionSyntax > ( ) { typeSymbol . Name . ToStringLiteral ( ) } ;
461+ exprssions . AddRange ( propertys . Select ( i => GetMemberName ( i ) . ToStringLiteral ( ) ) ) ;
462+ var function = new LuaFunctionExpressionSyntax ( ) ;
463+ function . AddStatement ( new LuaReturnStatementSyntax ( exprssions ) ) ;
464+ typeDeclaration . AddMethod ( LuaIdentifierNameSyntax . RecordMembers , function , false ) ;
465+ }
466+
428467 private INamedTypeSymbol VisitTypeDeclaration ( INamedTypeSymbol typeSymbol , TypeDeclarationSyntax node , LuaTypeDeclarationSyntax typeDeclaration ) {
429468 if ( node . Modifiers . IsPartial ( ) ) {
430469 if ( typeSymbol . DeclaringSyntaxReferences . Length > 1 ) {
@@ -485,7 +524,7 @@ internal void AcceptPartialType(PartialTypeDeclaration major, IEnumerable<Partia
485524 BuildTypeMembers ( major . TypeDeclaration , typeDeclaration . Node ) ;
486525 }
487526
488- CheckTypeDeclaration ( major . Symbol , major . TypeDeclaration , attributes ) ;
527+ CheckTypeDeclaration ( major . Symbol , major . TypeDeclaration , attributes , major . Node ) ;
489528 typeDeclarations_ . Pop ( ) ;
490529 compilationUnits_ . Pop ( ) ;
491530
@@ -528,7 +567,7 @@ private void VisitEnumDeclaration(INamedTypeSymbol typeSymbol, EnumDeclarationSy
528567 var statement = member . Accept < LuaKeyValueTableItemSyntax > ( this ) ;
529568 enumDeclaration . Add ( statement ) ;
530569 }
531- CheckTypeDeclaration ( typeSymbol , enumDeclaration , attributes ) ;
570+ CheckTypeDeclaration ( typeSymbol , enumDeclaration , attributes , node ) ;
532571 typeDeclarations_ . Pop ( ) ;
533572 generator_ . AddEnumDeclaration ( typeSymbol , enumDeclaration ) ;
534573 }
0 commit comments