Recent

Author Topic: Getting const offset of nested record field  (Read 678 times)

scribly

  • Jr. Member
  • **
  • Posts: 78
Getting const offset of nested record field
« on: August 02, 2022, 04:40:53 pm »
I'm trying to get a const filled with the offset of a field in a record.  I can get it to work for top fields, but not for nested fields

Code: Pascal  [Select][+][-]
  1. type
  2.   TSomething=record
  3.     a: integer;
  4.     b: integer;
  5.     c: integer;
  6.     d: record
  7.       a: integer;
  8.       b: integer;
  9.       c: integer;
  10.       d: integer;
  11.       e: integer;
  12.     end;
  13.     e: integer;
  14.   end;
  15.  
  16.   PSomething=^TSomething;
  17.  
  18. const
  19.   works=PtrUInt(@PSomething(nil)^.b);
  20.   worksaswell=PtrUInt(@PSomething(nil)^.d);
  21.   doesnotwork=PtrUInt(@PSomething(nil)^.d.c);
  22.  
  23. var doeswork: ptruint;
  24.  
  25. procedure initDoesWork();
  26. begin
  27.   doeswork:=PtrUInt(@PSomething(nil)^.d.c);
  28. end;
  29.  

I can of course use runtime code to fill it in, but it would be nice if there's a way to fill this in at compiletime
Any suggestions or there's really  no other way ?

Nicole

  • Hero Member
  • *****
  • Posts: 509
Re: Getting const offset of nested record field
« Reply #1 on: August 02, 2022, 06:14:14 pm »
you mix pointer and record.
The pointer points to the field, but then the address has done its job.
IMHO pointers cannot nest, because they are pointers.

Is it an option for you, just to fill in the integers as numbers or would this destroy your plan?

PascalDragon

  • Hero Member
  • *****
  • Posts: 4765
  • Compiler Developer
Re: Getting const offset of nested record field
« Reply #2 on: August 03, 2022, 10:19:38 am »
I can of course use runtime code to fill it in, but it would be nice if there's a way to fill this in at compiletime


If you declare your function as inline it should be rather similar to a constant so you could use that as a workaround.

Any suggestions or there's really  no other way ?

Would you please test with FPC 3.3.1, cause there might have been a few changes recently. Otherwise please report a bug.

you mix pointer and record.
The pointer points to the field, but then the address has done its job.
IMHO pointers cannot nest, because they are pointers.

You can perfectly fine retrieve pointers to records, its fields or fields of nested records no matter if at compile- or runtime.

MarkMLl

  • Hero Member
  • *****
  • Posts: 5596
Re: Getting const offset of nested record field
« Reply #3 on: August 03, 2022, 11:15:49 am »
I'm trying to get a const filled with the offset of a field in a record.  I can get it to work for top fields, but not for nested fields

Silly question, but are things being made worse by the fact that the main and subfields have a common name?

I'd suggest that you might do better if you broke the nested field out as its own type, computed the offsets of its fields separately, and then added those to the main field offset as appropriate.

That would leave the problem of variant records, which (with care) would probably be amenable to the same treatment, otherwise obviously note PascalDragon's comments.

In all cases, I'd be inclined to have assertions somewhere to make sure that integer (etc.) actually does have the size expected. I'd also be inclined to put a comment wherever the non-obvious @PSomething(nil)^ construct is used, so that anybody reading the code understands that it's simulating a variable at address zero... and I don't know whether this is necessary but it might be worth having an explicit check that PtrUInt(nil) is zero since while I don't think this applies to FPC I've seen platforms where it didn't hold.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

bytebites

  • Hero Member
  • *****
  • Posts: 523
Re: Getting const offset of nested record field
« Reply #4 on: August 03, 2022, 10:43:48 pm »
Compile without errorrs with 3.3.1. (Just add begin end. to the end of the program.

PascalDragon

  • Hero Member
  • *****
  • Posts: 4765
  • Compiler Developer
Re: Getting const offset of nested record field
« Reply #5 on: August 08, 2022, 11:22:01 pm »
Compile without errorrs with 3.3.1. (Just add begin end. to the end of the program.

Then it was some error in older FPC versions that got fixed with 3.3.1. If you'd be willing to find the correct commit it might be merged back to 3.2.x so that it will be fixed in the upcoming 3.2.4 as well (or you can test 3.2.x to check whether it has already been merged back).

 

TinyPortal © 2005-2018