rtti.inc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. { A record is designed as follows :
  39. 1 : tkrecord
  40. 2 : Length of name string (n);
  41. 3 : name string;
  42. 3+n : record size;
  43. 7+n : number of elements (N)
  44. 11+n : N times : Pointer to type info
  45. Offset in record
  46. }
  47. Type
  48. TRecElem = Record
  49. Info : Pointer;
  50. Offset : Longint;
  51. end;
  52. TRecElemArray = Array[1..Maxint] of TRecElem;
  53. PRecRec = ^TRecRec;
  54. TRecRec = record
  55. Size,Count : Longint;
  56. Elements : TRecElemArray;
  57. end;
  58. { An array is designed as follows :
  59. 1 : tkArray;
  60. 2 : length of name string (n);
  61. 3 : NAme string
  62. 3+n : Element Size
  63. 7+n : Number of elements
  64. 11+n : Pointer to type of elements
  65. }
  66. PArrayRec = ^TArrayRec;
  67. TArrayRec = record
  68. Size,Count : Longint;
  69. Info : Pointer;
  70. end;
  71. Procedure fpc_Initialize (Data,TypeInfo : pointer);saveregisters;[Public,Alias : 'FPC_INITIALIZE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  72. { this definition is sometimes (depending on switches)
  73. already defined or not so define it locally to avoid problems PM }
  74. Var Temp : PByte;
  75. I : longint;
  76. Size,Count : longint;
  77. TInfo : Pointer;
  78. begin
  79. Temp:=PByte(TypeInfo);
  80. case temp^ of
  81. tkAstring,tkWstring,tkInterface,tkDynArray:
  82. PPchar(Data)^:=Nil;
  83. tkArray:
  84. begin
  85. inc(temp);
  86. I:=temp^;
  87. inc(temp,(I+1)); // skip name string;
  88. Size:=PArrayRec(Temp)^.Size; // get element size
  89. Count:=PArrayRec(Temp)^.Count; // get element Count
  90. TInfo:=PArrayRec(Temp)^.Info; // Get element info
  91. For I:=0 to Count-1 do
  92. int_Initialize (Data+(I*size),TInfo);
  93. end;
  94. tkObject,
  95. tkRecord:
  96. begin
  97. inc(Temp);
  98. I:=Temp^;
  99. inc(temp,I+1); // skip name string;
  100. Count:=PRecRec(Temp)^.Count; // get element Count
  101. For I:=1 to count Do
  102. With PRecRec(Temp)^.elements[I] do
  103. int_Initialize (Data+Offset,Info);
  104. end;
  105. {$ifdef HASVARIANT}
  106. tkVariant:
  107. variant_init(Variant(PVarData(Data)^))
  108. {$endif HASVARIANT}
  109. end;
  110. end;
  111. Procedure fpc_finalize (Data,TypeInfo: Pointer);saveregisters;[Public,Alias : 'FPC_FINALIZE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  112. { this definition is sometimes (depending on switches)
  113. already defined or not so define it locally to avoid problems PM }
  114. Var Temp : PByte;
  115. I : longint;
  116. Size,Count : longint;
  117. TInfo : Pointer;
  118. begin
  119. Temp:=PByte(TypeInfo);
  120. case temp^ of
  121. tkAstring :
  122. fpc_AnsiStr_Decr_Ref(PPointer(Data)^);
  123. {$ifdef HASWIDESTRING}
  124. tkWstring :
  125. fpc_WideStr_Decr_Ref(PPointer(Data)^);
  126. {$endif HASWIDESTRING}
  127. tkArray :
  128. begin
  129. inc(Temp);
  130. I:=temp^;
  131. inc(temp,I+1); // skip name string;
  132. Size:=PArrayRec(Temp)^.Size; // get element size
  133. Count:=PArrayRec(Temp)^.Count; // get element Count
  134. TInfo:=PArrayRec(Temp)^.Info; // Get element info
  135. For I:=0 to Count-1 do
  136. int_Finalize (Data+(I*size),TInfo);
  137. end;
  138. tkObject,
  139. tkRecord:
  140. begin
  141. inc(Temp);
  142. I:=Temp^;
  143. inc(temp,I+1); // skip name string;
  144. Count:=PRecRec(Temp)^.Count; // get element Count
  145. For I:=1 to count do
  146. With PRecRec(Temp)^.elements[I] do
  147. int_Finalize (Data+Offset,Info);
  148. end;
  149. {$ifdef HASINTF}
  150. tkInterface:
  151. Intf_Decr_Ref(PPointer(Data)^);
  152. {$endif HASINTF}
  153. tkDynArray:
  154. fpc_dynarray_decr_ref(PPointer(Data)^,TypeInfo);
  155. {$ifdef HASVARIANT}
  156. tkVariant:
  157. variant_clear(Variant(PVarData(Data)^))
  158. {$endif HASVARIANT}
  159. end;
  160. end;
  161. Procedure fpc_Addref (Data,TypeInfo : Pointer);saveregisters; [Public,alias : 'FPC_ADDREF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  162. { this definition is sometimes (depending on switches)
  163. already defined or not so define it locally to avoid problems PM }
  164. Var Temp : PByte;
  165. I : longint;
  166. Size,Count : longint;
  167. TInfo : Pointer;
  168. begin
  169. Temp:=PByte(TypeInfo);
  170. case temp^ of
  171. tkAstring :
  172. fpc_AnsiStr_Incr_Ref(PPointer(Data)^);
  173. {$ifdef HASWIDESTRING}
  174. tkWstring :
  175. fpc_WideStr_Incr_Ref(PPointer(Data)^);
  176. {$endif HASWIDESTRING}
  177. tkArray :
  178. begin
  179. Inc(Temp);
  180. I:=temp^;
  181. inc(temp,I+1); // skip name string;
  182. Size:=PArrayRec(Temp)^.Size; // get element size
  183. Count:=PArrayRec(Temp)^.Count; // get element Count
  184. TInfo:=PArrayRec(Temp)^.Info; // Get element info
  185. For I:=0 to Count-1 do
  186. int_AddRef (Data+(I*size),TInfo);
  187. end;
  188. tkobject,
  189. tkrecord :
  190. begin
  191. Inc(Temp);
  192. I:=Temp^;
  193. temp:=temp+(I+1); // skip name string;
  194. Count:=PRecRec(Temp)^.Count; // get element Count
  195. For I:=1 to count do
  196. With PRecRec(Temp)^.elements[I] do
  197. int_AddRef (Data+Offset,Info);
  198. end;
  199. tkDynArray:
  200. fpc_dynarray_incr_ref(PPointer(Data)^);
  201. {$ifdef HASINTF}
  202. tkInterface:
  203. Intf_Incr_Ref(PPointer(Data)^);
  204. {$endif HASINTF}
  205. end;
  206. end;
  207. { alias for internal use }
  208. { we use another name else the compiler gets puzzled because of the wrong forward def }
  209. procedure fpc_systemDecRef (Data, TypeInfo : Pointer);saveregisters;[external name 'FPC_DECREF'];
  210. Procedure fpc_DecRef (Data, TypeInfo : Pointer);saveregisters;[Public,alias : 'FPC_DECREF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  211. { this definition is sometimes (depending on switches)
  212. already defined or not so define it locally to avoid problems PM }
  213. Var Temp : PByte;
  214. I : longint;
  215. Size,Count : longint;
  216. TInfo : Pointer;
  217. begin
  218. Temp:=PByte(TypeInfo);
  219. case temp^ of
  220. { see AddRef for comment about below construct (JM) }
  221. tkAstring:
  222. fpc_AnsiStr_Decr_Ref(PPointer(Data)^);
  223. {$ifdef HASWIDESTRING}
  224. tkWstring:
  225. fpc_WideStr_Decr_Ref(PPointer(Data)^);
  226. {$endif HASWIDESTRING}
  227. tkArray:
  228. begin
  229. inc(Temp);
  230. I:=temp^;
  231. inc(temp,I+1); // skip name string;
  232. Size:=PArrayRec(Temp)^.Size; // get element size
  233. Count:=PArrayRec(Temp)^.Count; // get element Count
  234. TInfo:=PArrayRec(Temp)^.Info; // Get element info
  235. For I:=0 to Count-1 do
  236. fpc_systemDecRef (Data+(I*size),TInfo);
  237. end;
  238. tkobject,
  239. tkrecord:
  240. begin
  241. inc(Temp);
  242. I:=temp^;
  243. inc(temp,I+1); // skip name string;
  244. Count:=PRecRec(Temp)^.Count; // get element Count
  245. For I:=1 to count do
  246. With PRecRec(Temp)^.elements[I] do
  247. fpc_systemDecRef (Data+Offset,Info);
  248. end;
  249. tkDynArray:
  250. fpc_dynarray_decr_ref(PPointer(Data)^,TypeInfo);
  251. {$ifdef HASINTF}
  252. tkInterface:
  253. Intf_Decr_Ref(PPointer(Data)^);
  254. {$endif HASINTF}
  255. end;
  256. end;
  257. procedure fpc_finalize_array(data,typeinfo : pointer;count,size : longint); [Public,Alias:'FPC_FINALIZEARRAY']; {$ifdef hascompilerproc} compilerproc; {$endif}
  258. var
  259. i : longint;
  260. begin
  261. for i:=0 to count-1 do
  262. int_finalize(data+size*i,typeinfo);
  263. end;
  264. {
  265. $Log$
  266. Revision 1.10 2004-02-26 16:19:01 peter
  267. * tkclass removed from finalize()
  268. * cleanupinstance now parses the tkclass rtti entry itself and
  269. calls finalize() for the rtti members
  270. Revision 1.9 2004/02/26 12:42:34 michael
  271. + Patch from peter to fix finalize (bug 2975)
  272. Revision 1.8 2004/01/22 22:09:05 peter
  273. * finalize needs to reset to nil after decr_ref
  274. Revision 1.7 2002/09/07 15:07:46 peter
  275. * old logs removed and tabs fixed
  276. Revision 1.6 2002/09/02 18:42:41 peter
  277. * moved genrtti.inc code to rtti
  278. * removed rttip.inc, the generic code is almost as fast and
  279. much easier to maintain and has less risks on bugs
  280. }