Skip to content

Commit f859223

Browse files
committed
Enhance metadata generation and update project settings
Refactored `GenerateMetadata` to accept `sources` for filtering types based on their location in project source files. Added support for generating metadata for methods in classes and method prototypes in interfaces using new helper methods `CreateMetaMethod` and `CreateMetaMethodPrototype`. Commented out diagnostic logging for compilation errors, providing a future debugging mechanism. Updated `launchSettings.json` by adding the `rangarok` profile, removing `tiax-rooted`, and making minor formatting changes. Improved code readability with formatting adjustments and broader type filtering in `GenerateMetadata`. Commented out `TargetProject.ProvisionProjectStructure` and updated method calls to reflect new parameters.
1 parent 0271d6d commit f859223

2 files changed

Lines changed: 105 additions & 8 deletions

File tree

src/AXSharp.compiler/src/AXSharp.Compiler/AXSharpProject.cs

Lines changed: 99 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,15 @@ public void Generate()
112112

113113
var compilationResult = Compilation.Create(toCompile, new List<ISemanticAnalyzer>(), Compilation.Settings.Default).Result;
114114

115-
115+
116+
//compilationResult.Compilation.GetDiagnostics()
117+
// .Where(d => d.Severity == AX.Text.Diagnostics.DiagnosticSeverity.Error)
118+
// .ToList()
119+
// .ForEach(d =>
120+
// {
121+
// Log.Logger.Error(d.ToString());
122+
// });
123+
116124
this.CleanOutput(this.OutputFolder);
117125

118126
foreach (var origin in projectSources)
@@ -194,7 +202,7 @@ public void Generate()
194202
}
195203

196204
//TargetProject.ProvisionProjectStructure();
197-
GenerateMetadata(compilationResult.Compilation);
205+
GenerateMetadata(compilationResult.Compilation, projectSources);
198206
TargetProject.GenerateResources();
199207
TargetProject.GenerateCompanionData();
200208
Log.Logger.Information($"Compilation of project '{AxProject.SrcFolder}' done.");
@@ -368,9 +376,18 @@ private string GetApaxFolderFromProjectReference(IReference projectReference)
368376
return string.Empty;
369377
}
370378

