Skip to content

Commit

Permalink
WI #2591 Capture storage area for SORT statement (#2592)
Browse files Browse the repository at this point in the history
  • Loading branch information
fm-117 committed Nov 2, 2023
1 parent 6423cb1 commit 27dcb86
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 27 deletions.
5 changes: 3 additions & 2 deletions TypeCobol.Test/Parser/Programs/Cobol85/SortStatement.Mix.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
01 AmbiguousName PIC X.
PROCEDURE DIVISION.
*KO unable to resolve SORT target
Line 21[17,37] <27, Error, Syntax> - Syntax error : Unable to resolve reference to SORT target 'something-not-defined'.
Line 21[17,37] <30, Error, Semantics> - Semantic error: Symbol something-not-defined is not referenced
SORT something-not-defined
*KO ambiguous reference
Line 23[17,29] <27, Error, Syntax> - Syntax error : Ambiguous reference to SORT target 'AmbiguousName'.
Line 23[17,29] <30, Error, Semantics> - Semantic error: Ambiguous reference to symbol AmbiguousName
Symbols found: TCOMFL06::AmbiguousName | TCOMFL06::AmbiguousName
SORT AmbiguousName
*KO SORT file without KEY
Line 25[12,15] <27, Error, Syntax> - Syntax error : SORT file statement requires at least one sorting KEY.
Expand Down
34 changes: 9 additions & 25 deletions TypeCobol/Compiler/Diagnostics/CrossChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,17 @@ public override bool Visit(PerformProcedure performProcedureNode)

public override bool Visit(Sort sort)
{
//TODO Check subscripts !

// Check nature of SORT target
DataDefinition sortTarget = null;
var sortStatement = sort.CodeElement;
if (sortStatement.FileNameOrTableName.SymbolReference != null)
DataDefinition sortTarget = null;
if (sort.StorageAreaReadsDataDefinition != null &&
sort.StorageAreaReadsDataDefinition.TryGetValue(sortStatement.FileNameOrTableName, out sortTarget))
{
CheckSortNature(sortStatement.FileNameOrTableName.SymbolReference);
// SORT target has been resolved, compute SORT nature from its target's CodeElementType
Debug.Assert(sortTarget.CodeElement != null);
sort.Nature = sortTarget.CodeElement.Type == CodeElementType.FileDescriptionEntry ? SortNature.FileSort : SortNature.TableSort;
}

// Check format based on nature of target
Expand All @@ -144,28 +149,6 @@ public override bool Visit(Sort sort)

return true;

void CheckSortNature(SymbolReference tableOrFileReference)
{
var candidateVariables = sort.SymbolTable.GetVariables(tableOrFileReference).ToList(); // Will get the FileDescription as it is also a DataDefinition
switch (candidateVariables.Count)
{
case 0:
// Nothing found
DiagnosticUtils.AddError(sort, $"Unable to resolve reference to SORT target '{tableOrFileReference.Name}'.", tableOrFileReference);
break;
case 1:
// Ok
sortTarget = candidateVariables[0];
Debug.Assert(sortTarget.CodeElement != null);
sort.Nature = sortTarget.CodeElement.Type == CodeElementType.FileDescriptionEntry ? SortNature.FileSort : SortNature.TableSort;
break;
default:
// Ambiguous reference
DiagnosticUtils.AddError(sort, $"Ambiguous reference to SORT target '{tableOrFileReference.Name}'.", tableOrFileReference);
break;
}
}

void CheckFileSort()
{
// File SORT must have at least one sorting KEY (ASCENDING or DESCENDING)
Expand Down Expand Up @@ -1324,6 +1307,7 @@ private static void CheckSubscripts(Node node, DataOrConditionStorageArea dataOr
{
//Those have their own specific subscript checking
case CodeElementType.SearchStatement:
case CodeElementType.SortStatement:
case CodeElementType.ProcedureStyleCall:
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ internal DataOrConditionStorageArea CreateDataItemReferenceOrFileName(CodeElemen
{
storageArea = new DataOrConditionStorageArea(qualifiedDataNameOrFileName, CreateSubscriptExpressions(context.subscript()), _insideFunctionArgument);
}

// Collect storage area read/writes at the code element level
this.storageAreaReads.Add(storageArea);

return storageArea;
}

Expand Down

0 comments on commit 27dcb86

Please sign in to comment.