fpc/rtl/inc/rtti.inc
svenbarth 345d83c3e3 Merge RTTI changes from packages branch (including adjustments that were required due to changes in trunk since then). These changes favor source backwards compatibility in contrast to Delphi compatibility. Binary compatiblity is however drastically broken due to the indirect references that are platform independant!
Merged revision(s) 28239-28289 from branches/svenbarth/packages:
Provide the possiblity to switch between the direct and indirect RTTI symbols.

ncgrtti.pas, TRTTIWriter:
  + get_rtti_label, get_rtti_label_ord2str & get_rtti_label_str2ord: add new "indirect" parameter and pass that along to rtti_mangledname
hlcgobj.pas, thlcgobj:
  * g_incrrefcount, g_initialize, g_finalize, g_array_rtti_helper: for now pass along False to get_rtti_label()
ncgvmt.pas, TVMTWriter:
  * writevmt: for now pass along False to get_rtti_label()
ncgld.pas, tcgrttinode:
  * pass_generate_code: for now pass along False to the get_rtti_label*() methods
........
Provide possibility to select between using a direct and an indirect RTTI reference. This way the references can be changed for selected cases.

ncgrtti, TRTTIWriter:
  * ref_rtti: new "indirect" parameter that's relayed to rtti_mangledname()
  * write_rtti_reference: new "indirect" paramater that's relayed to ref_rtti()
........
Switch properties to use the indirect type information without breaking backwards compatiblity. :)

compiler/ncgrtti.pas, TRTTIWriter:
  * published_properties_write_rtti_data: use the indirect reference, not the direct one
rtl/objpas/typinfo.pp:
  * to allow compilation with both 2.6.x and 2.7.1 and too avoid too many ifdefs at least in the declarations we define a macro TypeInfoPtr which is either PTypeInfo (2.6.x) or PPTypeInfo (2.7.1 and newer)
  * TPropInfo: rename PropType to PropTypeRef and change type to TypeInfoPtr
  + TPropInfo: add a new property PropType which returns a PTypeInfo out of the PropTypeRef depending on the compiler version
........
Switch further simple types (sets, enums, class references, pointers) to indirect type information (again without breaking backwards compatibility).

compiler/ncgrtti.pas, TRTTIWriter.write_rtti_data:
  * enumdef_rtti, setdef_rtti, classrefdef_rtti & pointerdef_rtti: write an indirect RTTI reference

rtl/objpas/typinfo.pp, TTypeData:
  * rename BaseType to BaseTypeRef, CompType to CompTypeRef, InstanceType to InstanceTypeRef and RefType to RefTypeRef and change their type to TypeInfoPtr
  + add properties BaseType, CompType, InstanceType & RefType which return a PTypeInfo out of the corresponding *Ref field depending on the compiler version
........
Switch class and interface parent as well as extended type to indirect type information

compiler/ncgrtti.pas, TRTTIWriter:
  * write_rtti_data.objectdef_rtti.objectdef_rtti_class_full: use indirect reference for class parent and extended type
  * write_rtti_data.objectdef_rtti.objectdef_rtti_interface_full: use indirect reference for interface parent

rtl/objpas/typinfo.pp, TTypeData:
  + add new method DerefTypeInfoPtr which returns Nil if the indirect reference is Nil and otherwise returns the dereferences indirect reference (for 2.6.x the direct reference is returned as is)
  * rename ParentInfo to ParentInfoRef, HelperParent to HelperParentRef, ExtendedInfo to ExtendedInfoRef, IntfParent ot IntfParentRef and RawIntfParent to RawIntfParentRef and change their type to TypeInfoPtr
  + introduce ParentInfo, HelperParent, ExtendedInfo, IntfParent and RawIntfParent properties that return a PTypeInfo and use the new DerefTypeInfoPtr to return the correct type info value
  * change the other newly introduced properties of TTypeData to use DerefTypeInfoPtr as well to be on the safe side
........
Switch record/object fields to indirect type information references.

compiler/ncgrtti.pas, TRTTIWriter.fields_write_rtti_data:
  * use the indirect reference for the object parent type
  * use the indirect reference for the field type

rtl/inc/rtti.inc:
  * TRecordElement: change TypeInfo to PPointer for 2.7.1 and newer
  * RecordRTTI: correctly dereference the element type for 2.7.1 and newer
  * fpc_copy: correctly reference the element type for 2.7.1 and newer
