Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

354 update to stc v8 #356

Merged
merged 7 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion GitVersion.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mode: ContinuousDeployment
next-version: 0.20.1
next-version: 0.21.0
branches:
main:
regex: ^master$|^main$
Expand Down
40 changes: 40 additions & 0 deletions src/AXSharp.compiler/src/AXSharp.Compiler/AXSharpProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,46 @@ public void Generate()
}
}

foreach (var sourceBuilderType in BuilderTypes)
{
var builder = Activator.CreateInstance(sourceBuilderType, this, compilationResult.Compilation);
var treeWalker = builder as ICombinedThreeVisitor;
var sourceBuilder = builder as ISourceBuilder;


if (treeWalker == null)
throw new FailedToCreateCombineThreeVisitorException(
$"Could not create {sourceBuilderType.Name} as ICombinedThreeVisitor");
if (sourceBuilder == null)
throw new FailedToCreateSourceBuilderException(
$"Could not create {sourceBuilderType.Name} as ISourceBuilder");

var visitor = new IxNodeVisitor(compilationResult.Compilation);

try
{
treeWalker.CreateMergedConfigurations(visitor, compilationResult.Compilation);

Policy
.Handle<IOException>()
.WaitAndRetry(5, a => TimeSpan.FromMilliseconds(500))
.Execute(() =>
{
using (var swr = new StreamWriter(Path.Combine(
EnsureFolder(Path.Combine(OutputFolder, ".g")),
"Configurations.g.cs")))
{
swr.Write(sourceBuilder.Output);
}
});
}
catch (NotImplementedException)
{

// swallow if not implemented
}
}

//TargetProject.ProvisionProjectStructure();
GenerateMetadata(compilationResult.Compilation);
TargetProject.GenerateResources();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ namespace AXSharp.Compiler.Core;
/// </summary>
public interface ICombinedThreeVisitor
{

public virtual void CreateMergedConfigurations(IxNodeVisitor visitor, Compilation compilation)
{
throw new NotImplementedException();
}


/// <summary>
/// Creates file declaration from <see cref="IFileSyntax" /> node of given syntax tree.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,37 @@ protected CsOnlinerConfigurationConstructorBuilder(ISourceBuilder sourceBuilder)
return builder;
}

public new static CsOnlinerConfigurationConstructorBuilder Create(IxNodeVisitor visitor,
IReadOnlyCollection<IConfigurationDeclaration> semantics, AXSharpProject project, ISourceBuilder sourceBuilder)
{
var builder = new CsOnlinerConfigurationConstructorBuilder(sourceBuilder);
builder.AddToSource(
$"public {project.TargetProject.ProjectRootNamespace}TwinController({typeof(ConnectorAdapter).n()} adapter, object[] parameters) {{");
builder.AddToSource("this.Connector = adapter.GetConnector(parameters);");


foreach (var conf in semantics)
{
conf.Variables.ToList().ForEach(p => p.Accept(visitor, builder));
}


builder.AddToSource("}");

builder.AddToSource(
$"public {project.TargetProject.ProjectRootNamespace}TwinController({typeof(ConnectorAdapter).n()} adapter) {{");
builder.AddToSource("this.Connector = adapter.GetConnector(adapter.Parameters);");

foreach (var conf in semantics)
{
conf.Variables.ToList().ForEach(p => p.Accept(visitor, builder));
}

builder.AddToSource("}");

return builder;
}

