[fpc-devel] Questions on RTTI

Michael Van Canneyt michael at freepascal.org
Fri Oct 23 11:50:08 CEST 2026



On Fri, 23 Oct 2026, Martin Frb via fpc-devel wrote:

> Can someone check the below statements for correctness please?
>
>
> ---------------------------------------------------
>
>
> - A  class itself has always some RTTI of its own. (but that does not 
> describe all the fields/members)

Yes, if you consider the list of managed fields as RTTI.

>
> -  other types may also have RTTI (even if they are outside a published 
> section, or even outside a class)

Yes.

>
> - fields/members can be found via RTTI  if they are published. That is 
> the field/member can be looked up by name within the RTTI of the class.

Yes.

But they can also be found for private/protected/published
fields/methods/properties if the programmer enabled extended RTTI.


>
>    (and that returns the RTTI for the field's type / and the location 
> or getter/setter of the field)

Yes.

>
>
> So for the "published" case the DW_TAG_Member needs to have the flag 
> (indicating the presence of RTTI)

Yes.

>
> (actually indicating that the containing type has type info for that 
> field, and its type)
>
>
> I'll have to double check the below with the fpc team, to make sure I 
> got the nitty gritty parts correct.
>
>
> RTTI may also be available for a type. Then the type would want to have 
> that flag, but that would not relate to the published concept.

TypeInfo(TSomeType) will generate RTTI for a type. This allows e.g. to
convert an enum into a string and vice versa.

>
> Some types have always RTTI for the type. Such types could be declared 
> scoped inside a class. But that would not mean they are published.
>
>
> Afaik (and as I said, I am double checking that)
>
> type
>
>    TFoo = class
>
>    public type
>
>        TMyEnum =(mon,tue,wed,thu,fri);
>
>   public
>
>        FMyAnonEnumVal = (Jan, Feb, Mar, Apr);
>
>    published
>
>        Value: integer;
>
>    end;
>
>
> TMyEnum has rtti for the type.

AFAIK: Not necessarily. 
Only if you
- you write() it 
- You have typeinfo(TMyEnum) somewhere
- you have extended RTTI enabled for the class.


>  But can't be looked up by name. (afaik 
> enums always produce RTTI, becaues they can be printed (with writeln) 
> using the name, rather than the stored ordinal val)

I think the compiler detects whether it is actually used and only emits it
then. Needs verifying.

> The same for FMyAnonEnumVal.
>
>
> But the published "Value" can also be found by looking up the name 
> "Value" starting from the class.

Yes.

Michael.


More information about the fpc-devel mailing list