Please note that this will likely break if a class contains properties, especially once Extended RTTI is integrated. Not to mention that it is in no way guaranteed that classes are written in that order.
I called it "crime" as no one should need to use something like that. I assume the compiler will provide a better mechanism in the near future ;-)
I did mention the class properties in the memory layout, but yes, did not account for them. Adding class properties should be easy considering that
Name is the only variable part of the record and located at the end, and knowing that all Pascal identifiers are limited to ['A'..'Z','a'..'z','0'..'9','_'] and does not start with a number.
NameIndex could help as well. So finding identifier names could use something like:
procedure RSearchIdentifierName(var p: pByte);
const
AllowedChars: set of char = ['A'..'Z','a'..'z','0'..'9','_'];
var
lastChar: pByte;
begin
{
Identifiers consist of between 1 and 127 significant characters
(letters, digits and the underscore character),
of which the first must be a letter (a-z or A-Z), or an underscore (_).
}
repeat
{ Skip non-allowed chars }
while not (char(p^) in AllowedChars) do
dec(p);
lastChar := p;
{ Find possible beginning}
while true do
begin
if (p+p^<=lastChar) then exit;
if not (char(p^) in AllowedChars) then
break;
dec(p);
end;
until false;
end;
to find classes, then we need to check the byte preceding the possible identifier name for
tkClass, then we have more means to make sure we landed on a class.
Repeating the process forward is easier than looking in reverse direction. OTOH, scanning the whole
.data section, where our classes are kept, is also possible.
As for breakage, you guys can, rightfully, cause this code to break. And we will have to fix it. Luckily, that does not happen frequently.
Honestly, I posted that code to get Thaddy to show us his example. Alas, I failed. :-|