[fpc-pascal] Constants in generics

Sven Barth pascaldragon at googlemail.com
Tue Jan 8 23:28:11 CET 2019


Am Di., 8. Jan. 2019, 21:01 hat Ryan Joseph <ryan at thealchemistguild.com>
geschrieben:

> I’ve made constants respect their proper definition now so we can do range
> checking but this broke some other things. For example there’s a range
> check error with static arrays now because “U” has a value of 0 so 0-1 = -1:
>
> type
>         generic TList<const U:byte> = record
>                 list: array[0..U-1] of integer; // ERROR: high value < low
> value (0..-1)
>         end;
>
>
> There’s a couple ways to resolve this:
>
> 1) Add some extra flags to nodes/syms so “U-1” can be known to contain a
> generic parameter and errors can be ignored. That’s the most complicated
> solution and I wasn’t able to figure that out easily because flags need to
> be transferred in multiple locations.
>

This is the preferred solution as otherwise you'd also defeat the purpose
of type safety and probably open another can of worms when constant
parameters are used in more complex expressions.

The symbols should already have sp_generic_param set (if not that should
definitely be changed so that it matches the type symbols). Now you only
need a way to differentiate whether the symbol is an undefined generic
parameter or not.
For type parameters it abuses the fact that the undefined defs of the
parameters are children of the generic while the final types are never
children of the generic. Maybe we can use something similar for the
constants?
If not we can always add some flag to tconstsym...

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20190108/be3ae105/attachment.html>


More information about the fpc-pascal mailing list