The preliminary patch attached to this ticket addresses #451 by adding support for mixed declarations and statements within the same block in a minimally invasive way.
It introduces the concept of "implicit blocks" and keeps most of the changes within the parser.
The changes can be summarized as follows:
If there is a declaration after a statement, the parser will pretend that it is part of a new nested block that extends to the end of the actual block.
To allow a distinction between explicit and implicit blocks, nest level variables, some of which had to be widened, are semantically partitioned into an upper and a lower portion, so that explicit blocks modify the nest level by a LEVEL_UNIT (=65536) while implicit blocks modify it by a SUBLEVEL_UNIT (=1).
The check for name conflicts divides by LEVEL_UNIT before comparing and thus ignores sublevels.
The rest of the code base can remain agnostic of the changes.
This should be the easiest way to add the intended functionality, provided that it is semantically equivalent in each and every obscure corner case, which has not been checked, yet.
I'd prefer a type other than int for the level. After all, LEVEL_UNIT is outside of the minimum range of int. I'd suggest to use long.
Philipp
Last edit: Philipp Klaus Krause 2018-06-14
I settled for
intbecause it is the data type that all other level variables in the code base use. Those would then have to be changed tolong, as well.Are there actually any supported host architectures with 16 bit
inttype?There would also be the possibility to define
LEVEL_UNITrelative tosizeof(int).Currently we do not support such hosts (though two years ago, there was a fork trying to make it work on one such host).
Defining relative to sizeof(level) (so when someone changes the type, the LEVEL_UNIT changes automatically) should do.
Philipp
We should also have tests for the new functionality.
I suggest to add a scope.c (or whatever name seems appropriate) in support/regression/tests (that should check a few corner cases, to ensure that they compile and the right one of multiple variables of the same name is used) and in support/valdiag/tests to check that diagnostics are emitted for invalid declarations and for declarations after statements in C89 mode.
In support/regression/tests/p99-conformance, we should remove the SKIP_MIXED define.
Philipp
This is a reworked version of the patch. It changes the type of all nest level variables to long, alters all relevant format strings, fixes bugs caused by the inlining functionality in SDCCast.c (which did not use LEVEL_UNIT) and adds test cases.
Thanks again for the patch. It is merged in [r10450].
Philipp