371-
private void GenerateMetadata(Compilation compilation)
379+
private void GenerateMetadata(Compilation compilation, IEnumerable<(ISyntaxTree parseTree, SourceFileText source)> sources)
372380
{
373-
var meta = compilation.GetSemanticTree().Types.Where(p => p.AccessModifier == AccessModifier.Public)
381+
var projectSourceFiles = sources.Select(s => s.source.Filename).ToHashSet();
382+
383+
var meta = compilation.GetSemanticTree().Types
384+
.Where(p => true)//p.AccessModifier == AccessModifier.Public | p.AccessModifier == AccessModifier.Internal)
385+
.Where(p => {
386+
var typeLocation = p.Location;
387+
if (typeLocation == null) return false;
388+
var lineSpan = typeLocation.GetLineSpan();
389+
return projectSourceFiles.Contains(lineSpan.Filename);
390+
})
374391
.Select(p => CreateMetaType(p));
375392

376393
using (var swr = new StreamWriter(Path.Combine(EnsureFolder(TargetProject.GetMetaDataFolder), "meta.json")))
@@ -417,13 +434,29 @@ private static string CreateMetaType(ITypeDeclaration type)
417434
sb.Append($"TYPE {type.Name} : STRUCT ; END_STRUCT\n");
418435
break;
419436
case DeclarationKind.Class:
420-
sb.Append($"CLASS {type.Name} \nEND_CLASS\n");
437+
sb.Append($"CLASS {type.Name}\n");
438+
if (type is IClassDeclaration classDecl)
439+
{
440+
foreach (var method in classDecl.Methods)
441+
{
442+
sb.Append(CreateMetaMethod(method));
443+
}
444+
}
445+
sb.Append("END_CLASS\n");
421446
break;
422447
case DeclarationKind.Enumeration:
423448
sb.Append($"TYPE {type.Name} : (item0); \nEND_TYPE\n");
424449
break;
425450
case DeclarationKind.Interface:
426-
sb.Append($"INTERFACE {type.Name} \nEND_INTERFACE\n");
451+
sb.Append($"INTERFACE {type.Name}\n");
452+
if (type is IInterfaceDeclaration interfaceDecl)
453+
{
454+
foreach (var method in interfaceDecl.Methods)
455+
{
456+
sb.Append(CreateMetaMethodPrototype(method));
457+
}
458+
}
459+
sb.Append("END_INTERFACE\n");
427460
break;
428461
case DeclarationKind.NamedValueType:
429462
sb.Append($"TYPE {type.Name} : INT (item0 := 0); \nEND_TYPE\n");
@@ -434,4 +467,64 @@ private static string CreateMetaType(ITypeDeclaration type)
434467

435468
return sb.ToString();
436469
}
470+
471+
private static string CreateMetaMethod(IMethodDeclaration method)
472+
{
473+
var sb = new StringBuilder();
474+
475+
// Get return type
476+
var returnVariable = method.Variables.FirstOrDefault(v => v.Section == Section.Return);
477+
var returnType = returnVariable != null ? $" : {returnVariable.Type.Name}" : string.Empty;
478+
479+
// Build method signature with access modifier
480+
var accessModifier = method.AccessModifier.ToString().ToUpperInvariant();
481+
482+
sb.Append($"METHOD {accessModifier} {method.Name}{returnType}\n");
483+
484+
// Add VAR_INPUT section if there are input parameters
485+
var inputVars = method.Variables.Where(v => v.Section == Section.Input).ToList();
486+
if (inputVars.Any())
487+
{
488+
sb.Append("VAR_INPUT\n");
489+
foreach (var inputVar in inputVars)
490+
{
491+
sb.Append($"{inputVar.Name} : {inputVar.Type.Name};\n");
492+
}
493+
sb.Append("END_VAR\n");
494+
}
495+
496+
// Add at least one statement (semicolon) to method body
497+
sb.Append(";\n");
498+
499+
sb.Append("END_METHOD\n");
500+
501+
return sb.ToString();
502+
}
503+
504+
private static string CreateMetaMethodPrototype(IMethodPrototypeDeclaration method)
505+
{
506+
var sb = new StringBuilder();
507+
508+
// Get return type
509+
var returnVariable = method.Variables.FirstOrDefault(v => v.Section == Section.Return);
510+
var returnType = returnVariable != null ? $" : {returnVariable.Type.Name}" : string.Empty;
511+
512+
sb.Append($"METHOD {method.Name}{returnType}\n");
513+
514+
// Add VAR_INPUT section if there are input parameters
515+
var inputVars = method.Variables.Where(v => v.Section == Section.Input).ToList();
516+
if (inputVars.Any())
517+
{
518+
sb.Append("VAR_INPUT\n");
519+
foreach (var inputVar in inputVars)
520+
{
521+
sb.Append($"{inputVar.Name} : {inputVar.Type.Name};\n");
522+
}
523+
sb.Append("END_VAR\n");
524+
}
525+
526+
sb.Append("END_METHOD\n");
527+
528+
return sb.ToString();
529+
}
437530
}

src/AXSharp.compiler/src/ixc/Properties/launchSettings.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"ixc-tom": {
88
"commandName": "Project",
99
"workingDirectory": "D:\\github\\Inxton\\axopen\\src\\components.elements\\app"
10-
},
10+
},
1111
"integration_plc": {
1212
"commandName": "Project",
1313
"workingDirectory": "c:\\W\\Develop\\gh\\inxton\\axopen\\src\\integrations\\ctrl\\"
@@ -51,11 +51,15 @@
5151
"commandLineArgs": "--source-project-folder C:\\W\\Develop\\gh\\inxton\\ax\\TiaAxTools\\Sandbox\\src\\Connectors\\plc1\\ax --output-project-folder C:\\W\\Develop\\gh\\inxton\\ax\\TiaAxTools\\Sandbox\\src\\Connectors\\plc1\\ix --project-file plc1.csproj --target-platform-moniker tia",
5252
"workingDirectory": "C:\\W\\Develop\\gh\\inxton\\ax\\TiaAxTools\\Sandbox\\"
5353
},
54-
5554
"tiax-rooted": {
5655
"commandName": "Project",
5756
"commandLineArgs": "-v Verbose",
5857
"workingDirectory": "C:\\W\\Develop\\gh\\inxton\\ax\\TiaAxTools\\Sandbox\\src\\Connectors\\plc1\\ax"
58+
},
59+
"rangarok": {
60+
"commandName": "Project",
61+
"commandLineArgs": "--verbosity Information",
62+
"workingDirectory": "c:\\W\\Develop\\gh\\inxton\\simatic-ax\\RANGAROK\\ST20\\ax\\"
5963
}
6064
}
6165
}

0 commit comments

Comments
 (0)