Recent

Author Topic: Compiling shadow classes with FPC  (Read 905 times)

Alex.Machado

  • New Member
  • *
  • Posts: 35
Compiling shadow classes with FPC
« on: October 02, 2023, 12:42:17 am »
Hi all,

I've been a Delphi programmer for more than 2 decades now and I'm currently re-starting with Lazarus after a long hiatus.

Today I found a problem using the latest FPC trunk source code (v 3.3.1) trying to build a simple shadow class that tries to access a private field of some other class. Interestingly the same code builds and works correctly using FPC v.3.2.2.

This hack is well known and sometimes it's used as a last resort to fix some 3rd party code when it's not possible nor desirable to change the original class interface.
The shadow class hack was explained in detail here:
https://hallvards.blogspot.com/2004/06/hack-5-access-to-private-fields.html

Using FPC v 3.3.1 it gives me the error: Class or Object types ”arg1” and ”arg2” are not related

I couldn't find any compiler directive that controls this behavior...

How can I turn this check off and just tell to the compiler "trust me... I know what I'm doing"??  ::)
« Last Edit: October 02, 2023, 12:48:40 am by Alex.Machado »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10553
  • Debugger - SynEdit - and more
    • wiki
Re: Compiling shadow classes with FPC
« Reply #1 on: October 02, 2023, 09:47:35 am »
That message should be a warning.

Unless you use something like
-Se => tread warning as error
or
-CR


But "using 3.3.1" trust me, you do not know what you are doing. Well maybe, maybe not.

If you use such "hack classes" in 3.3.1 then make sure that all involved code is compile with -O- (without any optimization) or maybe (have to check) -O1 may also work.

FPC has an optimization "class field reordering" => if your code is optimized by the compiler then the order of fields in memory in your shadow class may be different than in the original.




Alex.Machado

  • New Member
  • *
  • Posts: 35
Re: Compiling shadow classes with FPC
« Reply #2 on: October 02, 2023, 10:32:52 am »
Thanks for your response. I'm pretty sure it is an error not a warning and AFAIK there is no -Se or -CR switches being used. I'll double check.

About the class field reordering, it is only enabled in -O4 level according to the docs. Anyway, for this particular case, I'm confidedn that it will work even if -O4 is enabled, something that I intend to confirm if FPC let me play with that code anyway


 

TinyPortal © 2005-2018