Skip to content

Commit

Permalink
WI TypeCobolTeam#1302 TypeCobolTeam#1295 TypeCobolLinker refactoring T…
Browse files Browse the repository at this point in the history
…ypeCobolTeam#1327 Fix Global scope

TypeCobol refactoring but doesn't change its goals:
- Resolve type reference
- Detect circular reference between typedef
- Register link between type and variable in SymbolTable

TypeCobolLinker is now in CrossCheck phase.
It doesn't check/link dependencies directly but start Type
resolution from the main file.
This allows to fix TypeCobolTeam#1326 and TypeCobolTeam#1338.
It means the loading order of dependencies doesn't affect
the work of TypeCobolLinker.

As a TypeCobolLinker only start its work from the main
file, it means that unused Typedef and procedure in
dependencies won't be resolved anymore.
Type of used procedure will only be resolved,

WI TypeCobolTeam#1295 TypeCobolLinker can now detect circular references
between more than 2 types.
It can also handle circular references between main file
and dependencies.

WI TypeCobolTeam#1327 Fix global but it requires new TypeCobolLinker thats why it's
merged in the same commit.
The correct SymbolTable is now associated with global Node.
  • Loading branch information
osmedile committed Apr 3, 2019
1 parent 9a38793 commit a69b57b
Show file tree
Hide file tree
Showing 15 changed files with 472 additions and 242 deletions.
6 changes: 4 additions & 2 deletions Codegen/src/Nodes/ParameterEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public override IEnumerable<ITextLine> Lines {
//Type exists from Cobol 2002
string typedef = null;
if (this.CodeElement.DataType.CobolLanguageLevel >= TypeCobol.Compiler.CobolLanguageLevel.Cobol2002) {
var type = this.Description?.TypeDefinition ?? this.SymbolTable.GetType(this.CodeElement.DataType).FirstOrDefault();
var type = this.Description?.TypeDefinition;
if (type != null)
{
customtype = type;
Expand Down Expand Up @@ -84,7 +84,9 @@ public override IEnumerable<ITextLine> Lines {

if (picture == null && this.CodeElement.Usage == null && this.CodeElement.DataType.CobolLanguageLevel == Compiler.CobolLanguageLevel.Cobol85)
{//JCM humm... Type without picture lookup enclosing scope.
var type = this.Description?.TypeDefinition ?? this.SymbolTable.GetType(this.CodeElement.DataType).FirstOrDefault();

//TODO seems to be an impossible situation as we check if we are on Compiler.CobolLanguageLevel.Cobol85
var type = this.Description?.TypeDefinition;
if (type != null)
{
customtype = type;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--- Diagnostics ---
Line 19[44,48] <27, Error, Syntax> - Syntax error : PICTURE clause incompatible with TYPE clause OffendingSymbol=[44,48:X(21)]<PictureCharacterString>
Line 19[18,59] <30, Error, Semantics> - Semantic error: TYPE 'rib' is not referenced
Line 19[44,48] <27, Error, Syntax> - Syntax error : PICTURE clause incompatible with TYPE clause OffendingSymbol=[44,48:X(21)]<PictureCharacterString>
Line 23[44,48] <27, Error, Syntax> - Syntax error : PICTURE clause incompatible with TYPE clause OffendingSymbol=[44,48:X(08)]<PictureCharacterString>

--- Program ---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@


*Complex USED Typedef
01 Typedef1 TYPEDEF STRICT.
01 Typedef1 TYPEDEF STRICT global.
05 td-var1 pic X.
05 td-var10.
10 td-var11 pic X.
10 td-var12 pic X.
10 td-var13 type Typedef2.
01 Typedef2 TYPEDEF STRICT.
01 Typedef2 TYPEDEF STRICT global.
05 td-var2 pic X.
05 td-var20.
10 td-var21 pic X.
10 td-var22 pic X.
10 td-var23 type MainProgram::Typedef3.
01 Typedef3 TYPEDEF STRICT.
01 Typedef3 TYPEDEF STRICT global.
05 td-var3 pic X.
05 td-var30.
10 td-var31 pic X.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
 IDENTIFICATION DIVISION.
IDENTIFICATION DIVISION.
PROGRAM-ID. MainProgram.
data division.
working-storage section.
Expand Down Expand Up @@ -35,19 +35,19 @@


*Complex USED Typedef
01 Typedef1 TYPEDEF STRICT.
01 Typedef1 TYPEDEF STRICT global.
05 td-var1 pic X.
05 td-var10.
10 td-var11 pic X.
10 td-var12 pic X.
10 td-var13 type Typedef2.
01 Typedef2 TYPEDEF STRICT.
01 Typedef2 TYPEDEF STRICT global.
05 td-var2 pic X.
05 td-var20.
10 td-var21 pic X.
10 td-var22 pic X.
10 td-var23 type MainProgram::Typedef3.
01 Typedef3 TYPEDEF STRICT.
01 Typedef3 TYPEDEF STRICT global.
05 td-var3 pic X.
05 td-var30.
10 td-var31 pic X.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,13 @@
WORKING-STORAGE SECTION.

01 ThirdType TYPEDEF STRICT.
Line 8[16,22] <30, Error, Semantics> - Semantic error: Type circular reference detected
Line 8[13,35] <30, Error, Semantics> - Semantic error: Type circular reference detected : ThirdType -> myType
05 renjgrn TYPE myType.

01 myType TYPEDEF STRICT.
05 myVar PIC X(10).
05 secondGroup pic X.
Line 13[16,22] <30, Error, Semantics> - Semantic error: Type circular reference detected
Line 13[16,22] <30, Error, Semantics> - Semantic error: Type circular reference detected
Line 13[16,22] <30, Error, Semantics> - Semantic error: Type circular reference detected
Line 13[16,22] <30, Error, Semantics> - Semantic error: Type circular reference detected
Line 13[16,22] <30, Error, Semantics> - Semantic error: Type circular reference detected
05 yhrtger TYPE ThirdType.
Line 14[16,22] <30, Error, Semantics> - Semantic error: Type circular reference detected
Line 14[16,22] <30, Error, Semantics> - Semantic error: Type circular reference detected
Line 14[16,22] <30, Error, Semantics> - Semantic error: Type circular reference detected
Line 14[16,22] <30, Error, Semantics> - Semantic error: Type circular reference detected
Line 14[16,22] <30, Error, Semantics> - Semantic error: Type circular reference detected
05 ezgoerk TYPE MySendType.

01 MyGroup.
Expand All @@ -33,7 +23,7 @@ Line 17[15,20] <30, Error, Semantics> - Semantic error: Variable 'MyVar1' has to
01 MySendType TYPEDEF STRICT.
05 MyVariable PIC X(10).
05 MySecVariable PIC X.
Line 24[16,22] <30, Error, Semantics> - Semantic error: Type circular reference detected
Line 24[13,38] <30, Error, Semantics> - Semantic error: Type circular reference detected : MySendType -> myType
05 SelfRef TYPE myType.


Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
--- Diagnostics ---
Line 8[16,22] <30, Error, Semantics> - Semantic error: Type circular reference detected OffendingSymbol=[16,22:renjgrn]<UserDefinedWord>
Line 13[16,22] <30, Error, Semantics> - Semantic error: Type circular reference detected OffendingSymbol=[16,22:yhrtger]<UserDefinedWord>
Line 13[16,22] <30, Error, Semantics> - Semantic error: Type circular reference detected OffendingSymbol=[16,22:yhrtger]<UserDefinedWord>
Line 13[16,22] <30, Error, Semantics> - Semantic error: Type circular reference detected OffendingSymbol=[16,22:yhrtger]<UserDefinedWord>
Line 13[16,22] <30, Error, Semantics> - Semantic error: Type circular reference detected OffendingSymbol=[16,22:yhrtger]<UserDefinedWord>
Line 13[16,22] <30, Error, Semantics> - Semantic error: Type circular reference detected OffendingSymbol=[16,22:yhrtger]<UserDefinedWord>
Line 14[16,22] <30, Error, Semantics> - Semantic error: Type circular reference detected OffendingSymbol=[16,22:ezgoerk]<UserDefinedWord>
Line 14[16,22] <30, Error, Semantics> - Semantic error: Type circular reference detected OffendingSymbol=[16,22:ezgoerk]<UserDefinedWord>
Line 14[16,22] <30, Error, Semantics> - Semantic error: Type circular reference detected OffendingSymbol=[16,22:ezgoerk]<UserDefinedWord>
Line 14[16,22] <30, Error, Semantics> - Semantic error: Type circular reference detected OffendingSymbol=[16,22:ezgoerk]<UserDefinedWord>
Line 14[16,22] <30, Error, Semantics> - Semantic error: Type circular reference detected OffendingSymbol=[16,22:ezgoerk]<UserDefinedWord>
Line 8[13,35] <30, Error, Semantics> - Semantic error: Type circular reference detected : ThirdType -> myType
Line 17[15,20] <30, Error, Semantics> - Semantic error: Variable 'MyVar1' has to be limited to level 47 because of 'myType' maximum estimated children level OffendingSymbol=[15,20:MyVar1]<UserDefinedWord>
Line 24[16,22] <30, Error, Semantics> - Semantic error: Type circular reference detected OffendingSymbol=[16,22:SelfRef]<UserDefinedWord>
Line 24[13,38] <30, Error, Semantics> - Semantic error: Type circular reference detected : MySendType -> myType

--- Program ---
PROGRAM: CircularRefCheck common:False initial:False recursive:False
Expand Down
16 changes: 10 additions & 6 deletions TypeCobol/Compiler/CodeModel/SymbolTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,8 @@ void AddVariableUnderTypeDefinition([NotNull] DataDefinition data)
//TypeDefinition must NOT be added to DataTypeEntries, only its children
Debug.Assert(!(data is TypeDefinition));

//Types are declared in the Declarations SymbolTable
var table = GetTableFromScope(Scope.Declarations);

//Add symbol to the dictionary
Add(table.DataTypeEntries, data);
Add(this.DataTypeEntries, data);
}

public IEnumerable<DataDefinition> GetVariables(SymbolReference symbolReference)
Expand Down Expand Up @@ -686,6 +683,13 @@ private static void GetVariablesUnderTypeDefForPublicType(SymbolTable symbolTabl
if (temp != null) {
resultDataDefinitions.AddRange(temp);
}

//Stop when we reach the first Global scope, because otherwise a nested program can end up searching in it the global scope of its parent
//Global is the most upper scope that can contains Typedef (GlobalStorage cannot contains typedef)
if (currSymbolTable.CurrentScope == Scope.Global)
{
break;
}
currSymbolTable = currSymbolTable.EnclosingScope;
}

Expand Down Expand Up @@ -867,7 +871,7 @@ public void AddDataDefinitionsUnderType([NotNull] DataDefinition data)
}
}

public IList<TypeDefinition> EmptyTypeDefinitionList = new List<TypeDefinition>();
public List<TypeDefinition> EmptyTypeDefinitionList = new List<TypeDefinition>();
[NotNull]
public IList<TypeDefinition> GetType(DataDefinition symbol)
{
Expand All @@ -882,7 +886,7 @@ public List<TypeDefinition> GetType(SymbolReference symbolReference)


[NotNull]
public IList<TypeDefinition> GetType(DataType dataType, string pgmName = null)
public List<TypeDefinition> GetType(DataType dataType, string pgmName = null)
{
if (dataType.CobolLanguageLevel == CobolLanguageLevel.Cobol85)
{
Expand Down
12 changes: 9 additions & 3 deletions TypeCobol/Compiler/CompilationUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public void RefreshProgramClassDocumentSnapshot()
SourceFile root = temporarySnapshot.Root;
List<Diagnostic> diagnostics = new List<Diagnostic>();
Dictionary<CodeElement, Node> nodeCodeElementLinkers = temporarySnapshot.NodeCodeElementLinkers ?? new Dictionary<CodeElement, Node>();
ProgramClassParserStep.CrossCheckPrograms(root);
ProgramClassParserStep.CrossCheckPrograms(root, temporarySnapshot);

// Capture the result of the parse in a new snapshot
ProgramClassDocumentSnapshot = new ProgramClassDocument(
Expand Down Expand Up @@ -221,11 +221,17 @@ public void ProduceTemporarySemanticDocument()
List<Diagnostic> newDiagnostics;
Dictionary<CodeElement, Node> nodeCodeElementLinkers = new Dictionary<CodeElement, Node>();

List<DataDefinition> typedVariablesOutsideTypedef = new List<DataDefinition>();
List<TypeDefinition> typeThatNeedTypeLinking = new List<TypeDefinition>();

//TODO cast to ImmutableList<CodeElementsLine> sometimes fails here
ProgramClassParserStep.CupParseProgramOrClass(TextSourceInfo, ((ImmutableList<CodeElementsLine>)codeElementsDocument.Lines), CompilerOptions, CustomSymbols, perfStatsForParserInvocation, out root, out newDiagnostics, out nodeCodeElementLinkers);
ProgramClassParserStep.CupParseProgramOrClass(TextSourceInfo, ((ImmutableList<CodeElementsLine>)codeElementsDocument.Lines), CompilerOptions, CustomSymbols, perfStatsForParserInvocation, out root, out newDiagnostics, out nodeCodeElementLinkers,
out typedVariablesOutsideTypedef,
out typeThatNeedTypeLinking);

// Capture the produced results
TemporaryProgramClassDocumentSnapshot = new TemporarySemanticDocument(codeElementsDocument, new DocumentVersion<ICodeElementsLine>(this), codeElementsDocument.Lines, root, newDiagnostics, nodeCodeElementLinkers);
TemporaryProgramClassDocumentSnapshot = new TemporarySemanticDocument(codeElementsDocument, new DocumentVersion<ICodeElementsLine>(this), codeElementsDocument.Lines, root, newDiagnostics, nodeCodeElementLinkers,
typedVariablesOutsideTypedef, typeThatNeedTypeLinking);

// Stop perf measurement
PerfStatsForTemporarySemantic.OnStopRefreshParsingStep();
Expand Down
Loading

0 comments on commit a69b57b

Please sign in to comment.