........
Switch static arrays to indirect RTTI reference.

compiler/ncgrtti.pas, TRTTIWriter.write_rtti_data:
  * arraydef_rtti: write the dimension types and the final field type as indirect references

rtl/inc/rtti.inc:
  * TArrayInfo: switch ElInfo to PPointer for 2.7.1+
  * ArrayRTTI & fpc_copy: correctly dereference ElInfo for 2.7.1+
rtl/objpas/typinfo.pp, TArrayTypeData:
  * switch ElType and Dims to PPTypeInfo; no backwards compatibility needed here as TArrayTypeData was added in 2.7.1 only

tests/test/trtti8.pp: fix test
........
Switch dynamic arrays to indirect RTTI references.

compiler/ncgrtti.pas, TRTTIWriter.write_rtti_data.arraydef_rtti:
  * write indirect references for the two element entries

rtl/inc/dynarr.inc:
  * tdynarraytypedata: change elType2 to PPointer for 2.7.1+
  * fpc_dynarray_clear, fpc_dynarray_setlength & fpc_dynarray_copy: correctly dereference elType2 for 2.7.1+
rtl/objpas/typinfo.pp, TTypeData:
  * rename elType and elType2 to elTypeRef and elType2Ref respectively and change type to TypeInfoPtr
  * add properties elType and elType2 which return PTypeInfo by dereferencing elTypeRef and elType2Ref respecively correctly
  * remove a few stray "inline" directives in the implementation
........
Switch procedure parameters and result type to indirect RTTI references.

compiler/ncgrtti.pas, TRTTIWriter.write_rtti_data.procvardef_rtti:
  * write_procedure_param: use indirect RTTI reference
  * write result info for both methods and procvars as indirect RTTI reference
  * write parameters for procvars as indirect RTTI reference

rtl/objpas/typinfo.pp:
  * TProcedureParam: change type of ParamType to PPTypeInfo (no backwards compatiblity needed here; was added for 2.7.1)
  * TProcedureSignature: change type of RseultType to PPTypeInfo (no backwards compatibility needed here; was added for 2.7.1)
  * TTypeData: remark in the comments of tkMethod that ResultTypeRef and ParamTypeRefs are of type PPTypeInfo and not PTypeInfo

tests/test/trtti9.pp: 
  * fix test
........
Remove no longer needed "indirect" parameter for TRTTIWriter methods.

ncgrtti.pas, TRTTIWriter:
  - ref_rtti & write_rtti_reference: remove "indirect" parameter
  * ref_rtti: call rtti_mangledname with "indirect" always set to "true"
  - remove "true" parameter on callsites of write_rtti_reference & ref_rtti
........

git-svn-id: trunk@33944 -
2016-06-10 17:01:51 +00:00

419 lines
12 KiB
PHP

