Skip to content

Semantic differences in for-loop variable declaration #1807

@x-Aksara-x

Description

@x-Aksara-x

Is your bug report related to the C++ slang project or the pyslang Python bindings?
C++ slang

Describe the bug

If loop counter declared in for-loop initialization block the block statement in AST contains counter as member with initializer. As a result, when visiting the AST, information about the counter initial value is obtained before information about the presence of a loop.

To Reproduce

initial begin
  for (int i = 0;;);
end

Additional context
According to the LRM (IEEE 1800-2023, section 12.7.1 The for-loop, the first example in section), these procedure blocks are equivalent:

initial begin
  for (int i = 0; i <= 255; i++)
  ...
end
initial begin
  begin
    automatic int i;
    for (i = 0; i <= 255; i++)
    ...
  end
end

When constructing the AST, the initial value of i in the first case is stored as an initializer of the StatementBlockSymbol member, which appears in the AST before the ForLoopStatement. In the second case, the initial value of i can be obtained only from ForLoopStatement::initializers. The order in which the initial value of the loop counter is obtained affects the data flow analysis in the slang-tidy detector I am developing.

I suggest to uniform AST representation in both cases as it described by the standard.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions