RomanYankovsky / DelphiAST

Abstract syntax tree builder for Delphi
Mozilla Public License 2.0
271 stars 116 forks source link

Subranges and enums should not store their state in a anName attribute #242

Open JBontes opened 6 years ago

JBontes commented 6 years ago

Because the anName attribute can (and does) come from Lexer.Token. It can be anything. If I create a type named subrange or enum, which is perfectly legal, it will clash with the anName attribute for an actual enum or subrange.

Instead the fact that a type is a subrange or enum should be stored in a anType attribute.

Faulty

procedure TPasSyntaxTreeBuilder.SubrangeType;
begin
  FStack.Push(ntType).SetAttribute(anName, AttributeValues[atSubRange]);
  try
    inherited;
  finally
    FStack.Pop;
  end;
end;

procedure TPasSyntaxTreeBuilder.EnumeratedType;
var
  TypeNode: TSyntaxNode;
begin
  TypeNode := FStack.Push(ntType);
  try
    TypeNode.SetAttribute(anName, AttributeValues[atEnum]);
    if ScopedEnums then
      TypeNode.SetAttribute(anVisibility, 'scoped');
    inherited;
  finally
    FStack.Pop;
  end;
end;

Fix

```Delphi
procedure TPasSyntaxTreeBuilder.SubrangeType;
begin
  FStack.Push(ntType).SetAttribute(anType, AttributeValues[atSubRange]);
  try
    inherited;
  finally
    FStack.Pop;
  end;
end;

procedure TPasSyntaxTreeBuilder.EnumeratedType;
var
  TypeNode: TSyntaxNode;
begin
  TypeNode := FStack.Push(ntType);
  try
    TypeNode.SetAttribute(anType, AttributeValues[atEnum]);
    if ScopedEnums then
      TypeNode.SetAttribute(anVisibility, 'scoped');
    inherited;
  finally
    FStack.Pop;
  end;
end;