Skip to content

Commit 47d57f4

Browse files
authored
Merge pull request #390 from Inxton/revert-192-159-_POSSIBLE-BUG_Compilation_object_loses_semantic_information_about_type_when_more_than_libraries_are_imported
Revert "[POSSIBLE-BUG] Compilation object loses semantic information about type when more than libraries are imported"
2 parents 0cf6399 + 380499e commit 47d57f4

5 files changed

Lines changed: 25 additions & 144 deletions

File tree

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

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -102,28 +102,17 @@ public void Generate()
102102
{
103103
Log.Logger.Information($"Compilation of project '{AxProject.SrcFolder}' started");
104104

105-
IEnumerable<(ISyntaxTree parseTree, SourceFileText source)> projectSources = AxProject.Sources.Select(p => (parseTree: STParser.ParseTextAsync(p).Result, source: p));
105+
var projectSources = AxProject.Sources.Select(p => (parseTree: STParser.ParseTextAsync(p).Result, source: p));
106106

107107
TargetProject.ProvisionProjectStructure();
108108

109-
var refParseTrees = GetReferences().DistinctBy(p => p.GetText());
109+
var refParseTrees = GetReferences();
110110

111111

112112
var toCompile = refParseTrees.Concat(projectSources.Select(p => p.parseTree));
113113

114-
115-
//var packageProvider = AX.Package.DefaultPackageProvider.Empty as DefaultPackageProvider;
116-
117-
//var compilationResult =
118-
// Compilation.Create(projectSources.Select(p => p.parseTree),
119-
// new List<ISemanticAnalyzer>(),
120-
// new Compilation.Settings(packageProvider, FeatureFlags.None, SemanticOptions.CompileToLib)).Result;
121-
122114
var compilationResult = Compilation.Create(toCompile, new List<ISemanticAnalyzer>(), Compilation.Settings.Default).Result;
123115

124-
compilationResult.Compilation.GetDiagnostics().Where(o => o.Severity == AX.Text.Diagnostics.DiagnosticSeverity.Error).ToList()
125-
.ForEach(p => Log.Logger.Information($"{p.Severity} : {p.Location} : {p.Message.ToString()}"));
126-
127116
this.CleanOutput(this.OutputFolder);
128117

129118
foreach (var origin in projectSources)
@@ -416,12 +405,12 @@ private static string CreateMetaType(ITypeDeclaration type)
416405
var kind = type.Kind;
417406
var sb = new StringBuilder();
418407

419-
408+
420409
var hasNamespace = type.ContainingNamespace != null;
421410
if (hasNamespace) sb.Append($"NAMESPACE {type.ContainingNamespace!.FullyQualifiedName}\n\n");
422-
411+
423412
type?.Pragmas?.ToList().ForEach(p => sb.Append($"{{{p.Content}}}\n"));
424-
413+
425414
switch (kind)
426415
{
427416
case DeclarationKind.Struct:

src/AXSharp.compiler/src/AXSharp.Cs.Compiler/Helpers/SemanticsHelpers.cs

Lines changed: 6 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -34,69 +34,6 @@ public static bool IsMemberEligibleForTranspile(this IFieldDeclaration field, IS
3434
}
3535

3636

37-
/// <summary>
38-
/// Finds type declaration.
39-
/// </summary>
40-
/// <param name="compilation">Compilation object</param>
41-
/// <param name="typeAccess">Required type</param>
42-
/// <returns>Required type if found.</returns>
43-
public static ITypeDeclaration FindTypeDeclaration(this Compilation compilation, AX.ST.Semantic.Model.ISemanticTypeAccess? typeAccess)
44-
{
45-
// This is to resolve fully qualified type name when the type cannot be determined propeprly form the semantic tree.
46-
// TODO: This workaround should be removed once we can properly use project dependencies in the stc.
47-
if (typeAccess == null)
48-
{
49-
return null;
50-
}
51-
52-
var fullyQualified = compilation.GetSemanticTree().Types.DistinctBy(p => p.FullyQualifiedName).Where(p => p.FullyQualifiedName == typeAccess.Type.FullyQualifiedName).FirstOrDefault();
53-
54-
if (fullyQualified != null)
55-
{
56-
return fullyQualified;
57-
}
58-
59-
var candidates = compilation.GetSemanticTree().Types.DistinctBy(p => p.FullyQualifiedName).Where(p => p.Name == typeAccess.TypeSymbol.Name);
60-
if (candidates.Count() == 1)
61-
{
62-
return candidates.First();
63-
}
64-
65-
if (candidates.Count() == 0)
66-
{
67-
Log.Logger.Warning($"Type '{typeAccess?.ToString()}' not found in the semantic tree.");
68-
}
69-
70-
if (candidates.Count() > 1)
71-
{
72-
Log.Logger.Warning($"Multiple types found for '{typeAccess?.ToString()}' in the semantic tree. You may need to fully qualify the declaration.");
73-
}
74-
75-
return null;
76-
}
77-
78-
/// <summary>
79-
/// Determines fully qualified name of the declaration.
80-
/// </summary>
81-
/// <param name="declaration">Field declaration</param>
82-
/// <param name="compilation">Compilation object.</param>
83-
/// <returns>Fully qualified name of the declaration.</returns>
84-
public static string? DetermineFullyQualifiedName(this IFieldDeclaration declaration, Compilation compilation)
85-
{
86-
return compilation.FindTypeDeclaration(declaration.TypeAccess)?.FullyQualifiedName;
87-
}
88-
89-
/// <summary>
90-
/// Determines fully qualified name of the declaration.
91-
/// </summary>
92-
/// <param name="declaration">Variable declaration</param>
93-
/// <param name="compilation">Compilation object.</param>
94-
/// <returns>Fully qualified name of the declaration.</returns>
95-
public static string? DetermineFullyQualifiedName(this IVariableDeclaration declaration, Compilation compilation)
96-
{
97-
return compilation.FindTypeDeclaration(declaration.TypeAccess)?.FullyQualifiedName;
98-
}
99-
10037
private static bool IsToBeOmitted(this IStorageDeclaration fieldDeclaration, ISourceBuilder sourceBuilder, string coBuilder)
10138
{
10239

@@ -152,7 +89,8 @@ public static bool IsEligibleForTranspile(this IFieldDeclaration fieldDeclaratio
15289
type is IStringTypeDeclaration ||
15390
type is IStructuredTypeDeclaration ||
15491
type is INamedValueTypeDeclaration ||
155-
sourceBuilder.Compilation.FindTypeDeclaration(fieldDeclaration.TypeAccess) != null);
92+
sourceBuilder.Compilation.GetSemanticTree().Types.Any(p =>
93+
p.FullyQualifiedName == type.FullyQualifiedName));
15694

15795
if(!isEligible)
15896
{
@@ -179,7 +117,8 @@ public static bool IsEligibleForTranspile(this IVariableDeclaration variableDecl
179117
type is IStringTypeDeclaration ||
180118
type is IStructuredTypeDeclaration ||
181119
type is INamedValueTypeDeclaration ||
182-
sourceBuilder.Compilation.FindTypeDeclaration(variableDeclaration.TypeAccess) != null);
120+
sourceBuilder.Compilation.GetSemanticTree().Types.Any(p =>
121+
p.FullyQualifiedName == type.FullyQualifiedName));
183122

184123
if (!isEligible)
185124
{
@@ -208,7 +147,8 @@ public static bool IsEligibleForTranspile(this IArrayTypeDeclaration arrayTypeDe
208147
arrayTypeDeclaration.ElementTypeAccess.Type is IStringTypeDeclaration ||
209148
arrayTypeDeclaration.ElementTypeAccess.Type is IStructuredTypeDeclaration ||
210149
arrayTypeDeclaration.ElementTypeAccess.Type is INamedValueTypeDeclaration ||
211-
sourceBuilder.Compilation.FindTypeDeclaration(arrayTypeDeclaration.ElementTypeAccess) != null);
150+
sourceBuilder.Compilation.GetSemanticTree().Types.Any(p =>
151+
p.FullyQualifiedName == arrayTypeDeclaration.ElementTypeAccess.Type.FullyQualifiedName));
212152

213153
return isEligibleType && singleDimensionalArray;
214154

src/AXSharp.compiler/src/AXSharp.Cs.Compiler/Onliner/CsOnlinerMemberBuilder.cs

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -80,35 +80,11 @@ public void CreateFieldDeclaration(IFieldDeclaration fieldDeclaration, IxNodeVis
8080
AddToSource("{get;}");
8181
}
8282
break;
83-
case UndefinedTypeDeclaration undef:
84-
break;
8583
default:
86-
// This is a workaround for ambiguous types.
87-
// We do not require the stc build to be able to resolve the type when not fully qualified.
88-
// STC compilation here may produce errors wich are ignored at this point. This may result in ambiguous types
89-
// wich resolved here.
90-
// TODO: Once we are able to correctly load dependencies for the stc compilation we should remove this workaround.
91-
if (fieldDeclaration.Type.Kind == DeclarationKind.Ambiguous)
92-
{
93-
var fqn = fieldDeclaration.DetermineFullyQualifiedName(this.SourceBuilder.Compilation);
94-
95-
if (fqn != null)
96-
{
97-
AddToSource($"{fieldDeclaration.AccessModifier.Transform()} ");
98-
AddToSource($"{fqn} ");
99-
AddToSource($" {fieldDeclaration.Name}");
100-
AddToSource("{get;}");
101-
}
102-
}
103-
else
104-
{
105-
AddToSource($"{fieldDeclaration.AccessModifier.Transform()} ");
106-
fieldDeclaration.Type.Accept(visitor, this);
107-
AddToSource($" {fieldDeclaration.Name}");
108-
AddToSource("{get;}");
109-
}
110-
111-
84+
AddToSource($"{fieldDeclaration.AccessModifier.Transform()} ");
85+
fieldDeclaration.Type.Accept(visitor, this);
86+
AddToSource($" {fieldDeclaration.Name}");
87+
AddToSource("{get;}");
11288
break;
11389
}
11490
}
@@ -194,31 +170,10 @@ public void CreateVariableDeclaration(IVariableDeclaration semantics, IxNodeVisi
194170
}
195171
break;
196172
default:
197-
// This is a workaround for ambiguous types.
198-
// We do not require the stc build to be able to resolve the type when not fully qualified.
199-
// STC compilation here may produce errors wich are ignored at this point. This may result in ambiguous types
200-
// wich resolved here.
201-
// TODO: Once we are able to correctly load dependencies for the stc compilation we should remove this workaround.
202-
if (semantics.Type.Kind == DeclarationKind.Ambiguous)
203-
{
204-
var fqn = semantics.DetermineFullyQualifiedName(this.SourceBuilder.Compilation);
205-
206-
if (fqn != null)
207-
{
208-
AddToSource($"public");
209-
AddToSource($"{fqn} ");
210-
AddToSource($" {semantics.Name}");
211-
AddToSource("{get;}");
212-
}
213-
}
214-
else
215-
{
216-
AddToSource($"public");
217-
semantics.Type.Accept(visitor, this);
218-
AddToSource($" {semantics.Name}");
219-
AddToSource("{get;}");
220-
}
221-
173+
AddToSource($"public");
174+
semantics.Type.Accept(visitor, this);
175+
AddToSource($" {semantics.Name}");
176+
AddToSource("{get;}");
222177
break;
223178
}
224179
}

