[fpc-devel] Multiple type sections - Far forward type declarations [feasible feature request?]

Michael Van Canneyt michael at freepascal.org
Tue Oct 31 09:46:52 CET 2017



On Mon, 30 Oct 2017, Sandro Cumerlato wrote:

> Hello,
> please take a look at this small example:
>
> ----------------------------------------------
> program types;
>
> type
>  PMyTypeA = ^MyTypeA;
>  PMyTypeB = ^MyTypeB;
>
> type
>  MyTypeA = record
>    foo: integer;
>    bar: integer;
>  end;
>
> type
>  MyTypeB = record
>    foo: string;
>    bar: string;
>  end;
>
> begin
> end.
> ----------------------------------------------
>
> it obviously doesn't compile because there are multple "type" sections
> defined and forward types are not properly resolved within the same "type"
> section, reference: https://www.freepascal.org/docs-html/ref/refse16.html
>
> Considering that I often encounter difficulties translating C headers to
> Pascal because declared types are most of the time dislocated in the header
> source file and splitted in different blocks, I'd like to know if there is
> a switch to enable forward type resolution within the same source file at
> least, and if not please let me know if it can be considered a feasible
> feature request.

I'm not a compiler expert, but the following is an attempt at an answer:

Originally, Pascal is a one-pass language.

That means that when doing

Type
   Tsomething = record
    end;

Var
   a : TSomething;

When the compiler encounters the declaration of a, it has all information
it needs to define A. (stack space to use etc.)

With your extended "forward type resolution" this would no longer be possible.
Theoretically it probably can, but multiple passes would be needed. 
This would aversely affect the compiler speed.

Maybe some form of section folding (Type A ... Type B ->  Type A, B) can be done.

But maybe h2pas can be improved. multiple "type' sections following each
other should not happen.

Michael.



More information about the fpc-devel mailing list