rtti.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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(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(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. {$ifdef HASVARIANT}
  188. tkVariant:
  189. variant_addref(pvardata(Data)^);
  190. {$endif HASVARIANT}
  191. end;
  192. end;
  193. { alias for internal use }
  194. { we use another name else the compiler gets puzzled because of the wrong forward def }
  195. procedure fpc_systemDecRef (Data, TypeInfo : Pointer);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[external name 'FPC_DECREF'];
  196. Procedure fpc_DecRef (Data, TypeInfo : Pointer);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[Public,alias : 'FPC_DECREF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  197. begin
  198. case PByte(TypeInfo)^ of
  199. { see AddRef for comment about below construct (JM) }
  200. tkAstring:
  201. fpc_AnsiStr_Decr_Ref(PPointer(Data)^);
  202. {$ifdef HASWIDESTRING}
  203. tkWstring:
  204. fpc_WideStr_Decr_Ref(PPointer(Data)^);
  205. {$endif HASWIDESTRING}
  206. tkArray:
  207. arrayrtti(data,typeinfo,@fpc_systemDecRef);
  208. tkobject,
  209. tkrecord:
  210. recordrtti(data,typeinfo,@fpc_systemDecRef);
  211. tkDynArray:
  212. fpc_dynarray_decr_ref(PPointer(Data)^,TypeInfo);
  213. {$ifdef HASINTF}
  214. tkInterface:
  215. Intf_Decr_Ref(PPointer(Data)^);
  216. {$endif HASINTF}
  217. {$ifdef HASVARIANT}
  218. tkVariant:
  219. variant_clear(pvardata(data)^);
  220. {$endif HASVARIANT}
  221. end;
  222. end;
  223. procedure fpc_finalize_array(data,typeinfo : pointer;count,size : longint); [Public,Alias:'FPC_FINALIZEARRAY']; {$ifdef hascompilerproc} compilerproc; {$endif}
  224. var
  225. i : longint;
  226. begin
  227. for i:=0 to count-1 do
  228. int_finalize(data+size*i,typeinfo);
  229. end;
  230. {
  231. $Log$
  232. Revision 1.22 2005-02-14 17:13:26 peter
  233. * truncate log
  234. Revision 1.21 2005/01/15 18:47:26 florian
  235. * several variant init./final. stuff fixed
  236. Revision 1.20 2005/01/08 20:43:44 florian
  237. + init/cleaning code for variants added
  238. }