Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2017-10

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

7 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Sandro Cumerlato

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.

Thank You in advance.

Sandro Cumerlato

Reply by email

Michael Van Canneyt
to Sandro Cumerlato
Sandro Cumerlato wrote 36 lines
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.

Reply by email

Mattias Gaertner
to Michael Van Canneyt
[...] 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.

True for such an extended forward.
FPC allows only a simple forward. For example this is not allowed:

type
  TAnimal = class;
  TBird = class(TAnimal) end;
  TAnimal = class end;

The check at the end of a type section is done for usability, so that missing declarations are spotted early and you cannot write spaghetti code. There is no technical reason to check later, e.g. at the end of the unit interface.

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.

+1

Another solution is the h2paswizard in Lazarus, which uses h2pas and various codetools. One of them sorts the declarations.

Mattias

Reply by email

Marco van de Voort
to Michael Van Canneyt: I'm not a compiler expert, but the following is an attempt at an answer: Originally,…

In our previous episode, Michael Van Canneyt said:

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.

Note that the example shows consecutive type blocks. They can be merged simply by ignoring a TYPE while already in a type block.

This saves some trouble cause by header generators.

Reply by email

Michael Van Canneyt
to Marco van de Voort: In our previous episode, Michael Van Canneyt said: Note that the example shows…
Marco van de Voort wrote 8 lines
In our previous episode, Michael Van Canneyt said: > > 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. Note that the example shows consecutive type blocks. They can be merged simply by ignoring a TYPE while already in a type block.

That was the gist of my proposal to do "type section folding".

This saves some trouble cause by header generators.

And hence my proposal to improve the header generators...

Michael.

Reply by email

Sven Barth

Am 31.10.2017 10:47 schrieb "Michael Van Canneyt" :

On Tue, 31 Oct 2017, Marco van de Voort wrote:

In our previous episode, Michael Van Canneyt said:

Show quoted text 11 lines
> > 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. > Note that the example shows consecutive type blocks. They can be merged simply by ignoring a TYPE while already in a type block.

That was the gist of my proposal to do "type section folding".

That would indeed be something that could be done. However I'd need to check how well that would already work across include files.

Regards,
Sven

Reply by email