public override void CreateVariableDeclaration(IVariableDeclaration semantics, IxNodeVisitor visitor)
{
if (semantics.IsMemberEligibleForConstructor(SourceBuilder))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,37 @@ public virtual void CreateNamedValueTypeDeclaration(INamedValueTypeDeclaration n

public virtual void CreateVariableDeclaration(IVariableDeclaration semantics, IxNodeVisitor visitor)
{

if (semantics.IsMemberEligibleForConstructor(SourceBuilder))
{
AddToSource($"{semantics.Name}");
semantics.Type.Accept(visitor, this);
AddToSource($"(this, \"\", \"{semantics.Name}\");");
}
switch (semantics.Type)
{
case IArrayTypeDeclaration array:
AddArrayMemberInitialization(array, semantics, visitor);
break;
case IEnumTypeDeclaration @enum:
AddMemberInitialization(@enum, semantics);
break;
case INamedValueTypeDeclaration namedValue:
AddMemberInitialization(namedValue, semantics, visitor);
break;
case IScalarTypeDeclaration scalar:
AddMemberInitialization(scalar, semantics);
break;
case IStringTypeDeclaration @string:
AddMemberInitialization(@string, semantics);
break;
case IClassDeclaration @class:
AddMemberInitialization(@class, semantics, visitor);
break;
case IStructuredTypeDeclaration @struct:
AddMemberInitialization(@struct, semantics, visitor);
break;
}

AddToSource(semantics.SetProperties());
AddToSource(semantics.AddAnnotations(this.SourceBuilder));
}
}

public void CreateEnumTypeDeclaration(IEnumTypeDeclaration enumTypeDeclaration, IxNodeVisitor visitor)
Expand Down Expand Up @@ -214,6 +239,36 @@ public static CsOnlinerConstructorBuilder Create(IxNodeVisitor visitor, IStructu
return builder;
}

public static CsOnlinerConstructorBuilder Create(IxNodeVisitor visitor, IConfigurationDeclaration semantics,
ISourceBuilder sourceBuilder)
{
var builder = new CsOnlinerConstructorBuilder(sourceBuilder);


builder.AddToSource(
$"public {semantics.Name}({typeof(ITwinObject).n()} parent, string readableTail, string symbolTail)");


builder.AddToSource("{");
builder.AddToSource(@$"this.@SymbolTail = symbolTail;
this.@Connector = parent.GetConnector();
this.@Parent = parent;
HumanReadable = {typeof(Connector.Connector).n()}.CreateHumanReadable(parent.HumanReadable, readableTail);
Symbol = {typeof(Connector.Connector).n()}.CreateSymbol(parent.Symbol, symbolTail);");

builder.AddToSource(@$"PreConstruct(parent, readableTail, symbolTail);");

semantics.Variables.ToList().ForEach(p => p.Accept(visitor, builder));
builder.AddToSource("parent.AddChild(this);");
builder.AddToSource("parent.AddKid(this);");

builder.AddToSource(@$"PostConstruct(parent, readableTail, symbolTail);");

builder.AddToSource("}");

return builder;
}

public static CsOnlinerConstructorBuilder Create(IxNodeVisitor visitor, IConfigurationDeclaration semantics,
AXSharpProject project, ISourceBuilder sourceBuilder)
{
Expand All @@ -227,7 +282,7 @@ public static CsOnlinerConstructorBuilder Create(IxNodeVisitor visitor, IConfigu
return builder;
}

private void AddArrayMemberInitialization(IArrayTypeDeclaration type, IFieldDeclaration field,
private void AddArrayMemberInitialization(IArrayTypeDeclaration type, IStorageDeclaration field,
IxNodeVisitor visitor)
{
if(!type.IsMemberEligibleForConstructor(this.SourceBuilder))
Expand Down Expand Up @@ -277,46 +332,46 @@ private void AddArrayMemberInitialization(IArrayTypeDeclaration type, IFieldDecl

}

private void AddMemberInitialization(IClassDeclaration type, IFieldDeclaration field, IxNodeVisitor visitor)
private void AddMemberInitialization(IClassDeclaration type, IStorageDeclaration field, IxNodeVisitor visitor)
{
AddToSource($"{field.Name}");
AddToSource("= new");
type.Accept(visitor, this);
AddToSource($"(this, \"{field.GetAttributeNameValue(field.Name)}\", \"{field.Name}\");");
}

private void AddMemberInitialization(IStructuredTypeDeclaration type, IFieldDeclaration field, IxNodeVisitor visitor)
private void AddMemberInitialization(IStructuredTypeDeclaration type, IStorageDeclaration field, IxNodeVisitor visitor)
{
AddToSource($"{field.Name}");
AddToSource("= new");
type.Accept(visitor, this);
AddToSource($"(this, \"{field.GetAttributeNameValue(field.Name)}\", \"{field.Name}\");");
}

private void AddMemberInitialization(IScalarTypeDeclaration type, IFieldDeclaration field)
private void AddMemberInitialization(IScalarTypeDeclaration type, IStorageDeclaration field)
{
AddToSource($"{field.Name}");
AddToSource($"= @Connector.ConnectorAdapter.AdapterFactory.Create{IecToAdapterExtensions.ToAdapterType(type)}");
AddToSource($"(this, \"{field.GetAttributeNameValue(field.Name)}\", \"{field.Name}\");");
}

private void AddMemberInitialization(IStringTypeDeclaration type, IFieldDeclaration field)
private void AddMemberInitialization(IStringTypeDeclaration type, IStorageDeclaration field)
{
AddToSource($"{field.Name}");
AddToSource($"= @Connector.ConnectorAdapter.AdapterFactory.Create{IecToAdapterExtensions.ToAdapterType(type)}");
AddToSource($"(this, \"{field.GetAttributeNameValue(field.Name)}\", \"{field.Name}\");");
}

// We get warning here about unused method, it is false positive, but we will need to investigate further the object hierarchy.
private void AddMemberInitialization(IEnumTypeDeclaration enumType, IFieldDeclaration field)
private void AddMemberInitialization(IEnumTypeDeclaration enumType, IStorageDeclaration field)
{
AddToSource($"{field.Name}");
AddToSource("= @Connector.ConnectorAdapter.AdapterFactory.CreateINT");
AddToSource($"(this, \"{field.GetAttributeNameValue(field.Name)}\", \"{field.Name}\");");
AddToSource(field.SetProperties());
}

private void AddMemberInitialization(INamedValueTypeDeclaration namedValueType, IFieldDeclaration field,
private void AddMemberInitialization(INamedValueTypeDeclaration namedValueType, IStorageDeclaration field,
IxNodeVisitor visitor)
{
AddToSource($"{field.Name}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,23 @@ public static CsOnlinerMemberBuilder Create(IxNodeVisitor visitor, IStructuredTy
return builder;
}

public static CsOnlinerMemberBuilder Create(IxNodeVisitor visitor, IReadOnlyCollection<IConfigurationDeclaration> semantics,
ISourceBuilder sourceBuilder)
{
var builder = new CsOnlinerMemberBuilder(sourceBuilder);

foreach (var structuredTypeDeclaration in semantics)
{
builder.AddToSource(structuredTypeDeclaration.DeclareProperties());
structuredTypeDeclaration.Variables.ToList().ForEach(p => p.Accept(visitor, builder));
}

builder.AddToSource(@$"partial void PreConstruct(AXSharp.Connector.ITwinObject parent, string readableTail, string symbolTail);
partial void PostConstruct(AXSharp.Connector.ITwinObject parent, string readableTail, string symbolTail);");

return builder;
}

public static CsOnlinerMemberBuilder Create(IxNodeVisitor visitor, IClassDeclaration semantics,
ISourceBuilder sourceBuilder)
{
Expand Down
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.Pragmas.PragmaParser;
using AXSharp.Connector;

namespace AXSharp.Compiler.Cs.Onliner;
Expand All @@ -39,6 +40,31 @@ public CsOnlinerSourceBuilder(AXSharpProject project,
CompilerOptions = project.CompilerOptions;
}

public void CreateMergedConfigurations(IxNodeVisitor visitor, Compilation compilation)
{
var configurations = compilation.GetActiveConfigurations();
if(configurations.Count == 0)
return;

AddToSource("using System;");
AddToSource("using AXSharp.Connector;");
AddToSource("using AXSharp.Connector.ValueTypes;");
AddToSource("using System.Collections.Generic;");
AddToSource("using AXSharp.Connector.Localizations;");
AddToSource("using AXSharp.Abstractions.Presentation;");

TypeCommAccessibility = configurations.First().GetCommAccessibility(this);

AddToSource(
$"public partial class {Project.TargetProject.ProjectRootNamespace}TwinController : ITwinController {{");
AddToSource($"public {typeof(Connector.Connector).n()} Connector {{ get; }}");

AddToSource(CsOnlinerMemberBuilder.Create(visitor, configurations, this).Output);
AddToSource(CsOnlinerConfigurationConstructorBuilder.Create(visitor, configurations, Project, this).Output);
AddToSource("}");

}

/// <inheritdoc />
public Compilation Compilation { get; }

Expand Down Expand Up @@ -188,26 +214,67 @@ public void CreateConfigDeclaration(IConfigDeclarationSyntax configDeclarationSy
IConfigurationDeclaration configurationDeclaration,
IxNodeVisitor visitor)
{
TypeCommAccessibility = eCommAccessibility.None;

AddToSource(
$"public partial class {Project.TargetProject.ProjectRootNamespace}TwinController : ITwinController {{");
AddToSource($"public {typeof(Connector.Connector).n()} Connector {{ get; }}");
AddToSource(CsOnlinerMemberBuilder.Create(visitor, configurationDeclaration, this).Output);
AddToSource(CsOnlinerConfigurationConstructorBuilder
.Create(visitor, configurationDeclaration, Project, this).Output);
AddToSource("}");
/// In order to align with stc v7 where multiple configurations are allowed that are merged at
/// compile time, we need to create a merged configuration class that contains all the configurations.
/// We merge the configuration in <see>CreateMergedConfigurations</see> the entry is called outside visitor in
/// Generate method of the <see>AXSharpProject</see>.

return;
CreateConfigDeclaration(configurationDeclaration, visitor);

//TypeCommAccessibility = eCommAccessibility.None;

//AddToSource(
// $"public partial class {Project.TargetProject.ProjectRootNamespace}TwinController : ITwinController {{");
//AddToSource($"public {typeof(Connector.Connector).n()} Connector {{ get; }}");
//AddToSource(CsOnlinerMemberBuilder.Create(visitor, configurationDeclaration, this).Output);
//AddToSource(CsOnlinerConfigurationConstructorBuilder
// .Create(visitor, configurationDeclaration, Project, this).Output);
//AddToSource("}");
}

/// <inheritdoc />
public void CreateConfigDeclaration(IConfigurationDeclaration configurationDeclaration, IxNodeVisitor visitor)
{
TypeCommAccessibility = eCommAccessibility.None;
// see CreateConfigDeclaration overload comments
return;
TypeCommAccessibility = configurationDeclaration.GetCommAccessibility(this);

AddToSource(configurationDeclaration.Pragmas.AddAttributes());
AddToSource(
$"public partial class {configurationDeclaration.Name}");
AddToSource(":");

AddToSource(typeof(ITwinObject).n()!);

AddToSource("\n{");

AddToSource(CsOnlinerMemberBuilder.Create(visitor, configurationDeclaration, this).Output);

AddToSource(CsOnlinerConstructorBuilder.Create(visitor, configurationDeclaration, this).Output);

//AddToSource(CsOnlinerPlainerOnlineToPlainBuilder.Create(visitor, structuredTypeDeclaration, this).Output);
//AddToSource(CsOnlinerPlainerOnlineToPlainProtectedBuilder.Create(visitor, structuredTypeDeclaration, this).Output);
//AddToSource(CsOnlinerPlainerPlainToOnlineBuilder.Create(visitor, structuredTypeDeclaration, this).Output);

//AddToSource(CsOnlinerPlainerShadowToPlainBuilder.Create(visitor, structuredTypeDeclaration, this).Output);
//AddToSource(CsOnlinerPlainerShadowToPlainProtectedBuilder.Create(visitor, structuredTypeDeclaration, this).Output);
//AddToSource(CsOnlinerPlainerPlainToShadowBuilder.Create(visitor, structuredTypeDeclaration, this).Output);

//AddToSource(CsOnlinerHasChangedBuilder.Create(visitor, structuredTypeDeclaration, this).Output);
//AddPollingMethod(false);

//AddCreatePocoMethod(structuredTypeDeclaration, false);

CreateITwinObjectImplementation();

AddToSource($"public partial class {Project.TargetProject.ProjectRootNamespace} : ITwinController {{");
AddToSource(@$"public {typeof(Connector.Connector).n()} Connector {{ get; }}");
AddToSource(CsOnlinerConstructorBuilder.Create(visitor, configurationDeclaration, Project, this).Output);
AddToSource("}");
//TypeCommAccessibility = eCommAccessibility.None;

//AddToSource($"public partial class {Project.TargetProject.ProjectRootNamespace} : ITwinController {{");
//AddToSource(@$"public {typeof(Connector.Connector).n()} Connector {{ get; }}");
//AddToSource(CsOnlinerConstructorBuilder.Create(visitor, configurationDeclaration, Project, this).Output);
//AddToSource("}");
}

/// <inheritdoc />
Expand Down
Loading
Loading