an fpc language extension: the BASED construct
Original Reporter info from Mantis: giuliano
-
Reporter name: G. Colla
Original Reporter info from Mantis: giuliano
- Reporter name: G. Colla
Description:
The patch proposed implements in the fpc language the BASED construct, as it exists in PL/1 PL/M, RPG etc. It is somewhat similar in its implementation to the C++ variable reference.
The declarations
aPtr: pointer;
aVar: AnyType BASED aPtr;
will make any reference to aVar be interpreted as a reference to (AnyType)Ptr^.
It is legal to have other declarations:
anotherVar: AnotherType BASED aPtr;
and any reference to anotherVar will be interpreted as (AnotherType)Ptr^ .
By a formal POV, the Declaration section of the fpc manual should include a new path:
>--Variable modifiers ---------- based ---------- pointer -------------->
| | | | |
| -- PUint expression --- | |
The advantage of this language extension is to eliminate redundant dereferincings and typecastings, by moving them from code to declarations.
This improves type checking, and makes the code less error prone, more readable, and more in line with Pascal philosophy.
Whenever possible Pascal hides implementation details, leaving programmer to
concentrate on the core of its problem.
It's very useful in message passing techniques, where different message types
are funnelled to a single endpoint. Typically such messages are records composed
by a fixed header which tells about the message type, followed by a variable
content. They can be found everywhere, from IPC to X11 interface to tcp/ip packets.
It is also useful in data entry applications, where different fields require
different data types.
The patch enclosed should be applied in the compiler folder, and includes
patches to:
- the compiler itself, for interpreting the new keyword, generating the proper
code, generating the proper ppu, and generate debugging information (only dwarf
supported).
- the ppudump utility.
In order to make the feature available in the Lazarus IDE, a second patch is
proposed to make codetools and syntax highlighter work with the new feature.
No change is required for debugging, because the debugging information generated
is already properly handled by gdb, provided of course that the -gw option
is activated.
The patch should be applied in the components folder of Lazarus source tree.
As a side note, in the fpc list someone proposed to implement the same feature
using a different syntax, in order to avoid the introduction of a new keyword,
taking advantage of the similarity to the ABSOLUTE construct, such as
aPtr: Pointer;
aVar: AnyType ABSOLUTE aPtr^;
I tested this solution but I realized that, while this syntax can be easily
implemented as far as code generation is concerned, it requires much heavier
changes in Lazarus codetools, synedit and lazdebug.
Moreover making two logically different features share the same code
makes the compiler code harder to debug and to maintain.
Therefore I discarded it, at least as a first choice.
Mantis conversion info:
- Mantis ID: 33007
- OS: any
- OS Build: any
- Platform: any
- Monitored by: » giuliano (G. Colla), » @PascalDragon (Sven Barth)