{
This file is part of the Free Pascal run time library.
Copyright (c) 1999-2000 by Michael Van Canneyt
member of the Free Pascal development team
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
{ Run-Time type information routines }
{ the tk* constants are now declared in system.inc }
{$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
{$define USE_PACKED}
{$endif}
{$ifdef VER2_6}
{$define USE_PACKED}
{$endif}
type
PRecordElement=^TRecordElement;
TRecordElement=
{$ifdef USE_PACKED}
packed
{$endif USE_PACKED}
record
{$ifdef VER3_0}
TypeInfo: Pointer;
{$else}
TypeInfo: PPointer;
{$endif}
{$ifdef VER2_6}
Offset: Longint;
{$else}
Offset: SizeInt;
{$endif}
end;
PRecordInfo=^TRecordInfo;
TRecordInfo=
{$ifdef USE_PACKED}
packed
{$endif USE_PACKED}
record
Size: Longint;
Count: Longint;
{ Elements: array[count] of TRecordElement }
end;
PArrayInfo=^TArrayInfo;
TArrayInfo=
{$ifdef USE_PACKED}
packed
{$endif USE_PACKED}
record
Size: SizeInt;
ElCount: SizeInt;
{$ifdef VER3_0}
ElInfo: Pointer;
{$else}
ElInfo: PPointer;
{$endif}
DimCount: Byte;
Dims:array[0..255] of Pointer;
end;
function RTTIArraySize(typeInfo: Pointer): SizeInt;
begin
typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
{$ifdef VER2_6}
result:=PArrayInfo(typeInfo)^.Size*PArrayInfo(typeInfo)^.ElCount;
{$else}
result:=PArrayInfo(typeInfo)^.Size;
{$endif}
end;
function RTTIRecordSize(typeInfo: Pointer): SizeInt;
begin
typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
result:=PRecordInfo(typeInfo)^.Size;
end;
function RTTISize(typeInfo: Pointer): SizeInt;
begin
case PByte(typeinfo)^ of
tkAString,tkWString,tkUString,
tkInterface,tkDynarray:
result:=sizeof(Pointer);
{$ifdef FPC_HAS_FEATURE_VARIANTS}
tkVariant:
result:=sizeof(TVarData);
{$endif FPC_HAS_FEATURE_VARIANTS}
tkArray:
result:=RTTIArraySize(typeinfo);
tkObject,tkRecord:
result:=RTTIRecordSize(typeinfo);
else
result:=-1;
end;
end;
{ if you modify this procedure, fpc_copy must be probably modified as well }
procedure RecordRTTI(Data,TypeInfo:Pointer;rttiproc:TRTTIProc);
var
count,
i : longint;
begin
typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
Count:=PRecordInfo(typeInfo)^.Count;
Inc(PRecordInfo(typeInfo));
{ Process elements }
for i:=1 to count Do
begin
rttiproc(Data+PRecordElement(typeInfo)^.Offset,PRecordElement(typeInfo)^.TypeInfo{$ifndef VER3_0}^{$endif});
Inc(PRecordElement(typeInfo));
end;
end;
{ if you modify this procedure, fpc_copy must be probably modified as well }
{$ifdef VER2_6}
procedure ArrayRTTI(Data,TypeInfo:Pointer;rttiproc:TRTTIProc);
var
i: SizeInt;
begin
typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
{ Process elements }
for I:=0 to PArrayInfo(typeInfo)^.ElCount-1 do
rttiproc(Data+(I*PArrayInfo(typeInfo)^.Size),PArrayInfo(typeInfo)^.ElInfo);
end;
{$else}
procedure ArrayRTTI(Data,TypeInfo:Pointer;rttiproc:TRTTIProc);
var
i,Count,ElSize: SizeInt;
Info: Pointer;
begin
typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
Count:=PArrayInfo(typeInfo)^.ElCount;
{ no elements to process => exit }
if Count = 0 then
Exit;
ElSize:=PArrayInfo(typeInfo)^.Size div Count;
Info:=PArrayInfo(typeInfo)^.ElInfo{$ifndef VER3_0}^{$endif};
{ Process elements }
for I:=0 to Count-1 do
rttiproc(Data+(I*ElSize),Info);
end;
{$endif}
Procedure fpc_Initialize (Data,TypeInfo : pointer);[Public,Alias : 'FPC_INITIALIZE']; compilerproc;
begin
case PByte(TypeInfo)^ of
{$ifdef FPC_HAS_FEATURE_DYNARRAYS}
tkDynArray,
{$endif FPC_HAS_FEATURE_DYNARRAYS}
{$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
tkAstring,
{$endif FPC_HAS_FEATURE_ANSISTRINGS}
{$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
tkWstring,tkUString,
{$endif FPC_HAS_FEATURE_WIDESTRINGS}
tkInterface:
PPchar(Data)^:=Nil;
tkArray:
arrayrtti(data,typeinfo,@int_initialize);
{$ifdef FPC_HAS_FEATURE_OBJECTS}
tkObject,
{$endif FPC_HAS_FEATURE_OBJECTS}
tkRecord:
recordrtti(data,typeinfo,@int_initialize);
{$ifdef FPC_HAS_FEATURE_VARIANTS}
tkVariant:
variant_init(PVarData(Data)^);
{$endif FPC_HAS_FEATURE_VARIANTS}
end;
end;
Procedure fpc_finalize (Data,TypeInfo: Pointer);[Public,Alias : 'FPC_FINALIZE']; compilerproc;
begin
case PByte(TypeInfo)^ of
{$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
tkAstring :
fpc_AnsiStr_Decr_Ref(PPointer(Data)^);
{$endif FPC_HAS_FEATURE_ANSISTRINGS}
{$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
tkUstring :
fpc_UnicodeStr_Decr_Ref(PPointer(Data)^);
{$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
tkWstring :
fpc_WideStr_Decr_Ref(PPointer(Data)^);
{$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
{$endif FPC_HAS_FEATURE_WIDESTRINGS}
tkArray :
arrayrtti(data,typeinfo,@int_finalize);
{$ifdef FPC_HAS_FEATURE_OBJECTS}
tkObject,
{$endif FPC_HAS_FEATURE_OBJECTS}
tkRecord:
recordrtti(data,typeinfo,@int_finalize);
tkInterface:
Intf_Decr_Ref(PPointer(Data)^);
{$ifdef FPC_HAS_FEATURE_DYNARRAYS}
tkDynArray:
fpc_dynarray_clear(PPointer(Data)^,TypeInfo);
{$endif FPC_HAS_FEATURE_DYNARRAYS}
{$ifdef FPC_HAS_FEATURE_VARIANTS}
tkVariant:
variant_clear(PVarData(Data)^);
{$endif FPC_HAS_FEATURE_VARIANTS}
end;
end;
Procedure fpc_Addref (Data,TypeInfo : Pointer); [Public,alias : 'FPC_ADDREF']; compilerproc;
begin
case PByte(TypeInfo)^ of
{$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
tkAstring :
fpc_AnsiStr_Incr_Ref(PPointer(Data)^);
{$endif FPC_HAS_FEATURE_ANSISTRINGS}
{$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
{$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
tkWstring :
fpc_WideStr_Incr_Ref(PPointer(Data)^);
{$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
tkUstring :
fpc_UnicodeStr_Incr_Ref(PPointer(Data)^);
{$endif FPC_HAS_FEATURE_WIDESTRINGS}
tkArray :
arrayrtti(data,typeinfo,@int_addref);
{$ifdef FPC_HAS_FEATURE_OBJECTS}
tkobject,
{$endif FPC_HAS_FEATURE_OBJECTS}
tkrecord :
recordrtti(data,typeinfo,@int_addref);
{$ifdef FPC_HAS_FEATURE_DYNARRAYS}
tkDynArray:
fpc_dynarray_incr_ref(PPointer(Data)^);
{$endif FPC_HAS_FEATURE_DYNARRAYS}
tkInterface:
Intf_Incr_Ref(PPointer(Data)^);
{$ifdef FPC_HAS_FEATURE_VARIANTS}
tkVariant:
variant_addref(pvardata(Data)^);
{$endif FPC_HAS_FEATURE_DYNARRAYS}
end;
end;
{ define alias for internal use in the system unit }
Function fpc_Copy_internal (Src, Dest, TypeInfo : Pointer) : SizeInt;[external name 'FPC_COPY'];
Function fpc_Copy (Src, Dest, TypeInfo : Pointer) : SizeInt;[Public,alias : 'FPC_COPY']; compilerproc;
var
Temp: pbyte;
copiedsize,
expectedoffset,
count,
offset,
i: SizeInt;
info: pointer;
begin
result:=sizeof(pointer);
case PByte(TypeInfo)^ of
{$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
tkAstring:
fpc_AnsiStr_Assign(PPointer(Dest)^,PPointer(Src)^);
{$endif FPC_HAS_FEATURE_ANSISTRINGS}
{$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
{$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
tkWstring:
fpc_WideStr_Assign(PPointer(Dest)^,PPointer(Src)^);
{$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
tkUstring:
fpc_UnicodeStr_Assign(PPointer(Dest)^,PPointer(Src)^);
{$endif FPC_HAS_FEATURE_WIDESTRINGS}
tkArray:
begin
Temp:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
{$ifdef VER2_6}
{ Process elements }
for I:=0 to PArrayInfo(Temp)^.ElCount-1 do
fpc_Copy_internal(Src+(I*PArrayInfo(Temp)^.Size),Dest+(I*PArrayInfo(Temp)^.Size),PArrayInfo(Temp)^.ElInfo);
Result:=PArrayInfo(Temp)^.Size*PArrayInfo(Temp)^.ElCount;
{$else}
Result:=PArrayInfo(Temp)^.Size;
Count:=PArrayInfo(Temp)^.ElCount;
{ no elements to process => exit }
if Count = 0 then
Exit;
Info:=PArrayInfo(Temp)^.ElInfo{$ifndef VER3_0}^{$endif};
copiedsize:=Result div Count;
Offset:=0;
{ Process elements }
for I:=1 to Count do
begin
fpc_Copy_internal(Src+Offset,Dest+Offset,Info);
inc(Offset,copiedsize);
end;
{$endif}
end;
{$ifdef FPC_HAS_FEATURE_OBJECTS}
tkobject,
{$endif FPC_HAS_FEATURE_OBJECTS}
tkrecord:
begin
Temp:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
Result:=PRecordInfo(Temp)^.Size;
Count:=PRecordInfo(Temp)^.Count;
Inc(PRecordInfo(Temp));
expectedoffset:=0;
{ Process elements with rtti }
for i:=1 to count Do
begin
Info:=PRecordElement(Temp)^.TypeInfo{$ifndef VER3_0}^{$endif};
Offset:=PRecordElement(Temp)^.Offset;
Inc(PRecordElement(Temp));
if Offset>expectedoffset then
move((Src+expectedoffset)^,(Dest+expectedoffset)^,Offset-expectedoffset);
copiedsize:=fpc_Copy_internal(Src+Offset,Dest+Offset,Info);
expectedoffset:=Offset+copiedsize;
end;
{ elements remaining? }
if result>expectedoffset then
move((Src+expectedoffset)^,(Dest+expectedoffset)^,Result-expectedoffset);
end;
{$ifdef FPC_HAS_FEATURE_DYNARRAYS}
tkDynArray:
fpc_dynarray_assign(PPointer(Dest)^,PPointer(Src)^,typeinfo);
{$endif FPC_HAS_FEATURE_DYNARRAYS}
tkInterface:
fpc_intf_assign(PPointer(Dest)^,PPointer(Src)^);
{$ifdef FPC_HAS_FEATURE_VARIANTS}
tkVariant:
begin
VarCopyProc(pvardata(dest)^,pvardata(src)^);
result:=sizeof(tvardata);
end;
{$endif FPC_HAS_FEATURE_VARIANTS}
end;
end;
{ For internal use by the compiler, because otherwise $x- can cause trouble. }
{ Generally disabling extended syntax checking for all compilerprocs may }
{ have unintended side-effects }
procedure fpc_Copy_proc (Src, Dest, TypeInfo : Pointer);compilerproc; inline;
begin
fpc_copy_internal(src,dest,typeinfo);
end;
procedure fpc_initialize_array(data,typeinfo : pointer;count : SizeInt); [public,alias:'FPC_INITIALIZE_ARRAY'] compilerproc;
var
i, size : SizeInt;
begin
size:=RTTISize(typeinfo);
if size>0 then
for i:=0 to count-1 do
int_initialize(data+size*i,typeinfo);
end;
procedure fpc_finalize_array(data,typeinfo : pointer;count : SizeInt); [Public,Alias:'FPC_FINALIZE_ARRAY']; compilerproc;
var
i, size: SizeInt;
begin
size:=RTTISize(typeinfo);
if size>0 then
for i:=0 to count-1 do
int_finalize(data+size*i,typeinfo);
end;
procedure fpc_addref_array(data,typeinfo: pointer; count: SizeInt); [public,alias:'FPC_ADDREF_ARRAY']; compilerproc;
var
i, size: SizeInt;
begin
size:=RTTISize(typeinfo);
if size>0 then
for i:=0 to count-1 do
int_addref(data+size*i,typeinfo);
end;
{ The following two procedures are now obsolete, needed only for bootstrapping }
procedure fpc_decref (Data, TypeInfo : Pointer);[Public,alias : 'FPC_DECREF']; compilerproc;
begin
int_finalize(Data,TypeInfo);
end;
procedure fpc_decref_array(data,typeinfo: pointer; count: SizeInt); [public,alias:'FPC_DECREF_ARRAY']; compilerproc;
begin
int_finalizeArray(data,typeinfo,count);
end;
procedure InitializeArray(p, typeInfo: Pointer; count: SizeInt);
external name 'FPC_INITIALIZE_ARRAY';
procedure FinalizeArray(p, typeInfo: Pointer; count: SizeInt);
external name 'FPC_FINALIZE_ARRAY';
procedure CopyArray(dest, source, typeInfo: Pointer; count: SizeInt);
var
i, size: SizeInt;
begin
size:=RTTISize(typeInfo);
if size>0 then
for i:=0 to count-1 do
fpc_Copy_internal(source+size*i, dest+size*i, typeInfo);
end;