src/AXSharp.compiler/src/AXSharp.Cs.Compiler/Onliner/CsOnlinerSourceBuilder.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,11 @@ public void CreateClassDeclaration(IClassDeclarationSyntax classDeclarationSynta
156156
AddToSource(":");
157157

158158
var isExtended = false;
159-
AX.ST.Semantic.Model.ISemanticTypeAccess? extendedType = classDeclaration.ExtendedTypeAccesses.FirstOrDefault();
160-
161-
//TODO: Workaround for not fully qualified declarations. To be addressed with proper dependency handling in stc.
162-
var extend = Compilation.FindTypeDeclaration(extendedType);
163-
if (extend != null)
164-
{
165-
AddToSource($"{extend.FullyQualifiedName}{ReplaceGenericSignature(classDeclaration)}");
159+
var extendedType = classDeclaration.ExtendedTypeAccesses.FirstOrDefault();
160+
if (Compilation.GetSemanticTree().Types
161+
.Any(p => p.FullyQualifiedName == extendedType?.Type.FullyQualifiedName))
162+
{
163+
AddToSource($"{extendedType.Type.FullyQualifiedName}{ReplaceGenericSignature(classDeclaration)}");
166164
isExtended = true;
167165
}
168166
else

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
},
1111
"ixc-simple-template": {
1212
"commandName": "Project",
13-
"commandLineArgs": "--skip-deps",
14-
"workingDirectory": "C:\\W\\Develop\\gh\\inxton\\simatic-ax\\axopen.templates\\axopen.template.simple\\ax"
13+
"workingDirectory": "c:\\W\\Develop\\gh\\inxton\\_axopen\\axopen.templates\\axopen.template.simple\\ax"
1514
},
1615
"ixc-template-ref": {
1716
"commandName": "Project",

0 commit comments

Comments
 (0)