rtti.inc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Michael Van Canneyt
  4. member of the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. { Run-Time type information routines }
  12. { The RTTI is implemented through a series of constants : }
  13. Const
  14. tkUnknown = 0;
  15. tkInteger = 1;
  16. tkChar = 2;
  17. tkEnumeration = 3;
  18. tkFloat = 4;
  19. tkSet = 5;
  20. tkMethod = 6;
  21. tkSString = 7;
  22. tkString = tkSString;
  23. tkLString = 8;
  24. tkAString = 9;
  25. tkWString = 10;
  26. tkVariant = 11;
  27. tkArray = 12;
  28. tkRecord = 13;
  29. tkInterface = 14;
  30. tkClass = 15;
  31. tkObject = 16;
  32. tkWChar = 17;
  33. tkBool = 18;
  34. tkInt64 = 19;
  35. tkQWord = 20;
  36. tkDynArray = 21;
  37. type
  38. TRTTIProc=procedure(Data,TypeInfo:Pointer);
  39. { if you modify this procedure, fpc_copy must be probably modified as well }
  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. { if you modify this procedure, fpc_copy must be probably modified as well }
  81. procedure ArrayRTTI(Data,TypeInfo:Pointer;rttiproc:TRTTIProc);
  82. {
  83. An array is designed as follows :
  84. 1 : tkArray;
  85. 2 : length of name string (n);
  86. 3 : NAme string
  87. 3+n : Element Size
  88. 7+n : Number of elements
  89. 11+n : Pointer to type of elements
  90. }
  91. var
  92. Temp : pbyte;
  93. namelen : byte;
  94. count,
  95. size,
  96. i : SizeInt;
  97. info : pointer;
  98. begin
  99. Temp:=PByte(TypeInfo);
  100. inc(Temp);
  101. { Skip Name }
  102. namelen:=Temp^;
  103. inc(temp,namelen+1);
  104. temp:=aligntoptr(temp);
  105. { Element size }
  106. size:=PSizeInt(Temp)^;
  107. inc(Temp,sizeof(Size));
  108. { Element count }
  109. Count:=PSizeInt(Temp)^;
  110. inc(Temp,sizeof(Count));
  111. Info:=PPointer(Temp)^;
  112. inc(Temp,sizeof(Info));
  113. { Process elements }
  114. for I:=0 to Count-1 do
  115. rttiproc(Data+(I*size),Info);
  116. end;
  117. Procedure fpc_Initialize (Data,TypeInfo : pointer);[Public,Alias : 'FPC_INITIALIZE']; compilerproc;
  118. begin
  119. case PByte(TypeInfo)^ of
  120. tkAstring,tkWstring,tkInterface,tkDynArray:
  121. PPchar(Data)^:=Nil;
  122. tkArray:
  123. arrayrtti(data,typeinfo,@int_initialize);
  124. tkObject,
  125. tkRecord:
  126. recordrtti(data,typeinfo,@int_initialize);
  127. tkVariant:
  128. variant_init(PVarData(Data)^);
  129. end;
  130. end;
  131. Procedure fpc_finalize (Data,TypeInfo: Pointer);[Public,Alias : 'FPC_FINALIZE']; compilerproc;
  132. begin
  133. case PByte(TypeInfo)^ of
  134. tkAstring :
  135. begin
  136. fpc_AnsiStr_Decr_Ref(PPointer(Data)^);
  137. PPointer(Data)^:=nil;
  138. end;
  139. tkWstring :
  140. begin
  141. fpc_WideStr_Decr_Ref(PPointer(Data)^);
  142. PPointer(Data)^:=nil;
  143. end;
  144. tkArray :
  145. arrayrtti(data,typeinfo,@int_finalize);
  146. tkObject,
  147. tkRecord:
  148. recordrtti(data,typeinfo,@int_finalize);
  149. tkInterface:
  150. begin
  151. Intf_Decr_Ref(PPointer(Data)^);
  152. PPointer(Data)^:=nil;
  153. end;
  154. tkDynArray:
  155. fpc_dynarray_decr_ref(PPointer(Data)^,TypeInfo);
  156. tkVariant:
  157. variant_clear(PVarData(Data)^);
  158. end;
  159. end;
  160. Procedure fpc_Addref (Data,TypeInfo : Pointer); [Public,alias : 'FPC_ADDREF']; compilerproc;
  161. begin
  162. case PByte(TypeInfo)^ of
  163. tkAstring :
  164. fpc_AnsiStr_Incr_Ref(PPointer(Data)^);
  165. tkWstring :
  166. fpc_WideStr_Incr_Ref(PPointer(Data)^);
  167. tkArray :
  168. arrayrtti(data,typeinfo,@int_addref);
  169. tkobject,
  170. tkrecord :
  171. recordrtti(data,typeinfo,@int_addref);
  172. tkDynArray:
  173. fpc_dynarray_incr_ref(PPointer(Data)^);
  174. tkInterface:
  175. Intf_Incr_Ref(PPointer(Data)^);
  176. tkVariant:
  177. variant_addref(pvardata(Data)^);
  178. end;
  179. end;
  180. { alias for internal use }
  181. { we use another name else the compiler gets puzzled because of the wrong forward def }
  182. procedure fpc_systemDecRef (Data, TypeInfo : Pointer);[external name 'FPC_DECREF'];
  183. Procedure fpc_DecRef (Data, TypeInfo : Pointer);[Public,alias : 'FPC_DECREF']; compilerproc;
  184. begin
  185. case PByte(TypeInfo)^ of
  186. { see AddRef for comment about below construct (JM) }
  187. tkAstring:
  188. fpc_AnsiStr_Decr_Ref(PPointer(Data)^);
  189. tkWstring:
  190. fpc_WideStr_Decr_Ref(PPointer(Data)^);
  191. tkArray:
  192. arrayrtti(data,typeinfo,@fpc_systemDecRef);
  193. tkobject,
  194. tkrecord:
  195. recordrtti(data,typeinfo,@fpc_systemDecRef);
  196. tkDynArray:
  197. fpc_dynarray_decr_ref(PPointer(Data)^,TypeInfo);
  198. tkInterface:
  199. Intf_Decr_Ref(PPointer(Data)^);
  200. tkVariant:
  201. variant_clear(pvardata(data)^);
  202. end;
  203. end;
  204. { define alias for internal use in the system unit }
  205. Function fpc_Copy_internal (Src, Dest, TypeInfo : Pointer) : SizeInt;[external name 'FPC_COPY'];
  206. Function fpc_Copy (Src, Dest, TypeInfo : Pointer) : SizeInt;[Public,alias : 'FPC_COPY']; compilerproc;
  207. var
  208. Temp : pbyte;
  209. namelen : byte;
  210. copiedsize,
  211. expectedoffset,
  212. count,
  213. offset,
  214. size,
  215. i : SizeInt;
  216. info : pointer;
  217. begin
  218. result:=sizeof(pointer);
  219. case PByte(TypeInfo)^ of
  220. tkAstring:
  221. begin
  222. fpc_AnsiStr_Incr_Ref(PPointer(Src)^);
  223. fpc_AnsiStr_Decr_Ref(PPointer(Dest)^);
  224. PPointer(Dest)^:=PPointer(Src)^;
  225. end;
  226. tkWstring:
  227. fpc_WideStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  228. tkArray:
  229. begin
  230. Temp:=PByte(TypeInfo);
  231. inc(Temp);
  232. { Skip Name }
  233. namelen:=Temp^;
  234. inc(temp,namelen+1);
  235. temp:=aligntoptr(temp);
  236. { Element size }
  237. size:=PSizeInt(Temp)^;
  238. inc(Temp,sizeof(Size));
  239. { Element count }
  240. Count:=PSizeInt(Temp)^;
  241. inc(Temp,sizeof(Count));
  242. Info:=PPointer(Temp)^;
  243. inc(Temp,sizeof(Info));
  244. { Process elements }
  245. for I:=0 to Count-1 do
  246. fpc_Copy_internal(Src+(I*size),Dest+(I*size),Info);
  247. Result:=size*count;
  248. end;
  249. tkobject,
  250. tkrecord:
  251. begin
  252. Temp:=PByte(TypeInfo);
  253. inc(Temp);
  254. { Skip Name }
  255. namelen:=Temp^;
  256. inc(temp,namelen+1);
  257. temp:=aligntoptr(temp);
  258. Result:=plongint(temp)^;
  259. { Skip size }
  260. inc(Temp,4);
  261. { Element count }
  262. Count:=PLongint(Temp)^;
  263. inc(Temp,sizeof(longint));
  264. expectedoffset:=0;
  265. { Process elements with rtti }
  266. for i:=1 to count Do
  267. begin
  268. Info:=PPointer(Temp)^;
  269. inc(Temp,sizeof(Info));
  270. Offset:=PLongint(Temp)^;
  271. if Offset>expectedoffset then
  272. move((Src+expectedoffset)^,(Dest+expectedoffset)^,Offset-expectedoffset);
  273. inc(Temp,sizeof(longint));
  274. copiedsize:=fpc_Copy_internal(Src+Offset,Dest+Offset,Info);
  275. expectedoffset:=Offset+copiedsize;
  276. end;
  277. { elements remaining? }
  278. if result>expectedoffset then
  279. move((Src+expectedoffset)^,(Dest+expectedoffset)^,Result-expectedoffset);
  280. end;
  281. tkDynArray:
  282. begin
  283. fpc_dynarray_Incr_Ref(PPointer(Src)^);
  284. fpc_dynarray_Decr_Ref(PPointer(Dest)^,typeinfo);
  285. PPointer(Dest)^:=PPointer(Src)^;
  286. end;
  287. tkInterface:
  288. begin
  289. Intf_Incr_Ref(PPointer(Src)^);
  290. Intf_Decr_Ref(PPointer(Dest)^);
  291. PPointer(Dest)^:=PPointer(Src)^;
  292. end;
  293. tkVariant:
  294. begin
  295. VarCopyProc(pvardata(dest)^,pvardata(src)^);
  296. result:=sizeof(tvardata);
  297. end;
  298. end;
  299. end;
  300. procedure fpc_finalize_array(data,typeinfo : pointer;count,size : longint); [Public,Alias:'FPC_FINALIZEARRAY']; compilerproc;
  301. var
  302. i : longint;
  303. begin
  304. for i:=0 to count-1 do
  305. int_finalize(data+size*i,typeinfo);
  306. end;