Skip to content

Commit

Permalink
WI TypeCobolTeam#1301 SymbolTable: Limit search of symbol to a max scope
Browse files Browse the repository at this point in the history
Search for paragraphs/sections are limited to current SymbolTable.
Search for variables are limited to scope GlobalStorage.
Search for functions/types in all scopes.
  • Loading branch information
osmedile committed Mar 10, 2019
1 parent 4e3b346 commit 6c808fe
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions TypeCobol/Compiler/CodeModel/SymbolTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Linq.Expressions;
using System.Text.RegularExpressions;
using Castle.Core.Internal;
using TypeCobol.Compiler.Concurrency;

namespace TypeCobol.Compiler.CodeModel
{
Expand Down Expand Up @@ -82,14 +83,13 @@ public SymbolTable(SymbolTable enclosing, Scope current)
}

private List<T> GetFromTableAndEnclosing<T>(string head,
Func<SymbolTable, IDictionary<string, List<T>>> getTableFunction, SymbolTable symbolTable = null) where T : Node
Func<SymbolTable, IDictionary<string, List<T>>> getTableFunction, Scope maxScope = Scope.Intrinsic) where T : Node
{
symbolTable = symbolTable ?? this;
var table = getTableFunction.Invoke(symbolTable);
var table = getTableFunction.Invoke(this);
var values = GetFromTable(head, table);
if (EnclosingScope != null)
if (EnclosingScope != null && EnclosingScope.CurrentScope >= maxScope)
{
values.AddRange(EnclosingScope.GetFromTableAndEnclosing(head, getTableFunction));
values.AddRange(EnclosingScope.GetFromTableAndEnclosing(head, getTableFunction, maxScope));
}
return values;
}
Expand Down Expand Up @@ -194,7 +194,7 @@ public IEnumerable<DataDefinition> GetVariables(SymbolReference symbolReference)
private IList<DataDefinition> GetVariables(string name)
{
//Try to get variable in the current program
var found = GetFromTableAndEnclosing(name, GetDataDefinitionTable);
var found = GetFromTableAndEnclosing(name, GetDataDefinitionTable, Scope.GlobalStorage);

return found;
}
Expand Down Expand Up @@ -512,12 +512,12 @@ public void MatchVariable(IList<DataDefinition> found, DataDefinition headDataDe
var dataType = GetAllEnclosingTypeReferences().FirstOrDefault(k => k.Key == currentTypeDef); //Let's get typereferences (built by TypeCobolLinker phase)
if (dataType.Key == null || dataType.Value == null)
return;
var references = dataType.Value;
IEnumerable<DataDefinition> references = dataType.Value;

//If typedefcontext is set : Ignore references of this typedefContext to avoid loop seeking
// Only takes variable references that are declared inside the typeDefContext
if (typeDefContext != null)
references = references.Where(r => r.DataType != typeDefContext.DataType && r.ParentTypeDefinition == typeDefContext).ToList();
references = references.Where(r => r.DataType != typeDefContext.DataType && r.ParentTypeDefinition == typeDefContext);

var primaryPath = completeQualifiedNames.Last().ToArray(); //PrmiaryPath that will be added in front of every reference's path found
foreach (var reference in references)
Expand Down Expand Up @@ -557,11 +557,11 @@ private void AddAllReference(IList<DataDefinition> found, DataDefinition heaData
var dataType = GetAllEnclosingTypeReferences().FirstOrDefault(k => k.Key == currentDataDefinition);
if (dataType.Key == null || dataType.Value == null)
return;
var references = dataType.Value;
IEnumerable<DataDefinition> references = dataType.Value;
//If typedefcontext is setted : Ignore references of this typedefContext to avoid loop seeking
// + Only takes variable references that are declared inside the typeDefContext
if (typeDefContext != null)
references = references.Where(r => r.DataType != typeDefContext.DataType && r.ParentTypeDefinition == typeDefContext).ToList();
references = references.Where(r => r.DataType != typeDefContext.DataType && r.ParentTypeDefinition == typeDefContext);
var typePath = completeQualifiedNames.Last().ToArray();
var referenceCounter = 0;
foreach (var reference in references)
Expand Down Expand Up @@ -741,14 +741,19 @@ private Node GetAncestor(Node node, int generation)
private IDictionary<string, List<Section>> Sections =
new Dictionary<string, List<Section>>(StringComparer.OrdinalIgnoreCase);

private static IList<Section> EmptySectionList = new ImmutableList<Section>();

internal void AddSection(Section section)
{
Add(Sections, section);
}

public IList<Section> GetSection(string name)
{
return GetFromTableAndEnclosing(name, GetSectionTable);
Sections.TryGetValue(name, out var values);
if (values != null) return values.ToList(); //.ToList so the caller cannot modify our stored list

return EmptySectionList;
}

private IDictionary<string, List<Section>> GetSectionTable(SymbolTable symbolTable)
Expand All @@ -763,14 +768,19 @@ private IDictionary<string, List<Section>> GetSectionTable(SymbolTable symbolTab
private IDictionary<string, List<Paragraph>> Paragraphs =
new Dictionary<string, List<Paragraph>>(StringComparer.OrdinalIgnoreCase);

private static IList<Paragraph> EmptyParagraphList = new ImmutableList<Paragraph>();

internal void AddParagraph(Paragraph paragraph)
{
Add(Paragraphs, paragraph);
}

public IList<Paragraph> GetParagraph(string name)
{
return GetFromTableAndEnclosing(name, GetParagraphTable);
Paragraphs.TryGetValue(name, out var values);
if (values != null) return values.ToList(); //.ToList so the caller cannot modify our stored list

return EmptyParagraphList;
}

private IDictionary<string, List<Paragraph>> GetParagraphTable(SymbolTable symbolTable)
Expand Down Expand Up @@ -1127,7 +1137,7 @@ public void AddProgram(Program program)
[NotNull]
private List<Program> GetProgram(string name)
{
return GetFromTableAndEnclosing(name, GetProgramsTable);
return GetFromTableAndEnclosing(name, GetProgramsTable, Scope.Namespace);
}

private IDictionary<string, List<Program>> GetProgramsTable(SymbolTable symbolTable)
Expand Down

0 comments on commit 6c808fe

Please sign in to comment.