Mantis 2163

Clarify hierarchical scopes created by for and foreach loops

In Section 12.7.1

CHANGE

The variables used to control a for-loop can also be declared within the loop, as part of the for_initialization assignments. This creates a local variable within the loop. Other parallel loops cannot inadvertently affect the loop control variable. For example:

 

  module foo;

 

    initial begin

      for (int i = 0; i <= 255; i++)

        ...

    end

 

    initial begin

      loop2: for (int i = 15; i >= 0; i--)

        ...

    end

endmodule

 

The local variable declared within a for loop is equivalent to declaring an automatic variable in an unnamed block.

 

 

TO

 

The variables used to control a for-loop can also be declared within the loop, as part of the for_initialization assignments. This creates an implicit begin-end block around the loop, containing declaration of the variables with automatic lifetime. This block creates a new hierarchical scope, making the variables local to the loop scope. The block is unnamed by default, but can be named by adding a statement label (9.3.5) to the for-loop statement. This creates a local variable within the loop. Thus, oOther parallel loops cannot inadvertently affect the loop control variable. For example:

 

  module foo;

 

    initial begin                         

      for (int i = 0; i <= 255; i++)

        ...

    end

 

    initial begin

      loop2: for (int i = 15; i >= 0; i--)

        ...

    end

endmodule

 

This is equivalent to the following:

 

  module foo;

 

    initial begin

      begin

        int i;

        for (i = 0; i <= 255; i++)

          ...

      end

    end

 

    initial begin

      begin : loop2

        int i;

        for (i = 15; i >= 0; i--)

          ...

      end

    end

endmodule

 

Only for-loop statements containing variable declarations as part of the for-initialization assignments create implicit begin-end blocks around them.

 

The local variable declared within a for loop is equivalent to declaring an automatic variable in an unnamed block.

 

 

In Section 12.7.3, CHANGE

 

The number of loop variables shall not be greater than the number of dimensions of the array variable. Loop variables may be omitted to indicate no iteration over that dimension of the array, and trailing commas in the list may also be omitted. Loop variables are automatic and read-only, and their scope is local to the loop. The type of each loop variable is implicitly declared to be consistent with the type of array index. It shall be an error for any loop variable to have the same identifier as the array.

 

TO

The number of loop variables shall not be greater than the number of dimensions of the array variable. Loop variables may be omitted to indicate no iteration over that dimension of the array, and trailing commas in the list may also be omitted.

 

As in a for-loop (12.7.1), a foreach loop creates an implicit begin-end block around the loop statement, containing declarations of the loop variables with automatic lifetime. This block creates a new hierarchical scope, making the variables local to the loop scope. The block is unnamed by default, but can be named by adding a statement label (9.3.5) to the foreach statement. foreach loop variables are read-only. Loop variables are automatic and read-only, and their scope is local to the loop. The type of each loop variable is implicitly declared to be consistent with the type of array index. It shall be an error for any loop variable to have the same identifier as the array.