rtti.inc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. begin
  156. fpc_dynarray_decr_ref(PPointer(Data)^,TypeInfo);
  157. PPointer(Data)^:=nil;
  158. end;
  159. tkVariant:
  160. variant_clear(PVarData(Data)^);
  161. end;
  162. end;
  163. Procedure fpc_Addref (Data,TypeInfo : Pointer); [Public,alias : 'FPC_ADDREF']; compilerproc;
  164. begin
  165. case PByte(TypeInfo)^ of
  166. tkAstring :
  167. fpc_AnsiStr_Incr_Ref(PPointer(Data)^);
  168. tkWstring :
  169. fpc_WideStr_Incr_Ref(PPointer(Data)^);
  170. tkArray :
  171. arrayrtti(data,typeinfo,@int_addref);
  172. tkobject,
  173. tkrecord :
  174. recordrtti(data,typeinfo,@int_addref);
  175. tkDynArray:
  176. fpc_dynarray_incr_ref(PPointer(Data)^);
  177. tkInterface:
  178. Intf_Incr_Ref(PPointer(Data)^);
  179. tkVariant:
  180. variant_addref(pvardata(Data)^);
  181. end;
  182. end;
  183. { alias for internal use }
  184. { we use another name else the compiler gets puzzled because of the wrong forward def }
  185. procedure fpc_systemDecRef (Data, TypeInfo : Pointer);[external name 'FPC_DECREF'];
  186. Procedure fpc_DecRef (Data, TypeInfo : Pointer);[Public,alias : 'FPC_DECREF']; compilerproc;
  187. begin
  188. case PByte(TypeInfo)^ of
  189. { see AddRef for comment about below construct (JM) }
  190. tkAstring:
  191. fpc_AnsiStr_Decr_Ref(PPointer(Data)^);
  192. tkWstring:
  193. fpc_WideStr_Decr_Ref(PPointer(Data)^);
  194. tkArray:
  195. arrayrtti(data,typeinfo,@fpc_systemDecRef);
  196. tkobject,
  197. tkrecord:
  198. recordrtti(data,typeinfo,@fpc_systemDecRef);
  199. tkDynArray:
  200. fpc_dynarray_decr_ref(PPointer(Data)^,TypeInfo);
  201. tkInterface:
  202. Intf_Decr_Ref(PPointer(Data)^);
  203. tkVariant:
  204. variant_clear(pvardata(data)^);
  205. end;
  206. end;
  207. { define alias for internal use in the system unit }
  208. Function fpc_Copy_internal (Src, Dest, TypeInfo : Pointer) : SizeInt;[external name 'FPC_COPY'];
  209. Function fpc_Copy (Src, Dest, TypeInfo : Pointer) : SizeInt;[Public,alias : 'FPC_COPY']; compilerproc;
  210. var
  211. Temp : pbyte;
  212. namelen : byte;
  213. copiedsize,
  214. expectedoffset,
  215. count,
  216. offset,
  217. size,
  218. i : SizeInt;
  219. info : pointer;
  220. begin
  221. result:=sizeof(pointer);
  222. case PByte(TypeInfo)^ of
  223. tkAstring:
  224. begin
  225. fpc_AnsiStr_Incr_Ref(PPointer(Src)^);
  226. fpc_AnsiStr_Decr_Ref(PPointer(Dest)^);
  227. PPointer(Dest)^:=PPointer(Src)^;
  228. end;
  229. tkWstring:
  230. fpc_WideStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  231. tkArray:
  232. begin
  233. Temp:=PByte(TypeInfo);
  234. inc(Temp);
  235. { Skip Name }
  236. namelen:=Temp^;
  237. inc(temp,namelen+1);
  238. temp:=aligntoptr(temp);
  239. { Element size }
  240. size:=PSizeInt(Temp)^;
  241. inc(Temp,sizeof(Size));
  242. { Element count }
  243. Count:=PSizeInt(Temp)^;
  244. inc(Temp,sizeof(Count));
  245. Info:=PPointer(Temp)^;
  246. inc(Temp,sizeof(Info));
  247. { Process elements }
  248. for I:=0 to Count-1 do
  249. fpc_Copy_internal(Src+(I*size),Dest+(I*size),Info);
  250. Result:=size*count;
  251. end;
  252. tkobject,
  253. tkrecord:
  254. begin
  255. Temp:=PByte(TypeInfo);
  256. inc(Temp);
  257. { Skip Name }
  258. namelen:=Temp^;
  259. inc(temp,namelen+1);
  260. temp:=aligntoptr(temp);
  261. Result:=plongint(temp)^;
  262. { Skip size }
  263. inc(Temp,4);
  264. { Element count }
  265. Count:=PLongint(Temp)^;
  266. inc(Temp,sizeof(longint));
  267. expectedoffset:=0;
  268. { Process elements with rtti }
  269. for i:=1 to count Do
  270. begin
  271. Info:=PPointer(Temp)^;
  272. inc(Temp,sizeof(Info));
  273. Offset:=PLongint(Temp)^;
  274. if Offset>expectedoffset then
  275. move((Src+expectedoffset)^,(Dest+expectedoffset)^,Offset-expectedoffset);
  276. inc(Temp,sizeof(longint));
  277. copiedsize:=fpc_Copy_internal(Src+Offset,Dest+Offset,Info);
  278. expectedoffset:=Offset+copiedsize;
  279. end;
  280. { elements remaining? }
  281. if result>expectedoffset then
  282. move((Src+expectedoffset)^,(Dest+expectedoffset)^,Result-expectedoffset);
  283. end;
  284. tkDynArray:
  285. begin
  286. fpc_dynarray_Incr_Ref(PPointer(Src)^);
  287. fpc_dynarray_Decr_Ref(PPointer(Dest)^,typeinfo);
  288. PPointer(Dest)^:=PPointer(Src)^;
  289. end;
  290. tkInterface:
  291. begin
  292. Intf_Incr_Ref(PPointer(Src)^);
  293. Intf_Decr_Ref(PPointer(Dest)^);
  294. PPointer(Dest)^:=PPointer(Src)^;
  295. end;
  296. tkVariant:
  297. begin
  298. VarCopyProc(pvardata(dest)^,pvardata(src)^);
  299. result:=sizeof(tvardata);
  300. end;
  301. end;
  302. end;
  303. { For internal use by the compiler, because otherwise $x- can cause trouble. }
  304. { Generally disabling extended syntax checking for all compilerprocs may }
  305. { have unintended side-effects }
  306. procedure fpc_Copy_proc (Src, Dest, TypeInfo : Pointer);compilerproc; inline;
  307. begin
  308. fpc_copy_internal(src,dest,typeinfo);
  309. end;
  310. procedure fpc_finalize_array(data,typeinfo : pointer;count,size : longint); [Public,Alias:'FPC_FINALIZEARRAY']; compilerproc;
  311. var
  312. i : longint;
  313. begin
  314. for i:=0 to count-1 do
  315. int_finalize(data+size*i,typeinfo);
  316. end;