Sign in Join

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

Mailing lists / fpc-pascal / 2003-04

Forward type declaration

11 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

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

Johann Glaser

Hi!

Currently I'm translating libusb for FPC. Therefore I used "h2pas".

libusb has two types called usb_device and usb_bus. Both are records. Each of them has fields, which have ^usb_device and ^usb_bus as types. So there is a cross type reference between these two records.

In C they simply build a forward type declaration ("struct usb_device; struct usb_bus;"). In FPC unfortunately this is not possible. Is this correct?

Are there any plans to add forward type declarations?

BTW: h2pas crashes with runtime error 216 when it encounters incomplete type definitions like the above mentioned "struct"s. I'm using the Debian 1.0.6-1 package of fp-compiler and all other parts.

Bye
Hansi

Johann Glaser Vienna University of Technology Electrical Engineering ____ http://www.johann-glaser.at/ ____
Peter Vreman
to Johann Glaser
Show quoted text 13 lines
Hi! Currently I'm translating libusb for FPC. Therefore I used "h2pas". libusb has two types called usb_device and usb_bus. Both are records. Each of them has fields, which have ^usb_device and ^usb_bus as types. So there is a cross type reference between these two records. In C they simply build a forward type declaration ("struct usb_device; struct usb_bus;"). In FPC unfortunately this is not possible. Is this correct? Are there any plans to add forward type declarations?

It is already possible.

You need to define a pointer type to the record first. See the example for a linked list:

type
  PRec = ^TRec
  TRec = record
     next  : PRec;
  end;

Peter

Johann Glaser
to Peter Vreman: It is already possible. You need to define a pointer type to the record first. See the…

Hi Peter!

Show quoted text 10 lines
It is already possible. You need to define a pointer type to the record first. See the example for a linked list: type PRec = ^TRec TRec = record next : PRec; end;

I tried this. But therefore the declaration of TRec must be immediately after the declaration of PRec. Nothing can be in between. But this would be necessary for my case.

Bye
Hansi

Johann Glaser Vienna University of Technology Electrical Engineering ____ http://www.johann-glaser.at/ ____
Unknown author
to Johann Glaser: Hi! Currently I'm translating libusb for FPC. Therefore I used "h2pas". libusb has two…
Johann Glaser wrote 13 lines
Hi! Currently I'm translating libusb for FPC. Therefore I used "h2pas". libusb has two types called usb_device and usb_bus. Both are records. Each of them has fields, which have ^usb_device and ^usb_bus as types. So there is a cross type reference between these two records. In C they simply build a forward type declaration ("struct usb_device; struct usb_bus;"). In FPC unfortunately this is not possible. Is this correct? Are there any plans to add forward type declarations?
You can simply solve this by declaring
  Pusb_device = ^usb_device;
  usb_device = record
    //...
  end;

In fact, using the -P option of h2pas will do this for you.

BTW: h2pas crashes with runtime error 216 when it encounters incomplete type definitions like the above mentioned "struct"s. I'm using the Debian 1.0.6-1 package of fp-compiler and all other parts.

This is a known problem; h2pas is far from perfect, it is meant to help people out when translating by doing most of the work. Some things, however cannot be done.

Michael.

Peter Vreman
to Johann Glaser: Hi Peter! I tried this. But therefore the declaration of TRec must be immediately after…
Show quoted text 17 lines
Hi Peter! > It is already possible. > > You need to define a pointer type to the record first. See the example > for > a linked list: > > type > PRec = ^TRec > TRec = record > next : PRec; > end; I tried this. But therefore the declaration of TRec must be immediately after the declaration of PRec. Nothing can be in between. But this would be necessary for my case.

It should be in the same type block. That should (always?) be possible to manage as type declarations are not dependent on code, maybe on constants, but constants are not dependent on structured types.

Peter

Unknown author
to Johann Glaser: Hi Peter! I tried this. But therefore the declaration of TRec must be immediately after…
Johann Glaser wrote 16 lines
Hi Peter! > It is already possible. > > You need to define a pointer type to the record first. See the example for > a linked list: > > type > PRec = ^TRec > TRec = record > next : PRec; > end; I tried this. But therefore the declaration of TRec must be immediately after the declaration of PRec. Nothing can be in between. But this would be necessary for my case.

I don't see why ? Please explain ?

Michael.

Johann Glaser
to Unknown author

Hi Michael!

Show quoted text 11 lines
> > type > > PRec = ^TRec > > TRec = record > > next : PRec; > > end; > > I tried this. But therefore the declaration of TRec must be immediately > after the declaration of PRec. Nothing can be in between. But this would > be necessary for my case. I don't see why ? Please explain ?

The compiler complains if there is something in between.

HA! I found it: There must not be another "Type" keyword in between. Could you please add "forward type declarations" and this important fact to the documentation?

Thanks
Hansi

Johann Glaser Vienna University of Technology Electrical Engineering ____ http://www.johann-glaser.at/ ____
Unknown author
to Johann Glaser
Johann Glaser wrote 19 lines
Hi Michael! > > > type > > > PRec = ^TRec > > > TRec = record > > > next : PRec; > > > end; > > > > I tried this. But therefore the declaration of TRec must be immediately > > after the declaration of PRec. Nothing can be in between. But this would > > be necessary for my case. > > I don't see why ? Please explain ? The compiler complains if there is something in between. HA! I found it: There must not be another "Type" keyword in between. Could you please add "forward type declarations" and this important fact to the documentation?

Done.

Michael.

Jeff Pohlmeyer
h2pas crashes with runtime error 216 when it encounters incomplete type definitions like the above mentioned "struct"s.

I submitted a patch back in February that should *hopefully* solve this problem. You can try upgrading h2pas from the CVS sources, or see:

http://www.nl.freepascal.org/lists/fpc-devel/2003-February/002782.html http://www.nl.freepascal.org/lists/fpc-devel/2003-February/002783.html

__________________________________________________ Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo
http://search.yahoo.com

max martinelli

Hi Hansi... hope this code can help u!!
(all with in the same "type" block... i.e. do not "interrupt" the "type" block with "const" or "procedure" or "function" or "type")

type
pusb_device  = ^usb_device;
pusb_bus = ^usb_bus;
// .. more typedefs
usb_device  = record
    a: pusb_bus;
    b: pusb_device;
    // x:  usb_device  is invalid (recursive!!!)
    // xx: usb_bus is invalid too.
    end;
// .. more typedefs
usb_bus = record
    c: pusb_bus;
    d: usb_device ;
    e: pusb_device ;
    // xx: usb_bus is invalid (recursive!!!)
    end;

warm regards
max

Message: 1

Earlier message 27 lines
From: Johann Glaser To: FPC-Pascal Organization: Date: 23 Apr 2003 21:35:41 +0200 Subject: [fpc-pascal]Forward type declaration Reply-To: Hi! Currently I'm translating libusb for FPC. Therefore I used "h2pas". libusb has two types called usb_device and usb_bus. Both are records. Each of them has fields, which have ^usb_device and ^usb_bus as types. So there is a cross type reference between these two records. In C they simply build a forward type declaration ("struct usb_device; struct usb_bus;"). In FPC unfortunately this is not possible. Is this correct? Are there any plans to add forward type declarations? BTW: h2pas crashes with runtime error 216 when it encounters incomplete type definitions like the above mentioned "struct"s. I'm using the Debian 1.0.6-1 package of fp-compiler and all other parts. Bye Hansi