rtti.inc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Michael Van Canneyt
  5. member of the Free Pascal development team
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. { Run-Time type information routines }
  13. { The RTTI is implemented through a series of constants : }
  14. Const
  15. tkUnknown = 0;
  16. tkInteger = 1;
  17. tkChar = 2;
  18. tkEnumeration = 3;
  19. tkFloat = 4;
  20. tkSet = 5;
  21. tkMethod = 6;
  22. tkSString = 7;
  23. tkString = tkSString;
  24. tkLString = 8;
  25. tkAString = 9;
  26. tkWString = 10;
  27. tkVariant = 11;
  28. tkArray = 12;
  29. tkRecord = 13;
  30. tkInterface = 14;
  31. tkClass = 15;
  32. tkObject = 16;
  33. tkWChar = 17;
  34. tkBool = 18;
  35. tkInt64 = 19;
  36. tkQWord = 20;
  37. tkDynArray = 21;
  38. type
  39. TRTTIProc=procedure(Data,TypeInfo:Pointer);
  40. procedure RecordRTTI(Data,TypeInfo:Pointer;rttiproc:TRTTIProc);
  41. {
  42. A record is designed as follows :
  43. 1 : tkrecord
  44. 2 : Length of name string (n);
  45. 3 : name string;
  46. 3+n : record size;
  47. 7+n : number of elements (N)
  48. 11+n : N times : Pointer to type info
  49. Offset in record
  50. }
  51. var
  52. Temp : pbyte;
  53. namelen : byte;
  54. count,
  55. offset,
  56. i : longint;
  57. info : pointer;
  58. begin
  59. Temp:=PByte(TypeInfo);
  60. inc(Temp);
  61. { Skip Name }
  62. namelen:=Temp^;
  63. inc(temp,namelen+1);
  64. temp:=aligntoptr(temp);
  65. { Skip size }
  66. inc(Temp,4);
  67. { Element count }
  68. Count:=PLongint(Temp)^;
  69. inc(Temp,sizeof(Count));
  70. { Process elements }
  71. for i:=1 to count Do
  72. begin
  73. Info:=PPointer(Temp)^;
  74. inc(Temp,sizeof(Info));
  75. Offset:=PLongint(Temp)^;
  76. inc(Temp,sizeof(Offset));
  77. rttiproc (Data+Offset,Info);
  78. end;
  79. end;
  80. procedure ArrayRTTI(Data,TypeInfo:Pointer;rttiproc:TRTTIProc);
  81. {
  82. An array is designed as follows :
  83. 1 : tkArray;
  84. 2 : length of name string (n);
  85. 3 : NAme string
  86. 3+n : Element Size
  87. 7+n : Number of elements
  88. 11+n : Pointer to type of elements
  89. }
  90. var
  91. Temp : pbyte;
  92. namelen : byte;
  93. count,
  94. size,
  95. i : SizeInt;
  96. info : pointer;
  97. begin
  98. Temp:=PByte(TypeInfo);
  99. inc(Temp);
  100. { Skip Name }
  101. namelen:=Temp^;
  102. inc(temp,namelen+1);
  103. temp:=aligntoptr(temp);
  104. { Element size }
  105. size:=PSizeInt(Temp)^;
  106. inc(Temp,sizeof(Size));
  107. { Element count }
  108. Count:=PSizeInt(Temp)^;
  109. inc(Temp,sizeof(Count));
  110. Info:=PPointer(Temp)^;
  111. inc(Temp,sizeof(Info));
  112. { Process elements }
  113. for I:=0 to Count-1 do
  114. rttiproc(Data+(I*size),Info);
  115. end;
  116. Procedure fpc_Initialize (Data,TypeInfo : pointer);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[Public,Alias : 'FPC_INITIALIZE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  117. begin
  118. case PByte(TypeInfo)^ of
  119. tkAstring,tkWstring,tkInterface,tkDynArray:
  120. PPchar(Data)^:=Nil;
  121. tkArray:
  122. arrayrtti(data,typeinfo,@int_initialize);
  123. tkObject,
  124. tkRecord:
  125. recordrtti(data,typeinfo,@int_initialize);
  126. {$ifdef HASVARIANT}
  127. tkVariant:
  128. variant_init(Variant(PVarData(Data)^));
  129. {$endif HASVARIANT}
  130. end;
  131. end;
  132. Procedure fpc_finalize (Data,TypeInfo: Pointer);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[Public,Alias : 'FPC_FINALIZE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  133. begin
  134. case PByte(TypeInfo)^ of
  135. tkAstring :
  136. begin
  137. fpc_AnsiStr_Decr_Ref(PPointer(Data)^);
  138. PPointer(Data)^:=nil;
  139. end;
  140. {$ifdef HASWIDESTRING}
  141. tkWstring :
  142. begin
  143. fpc_WideStr_Decr_Ref(PPointer(Data)^);
  144. PPointer(Data)^:=nil;
  145. end;
  146. {$endif HASWIDESTRING}
  147. tkArray :
  148. arrayrtti(data,typeinfo,@int_finalize);
  149. tkObject,
  150. tkRecord:
  151. recordrtti(data,typeinfo,@int_finalize);
  152. {$ifdef HASINTF}
  153. tkInterface:
  154. begin
  155. Intf_Decr_Ref(PPointer(Data)^);
  156. PPointer(Data)^:=nil;
  157. end;
  158. {$endif HASINTF}
  159. tkDynArray:
  160. fpc_dynarray_decr_ref(PPointer(Data)^,TypeInfo);
  161. {$ifdef HASVARIANT}
  162. tkVariant:
  163. variant_clear(Variant(PVarData(Data)^))
  164. {$endif HASVARIANT}
  165. end;
  166. end;
  167. Procedure fpc_Addref (Data,TypeInfo : Pointer);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif} [Public,alias : 'FPC_ADDREF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  168. begin
  169. case PByte(TypeInfo)^ of
  170. tkAstring :
  171. fpc_AnsiStr_Incr_Ref(PPointer(Data)^);
  172. {$ifdef HASWIDESTRING}
  173. tkWstring :
  174. fpc_WideStr_Incr_Ref(PPointer(Data)^);
  175. {$endif HASWIDESTRING}
  176. tkArray :
  177. arrayrtti(data,typeinfo,@int_addref);
  178. tkobject,
  179. tkrecord :
  180. recordrtti(data,typeinfo,@int_addref);
  181. tkDynArray:
  182. fpc_dynarray_incr_ref(PPointer(Data)^);
  183. {$ifdef HASINTF}
  184. tkInterface:
  185. Intf_Incr_Ref(PPointer(Data)^);
  186. {$endif HASINTF}
  187. end;
  188. end;
  189. { alias for internal use }
  190. { we use another name else the compiler gets puzzled because of the wrong forward def }
  191. procedure fpc_systemDecRef (Data, TypeInfo : Pointer);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[external name 'FPC_DECREF'];
  192. Procedure fpc_DecRef (Data, TypeInfo : Pointer);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[Public,alias : 'FPC_DECREF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  193. begin
  194. case PByte(TypeInfo)^ of
  195. { see AddRef for comment about below construct (JM) }
  196. tkAstring:
  197. fpc_AnsiStr_Decr_Ref(PPointer(Data)^);
  198. {$ifdef HASWIDESTRING}
  199. tkWstring:
  200. fpc_WideStr_Decr_Ref(PPointer(Data)^);
  201. {$endif HASWIDESTRING}
  202. tkArray:
  203. arrayrtti(data,typeinfo,@fpc_systemDecRef);
  204. tkobject,
  205. tkrecord:
  206. recordrtti(data,typeinfo,@fpc_systemDecRef);
  207. tkDynArray:
  208. fpc_dynarray_decr_ref(PPointer(Data)^,TypeInfo);
  209. {$ifdef HASINTF}
  210. tkInterface:
  211. Intf_Decr_Ref(PPointer(Data)^);
  212. {$endif HASINTF}
  213. end;
  214. end;
  215. procedure fpc_finalize_array(data,typeinfo : pointer;count,size : longint); [Public,Alias:'FPC_FINALIZEARRAY']; {$ifdef hascompilerproc} compilerproc; {$endif}
  216. var
  217. i : longint;
  218. begin
  219. for i:=0 to count-1 do
  220. int_finalize(data+size*i,typeinfo);
  221. end;
  222. {
  223. $Log$
  224. Revision 1.19 2004-11-02 15:52:04 florian
  225. * fixed rtti reading of arrays for 64 bit
  226. Revision 1.18 2004/10/26 15:02:51 peter
  227. * rtti is always aligned, remove move() workarounds
  228. Revision 1.17 2004/10/24 21:39:42 peter
  229. * record and array parsing moved to procedure and handle like
  230. a data stream instead of using records
  231. Revision 1.16 2004/10/24 20:01:42 peter
  232. * saveregisters calling convention is obsolete
  233. Revision 1.15 2004/10/04 21:26:16 florian
  234. * rtti alignment fixed
  235. Revision 1.14 2004/08/18 21:03:35 florian
  236. * sparc uses wait4 as well
  237. Revision 1.13 2004/07/02 21:21:09 peter
  238. * decr ref doesn't reset pointer
  239. * finalize resets pointer for astring,wstring
  240. Revision 1.12 2004/05/31 20:25:04 peter
  241. * removed warnings
  242. Revision 1.11 2004/03/27 23:22:38 florian
  243. * fixed alignment issues
  244. Revision 1.10 2004/02/26 16:19:01 peter
  245. * tkclass removed from finalize()
  246. * cleanupinstance now parses the tkclass rtti entry itself and
  247. calls finalize() for the rtti members
  248. Revision 1.9 2004/02/26 12:42:34 michael
  249. + Patch from peter to fix finalize (bug 2975)
  250. Revision 1.8 2004/01/22 22:09:05 peter
  251. * finalize needs to reset to nil after decr_ref
  252. Revision 1.7 2002/09/07 15:07:46 peter
  253. * old logs removed and tabs fixed
  254. Revision 1.6 2002/09/02 18:42:41 peter
  255. * moved genrtti.inc code to rtti
  256. * removed rttip.inc, the generic code is almost as fast and
  257. much easier to maintain and has less risks on bugs
  258. }