Skip to content

Commit

Permalink
[BUG] Poco array fields are null (#312)
Browse files Browse the repository at this point in the history
* Create draft PR for #311

* The most significant changes involve the addition of the `CsPlainConstructorBuilder.cs` file and modifications to the `CsPlainSourceBuilder.cs` file. The new file contains the `CsPlainConstructorBuilder` class, which is used to build constructors for classes. The `CsPlainSourceBuilder.cs` file was updated to use this new class, specifically the `Create` method, to add constructors to the source code. Additionally, the `Arrays.cs` file was updated with a new method for creating and initializing arrays, and the `PlainersSwappingTests.cs` file had code for initializing an array commented out.

Changes:

1. Addition of `CsPlainConstructorBuilder.cs` file: This new file contains the `CsPlainConstructorBuilder` class, which is used to build constructors for classes. It implements the `ICombinedThreeVisitor` interface and contains methods for creating different types of declarations and adding them to the source code. It also contains a `Create` method which is used to create a new instance of the `CsPlainConstructorBuilder` class and add the constructor to the source code.

2. Modification of `CsPlainSourceBuilder.cs` file: The `using AXSharp.Compiler.Cs.Onliner;` directive was added to allow the `CsPlainSourceBuilder` class to use the `CsPlainConstructorBuilder` class. The `CsPlainConstructorBuilder.Create` method was added to the `CreateClassDeclaration` method, creating a new instance of the `CsPlainConstructorBuilder` class and adding the constructor to the source code.

3. Addition of `InstantiateArray` method in `Arrays.cs` file: This new method is used to create a new array and initialize it with a specified initializer function and array bounds.

4. Commenting out of array initialization in `PlainersSwappingTests.cs` file: The code for initializing the `ArrayOfDrives` array was commented out, suggesting that the array is now being initialized elsewhere, possibly in the constructor of the `p` object.

---------

Co-authored-by: PTKu <61538034+PTKu@users.noreply.github.com>
  • Loading branch information
IX-BOT and PTKu committed May 10, 2024
1 parent 5c015b8 commit f72e44b
Show file tree
Hide file tree
Showing 43 changed files with 653 additions and 173 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// AXSharp.Compiler.Cs
// Copyright (c) 2023 Peter Kurhajec (PTKu), MTS, and Contributors. All Rights Reserved.
// Contributors: https://github.com/ix-ax/axsharp/graphs/contributors
// See the LICENSE file in the repository root for more information.
// https://github.com/ix-ax/axsharp/blob/dev/LICENSE
// Third party licenses: https://github.com/ix-ax/axsharp/blob/master/notices.md

using System.Globalization;
using System.Text;
using AX.ST.Semantic;
using AX.ST.Semantic.Model;
using AX.ST.Semantic.Model.Declarations;
using AX.ST.Semantic.Model.Declarations.Types;
using AX.ST.Syntax.Parser;
using AXSharp.Compiler.Core;
using AXSharp.Compiler.Cs.Helpers;
using AXSharp.Compiler.Cs.Helpers.Onliners;
using AXSharp.Connector;
using AXSharp.Connector.BuilderHelpers;
using AXSharp.Compiler.Cs;

namespace AXSharp.Compiler.Cs.Onliner;

internal class CsPlainConstructorBuilder : ICombinedThreeVisitor
{
private readonly StringBuilder _constructorStatements = new();

protected CsPlainConstructorBuilder(ISourceBuilder sourceBuilder)
{
SourceBuilder = sourceBuilder;
}

protected ISourceBuilder SourceBuilder { get; }

public string Output => _constructorStatements.ToString().FormatCode();

public void CreateClassDeclaration(IClassDeclaration classDeclaration, IxNodeVisitor visitor)
{
AddToSource($"{classDeclaration.GetQualifiedName()}");
}

public void CreateReferenceToDeclaration(IReferenceTypeDeclaration referenceTypeDeclaration, IxNodeVisitor visitor)
{
referenceTypeDeclaration.ReferencedType.Accept(visitor, this);
}

public void CreateScalarTypeDeclaration(IScalarTypeDeclaration scalarTypeDeclaration, IxNodeVisitor visitor)
{
AddToSource($"{scalarTypeDeclaration.TransformType()}");
}

public void CreateSemanticTypeAccess(ISemanticTypeAccess semanticTypeAccess, IxNodeVisitor visitor)
{
semanticTypeAccess.Type.Accept(visitor, this);
}

public void CreateStringTypeDeclaration(IStringTypeDeclaration stringTypeDeclaration, IxNodeVisitor visitor)
{
AddToSource($"{stringTypeDeclaration.TransformType()}");
}

public void CreateStructuredType(IStructuredTypeDeclaration structuredTypeDeclaration, IxNodeVisitor visitor)
{
AddToSource($"{structuredTypeDeclaration.GetQualifiedName()}");
}

public void CreateFieldDeclaration(IFieldDeclaration fieldDeclaration, IxNodeVisitor visitor)
{
if (fieldDeclaration.IsMemberEligibleForConstructor(SourceBuilder))
{
switch (fieldDeclaration.Type)
{
case IArrayTypeDeclaration array:
AddArrayMemberInitialization(array, fieldDeclaration, visitor);
break;
}
}
}

public virtual void CreateNamedValueTypeDeclaration(INamedValueTypeDeclaration namedValueTypeDeclaration,
IxNodeVisitor visitor)
{
AddToSource(namedValueTypeDeclaration.ValueTypeAccess.Type.Name.ToUpperInvariant());
}

public void CreateInterfaceDeclaration(IInterfaceDeclaration interfaceDeclaration, IxNodeVisitor visitor)
{
// No way to construct interfaces.
}

public void CreateArrayTypeDeclaration(IArrayTypeDeclaration arrayTypeDeclaration, IxNodeVisitor visitor)
{

}

protected void AddToSource(string token, string separator = " ")
{
_constructorStatements.Append($"{token}{separator}");
}

public static CsPlainConstructorBuilder Create(IxNodeVisitor visitor, IClassDeclaration semantics,
ISourceBuilder sourceBuilder, bool isExtended, AXSharpProject project)
{
var builder = new CsPlainConstructorBuilder(sourceBuilder);


builder.AddToSource(
$"public {semantics.Name}()");


if (isExtended)
{
builder.AddToSource(": base()");
}

builder.AddToSource("{");

semantics.Fields.ToList().ForEach(p => p.Accept(visitor, builder));

builder.AddToSource("}");
return builder;
}

private void AddArrayMemberInitialization(IArrayTypeDeclaration type, IFieldDeclaration field,
IxNodeVisitor visitor)
{
if(!type.IsMemberEligibleForConstructor(this.SourceBuilder))
return;

switch (type.ElementTypeAccess.Type)
{
case IClassDeclaration classDeclaration:
case IStructuredTypeDeclaration structuredTypeDeclaration:
case IEnumTypeDeclaration enumTypeDeclaration:
case INamedValueTypeDeclaration namedValueTypeDeclaration:
AddToSource("#pragma warning disable CS0612\n");
AddToSource($"{typeof(Arrays).n()}.InstantiateArray({field.Name}, " +
"() => ");
AddToSource("new");
type.ElementTypeAccess.Type.Accept(visitor, this);
var dimensions = "new[] {";
foreach (var dimension in type.Dimensions)
{
dimensions = $"{dimensions}({dimension.LowerBoundValue}, {dimension.UpperBoundValue})";
}

dimensions = $"{dimensions}}}";

AddToSource($"(), {dimensions});");
AddToSource("#pragma warning restore CS0612\n");
break;
}
}



public void AddTypeConstructionParameters(string parametersString)
{
AddToSource(parametersString);
}

/// <inheritdoc />
public void CreateDocComment(IDocComment semanticTypeAccess, ICombinedThreeVisitor data)
{
AddToSource(semanticTypeAccess.AddDocumentationComment(SourceBuilder));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using AXSharp.Compiler.Core;
using AXSharp.Compiler.Cs.Helpers;
using AXSharp.Compiler.Cs.Helpers.Plain;
using AXSharp.Compiler.Cs.Onliner;

namespace AXSharp.Compiler.Cs.Plain;

Expand Down Expand Up @@ -88,6 +89,7 @@ public class CsPlainSourceBuilder : ICombinedThreeVisitor, ISourceBuilder


AddToSource("{");
AddToSource(CsPlainConstructorBuilder.Create(visitor, classDeclaration, this, isExtended, Project).Output);
classDeclarationSyntax.UsingDirectives.ToList().ForEach(p => p.Visit(visitor, this));
classDeclaration.Fields.ToList().ForEach(p => p.Accept(visitor, this));
AddToSource("}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ namespace Pocos
{
public partial class AbstractMotor : AXSharp.Connector.IPlain
{
public AbstractMotor()
{
}

public Boolean Run { get; set; }

public Boolean ReverseDirection { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@ namespace ArrayDeclarationSimpleNamespace
{
public partial class array_declaration_class : AXSharp.Connector.IPlain
{
public array_declaration_class()
{
#pragma warning disable CS0612
AXSharp.Connector.BuilderHelpers.Arrays.InstantiateArray(complex, () => new ArrayDeclarationSimpleNamespace.some_complex_type(), new[] { (1, 100) });
#pragma warning restore CS0612
}

public Int16[] primitive { get; set; } = new Int16[100];
public ArrayDeclarationSimpleNamespace.some_complex_type[] complex { get; set; } = new ArrayDeclarationSimpleNamespace.some_complex_type[100];
}

public partial class some_complex_type : AXSharp.Connector.IPlain
{
public some_complex_type()
{
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ namespace Pocos
{
public partial class class_all_primitives : AXSharp.Connector.IPlain
{
public class_all_primitives()
{
}

public Boolean myBOOL { get; set; }

public Byte myBYTE { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ namespace Simatic.Ax.StateFramework
{
public partial class State1Transition : Simatic.Ax.StateFramework.AbstractState, AXSharp.Connector.IPlain
{
public State1Transition() : base()
{
}
}
}

namespace Simatic.Ax.StateFramework
{
public partial class AbstractState : AXSharp.Connector.IPlain, IState, IStateMuteable
{
public AbstractState()
{
}

public Int16 StateID { get; set; }

public string StateName { get; set; } = string.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ namespace Pocos
{
public partial class Extended : Extendee, AXSharp.Connector.IPlain
{
public Extended() : base()
{
}
}

public partial class Extendee : AXSharp.Connector.IPlain
{
public Extendee()
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ namespace Pocos
{
public partial class ExtendsAndImplements : ExtendeeExtendsAndImplements, AXSharp.Connector.IPlain, IImplementation1, IImplementation2
{
public ExtendsAndImplements() : base()
{
}
}

public partial class ExtendeeExtendsAndImplements : AXSharp.Connector.IPlain
{
public ExtendeeExtendsAndImplements()
{
}
}

public partial interface IImplementation1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,35 @@ namespace Generics
{
public partial class Extender : AXSharp.Connector.IPlain
{
public Extender()
{
}
}

public partial class Extendee : Generics.Extender, AXSharp.Connector.IPlain
{
public Extendee() : base()
{
}

public Generics.SomeType SomeType { get; set; } = new Generics.SomeType();
public Generics.SomeType SomeTypeAsPoco { get; set; } = new Generics.SomeType();
}

public partial class Extendee2 : Generics.Extender, AXSharp.Connector.IPlain
{
public Extendee2() : base()
{
}

public Generics.SomeType SomeType { get; set; } = new Generics.SomeType();
}

public partial class SomeType : AXSharp.Connector.IPlain
{
public SomeType()
{
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ namespace Pocos
{
public partial class _NULL_CONTEXT : AXSharp.Connector.IPlain, IContext
{
public _NULL_CONTEXT()
{
}
}

public partial interface IContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ namespace Pocos
{
public partial class _NULL_CONTEXT_MULTIPLE : AXSharp.Connector.IPlain, IContext_Multiple, IObject_Multiple
{
public _NULL_CONTEXT_MULTIPLE()
{
}
}

public partial interface IContext_Multiple
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ namespace Pocos
{
internal partial class ClassWithComplexTypes : AXSharp.Connector.IPlain
{
public ClassWithComplexTypes()
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ namespace Pocos
{
public partial class NoAccessModifierClass : AXSharp.Connector.IPlain
{
public NoAccessModifierClass()
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ namespace ClassWithComplexTypesNamespace
{
public partial class ClassWithComplexTypes : AXSharp.Connector.IPlain
{
public ClassWithComplexTypes()
{
}

public ClassWithComplexTypesNamespace.ComplexType1 myComplexType { get; set; } = new ClassWithComplexTypesNamespace.ComplexType1();
}

public partial class ComplexType1 : AXSharp.Connector.IPlain
{
public ComplexType1()
{
}
}
}
}
Loading

0 comments on commit f72e44b

Please sign in to comment.