Peter N Lewis wrote:
Personally, I don't see any particular reason to restrict the compiler to what can be used for an index variable unless there is some good reason for it. If it significantly simplifies the compiler to only allow index variables that are local simple variables, that's fine. But otherwise, why worry about it, as long as the semantics are clearly spelled out and self consistent, then if the user wants to shoot themselves in the foot with weird uses, that's their own fault. I would suggest implementing a for loop as:
for index := lower to upper do begin whatever; end;
exactly equivalent to:
temp_indexptr := @index; temp_indexptr^ := lower; temp_upper := upper while temp_indexptr^ <= temp_upper do begin whatever; Inc(temp_indexptr^); end;
(expanding for the other options of for loops as required).
But Pascal for-loops are more than while loops. They guarantee that the loop will be executed as specified in the for-clause, without side-effects. I don't see any particular reason to abandon these guarantees.
I might allow constant indicies in BP mode if it's not too much work. In this case, I'll probably have to consider threatening the whole array as dangerous, as we don't track threatening of single fields. Anyway, in BP mode this is a warning only ...
(As a side-note, not related to the actual issue here, the above code would be wrong if temp_upper is the maximum value of the control variable. Then it would produce an out-of-range value, and if not checked, possibly a wrap-around, looping forever. Standard for-loops are described in more complex ways to avoid these problems. See the standards, or actually the BP manual as well, which in this regard mostly copied the text from the standards.)
Then change the documentation from:
Please note: A modification of the index variable may result in unpredictable action.
Oops, I hadn't noticed this wrong statement in the documentation. I'm changing it to this:
@strong{Please note:} A modification of the index variable would result in undefined behaviour. Therefore it is forbidden by the standards, and GPC does not allow it. In some modes for compatibility with other dialects that do not enforce this restriction, GPC only gives a warning. Heed the warning, the behaviour is still undefined.
Frank