-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BUG] Poco array fields are null (#312)
* 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
Showing
43 changed files
with
653 additions
and
173 deletions.
There are no files selected for viewing
167 changes: 167 additions & 0 deletions
167
src/AXSharp.compiler/src/AXSharp.Cs.Compiler/Plain/CsPlainConstructorBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.