rtti.inc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. tkInterfaceCorba = 22;
  38. tkProcVar = 23;
  39. tkUString = 24;
  40. type
  41. TRTTIProc=procedure(Data,TypeInfo:Pointer);
  42. { if you modify this procedure, fpc_copy must be probably modified as well }
  43. procedure RecordRTTI(Data,TypeInfo:Pointer;rttiproc:TRTTIProc);
  44. {
  45. A record is designed as follows :
  46. 1 : tkrecord
  47. 2 : Length of name string (n);
  48. 3 : name string;
  49. 3+n : record size;
  50. 7+n : number of elements (N)
  51. 11+n : N times : Pointer to type info
  52. Offset in record
  53. }
  54. var
  55. Temp : pbyte;
  56. namelen : byte;
  57. count,
  58. offset,
  59. i : longint;
  60. info : pointer;
  61. begin
  62. Temp:=PByte(TypeInfo);
  63. inc(Temp);
  64. { Skip Name }
  65. namelen:=Temp^;
  66. inc(temp,namelen+1);
  67. temp:=aligntoptr(temp);
  68. { Skip size }
  69. inc(Temp,4);
  70. { Element count }
  71. Count:=PLongint(Temp)^;
  72. inc(Temp,sizeof(Count));
  73. { Process elements }
  74. for i:=1 to count Do
  75. begin
  76. Info:=PPointer(Temp)^;
  77. inc(Temp,sizeof(Info));
  78. Offset:=PLongint(Temp)^;
  79. inc(Temp,sizeof(Offset));
  80. rttiproc (Data+Offset,Info);
  81. end;
  82. end;
  83. { if you modify this procedure, fpc_copy must be probably modified as well }
  84. procedure ArrayRTTI(Data,TypeInfo:Pointer;rttiproc:TRTTIProc);
  85. {
  86. An array is designed as follows :
  87. 1 : tkArray;
  88. 2 : length of name string (n);
  89. 3 : NAme string
  90. 3+n : Element Size
  91. 7+n : Number of elements
  92. 11+n : Pointer to type of elements
  93. }
  94. var
  95. Temp : pbyte;
  96. namelen : byte;
  97. count,
  98. size,
  99. i : SizeInt;
  100. info : pointer;
  101. begin
  102. Temp:=PByte(TypeInfo);
  103. inc(Temp);
  104. { Skip Name }
  105. namelen:=Temp^;
  106. inc(temp,namelen+1);
  107. temp:=aligntoptr(temp);
  108. { Element size }
  109. size:=PSizeInt(Temp)^;
  110. inc(Temp,sizeof(Size));
  111. { Element count }
  112. Count:=PSizeInt(Temp)^;
  113. inc(Temp,sizeof(Count));
  114. Info:=PPointer(Temp)^;
  115. inc(Temp,sizeof(Info));
  116. { Process elements }
  117. for I:=0 to Count-1 do
  118. rttiproc(Data+(I*size),Info);
  119. end;
  120. Procedure fpc_Initialize (Data,TypeInfo : pointer);[Public,Alias : 'FPC_INITIALIZE']; compilerproc;
  121. begin
  122. case PByte(TypeInfo)^ of
  123. tkAstring,tkWstring,tkUString,tkInterface,tkDynArray:
  124. PPchar(Data)^:=Nil;
  125. tkArray:
  126. arrayrtti(data,typeinfo,@int_initialize);
  127. tkObject,
  128. tkRecord:
  129. recordrtti(data,typeinfo,@int_initialize);
  130. tkVariant:
  131. variant_init(PVarData(Data)^);
  132. end;
  133. end;
  134. Procedure fpc_finalize (Data,TypeInfo: Pointer);[Public,Alias : 'FPC_FINALIZE']; compilerproc;
  135. begin
  136. case PByte(TypeInfo)^ of
  137. tkAstring :
  138. begin
  139. fpc_AnsiStr_Decr_Ref(PPointer(Data)^);
  140. PPointer(Data)^:=nil;
  141. end;
  142. {$ifndef VER2_2}
  143. tkUstring :
  144. begin
  145. fpc_UnicodeStr_Decr_Ref(PPointer(Data)^);
  146. PPointer(Data)^:=nil;
  147. end;
  148. {$endif VER2_2}
  149. {$ifdef WINDOWS}
  150. tkWstring :
  151. begin
  152. fpc_WideStr_Decr_Ref(PPointer(Data)^);
  153. PPointer(Data)^:=nil;
  154. end;
  155. {$endif WINDOWS}
  156. tkArray :
  157. arrayrtti(data,typeinfo,@int_finalize);
  158. tkObject,
  159. tkRecord:
  160. recordrtti(data,typeinfo,@int_finalize);
  161. tkInterface:
  162. begin
  163. Intf_Decr_Ref(PPointer(Data)^);
  164. PPointer(Data)^:=nil;
  165. end;
  166. tkDynArray:
  167. fpc_dynarray_decr_ref(PPointer(Data)^,TypeInfo);
  168. tkVariant:
  169. variant_clear(PVarData(Data)^);
  170. end;
  171. end;
  172. Procedure fpc_Addref (Data,TypeInfo : Pointer); [Public,alias : 'FPC_ADDREF']; compilerproc;
  173. begin
  174. case PByte(TypeInfo)^ of
  175. tkAstring :
  176. fpc_AnsiStr_Incr_Ref(PPointer(Data)^);
  177. {$ifdef WINDOWS}
  178. tkWstring :
  179. fpc_WideStr_Incr_Ref(PPointer(Data)^);
  180. {$endif WINDOWS}
  181. {$ifndef VER2_2}
  182. tkUstring :
  183. fpc_UnicodeStr_Incr_Ref(PPointer(Data)^);
  184. {$endif VER2_2}
  185. tkArray :
  186. arrayrtti(data,typeinfo,@int_addref);
  187. tkobject,
  188. tkrecord :
  189. recordrtti(data,typeinfo,@int_addref);
  190. tkDynArray:
  191. fpc_dynarray_incr_ref(PPointer(Data)^);
  192. tkInterface:
  193. Intf_Incr_Ref(PPointer(Data)^);
  194. tkVariant:
  195. variant_addref(pvardata(Data)^);
  196. end;
  197. end;
  198. { alias for internal use }
  199. { we use another name else the compiler gets puzzled because of the wrong forward def }
  200. procedure fpc_systemDecRef (Data, TypeInfo : Pointer);[external name 'FPC_DECREF'];
  201. Procedure fpc_DecRef (Data, TypeInfo : Pointer);[Public,alias : 'FPC_DECREF']; compilerproc;
  202. begin
  203. case PByte(TypeInfo)^ of
  204. { see AddRef for comment about below construct (JM) }
  205. tkAstring:
  206. fpc_AnsiStr_Decr_Ref(PPointer(Data)^);
  207. {$ifdef WINDOWS}
  208. tkWstring:
  209. fpc_WideStr_Decr_Ref(PPointer(Data)^);
  210. {$endif WINDOWS}
  211. {$ifndef VER2_2}
  212. tkUString:
  213. fpc_UnicodeStr_Decr_Ref(PPointer(Data)^);
  214. {$endif VER2_2}
  215. tkArray:
  216. arrayrtti(data,typeinfo,@fpc_systemDecRef);
  217. tkobject,
  218. tkrecord:
  219. recordrtti(data,typeinfo,@fpc_systemDecRef);
  220. tkDynArray:
  221. fpc_dynarray_decr_ref(PPointer(Data)^,TypeInfo);
  222. tkInterface:
  223. Intf_Decr_Ref(PPointer(Data)^);
  224. tkVariant:
  225. variant_clear(pvardata(data)^);
  226. end;
  227. end;
  228. { define alias for internal use in the system unit }
  229. Function fpc_Copy_internal (Src, Dest, TypeInfo : Pointer) : SizeInt;[external name 'FPC_COPY'];
  230. Function fpc_Copy (Src, Dest, TypeInfo : Pointer) : SizeInt;[Public,alias : 'FPC_COPY']; compilerproc;
  231. var
  232. Temp : pbyte;
  233. namelen : byte;
  234. copiedsize,
  235. expectedoffset,
  236. count,
  237. offset,
  238. size,
  239. i : SizeInt;
  240. info : pointer;
  241. begin
  242. result:=sizeof(pointer);
  243. case PByte(TypeInfo)^ of
  244. tkAstring:
  245. begin
  246. fpc_AnsiStr_Incr_Ref(PPointer(Src)^);
  247. fpc_AnsiStr_Decr_Ref(PPointer(Dest)^);
  248. PPointer(Dest)^:=PPointer(Src)^;
  249. end;
  250. {$ifdef WINDOWS}
  251. tkWstring:
  252. fpc_WideStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  253. {$endif WINDOWS}
  254. {$ifndef VER2_2}
  255. tkUstring:
  256. fpc_UnicodeStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  257. {$endif VER2_2}
  258. tkArray:
  259. begin
  260. Temp:=PByte(TypeInfo);
  261. inc(Temp);
  262. { Skip Name }
  263. namelen:=Temp^;
  264. inc(temp,namelen+1);
  265. temp:=aligntoptr(temp);
  266. { Element size }
  267. size:=PSizeInt(Temp)^;
  268. inc(Temp,sizeof(Size));
  269. { Element count }
  270. Count:=PSizeInt(Temp)^;
  271. inc(Temp,sizeof(Count));
  272. Info:=PPointer(Temp)^;
  273. inc(Temp,sizeof(Info));
  274. { Process elements }
  275. for I:=0 to Count-1 do
  276. fpc_Copy_internal(Src+(I*size),Dest+(I*size),Info);
  277. Result:=size*count;
  278. end;
  279. tkobject,
  280. tkrecord:
  281. begin
  282. Temp:=PByte(TypeInfo);
  283. inc(Temp);
  284. { Skip Name }
  285. namelen:=Temp^;
  286. inc(temp,namelen+1);
  287. temp:=aligntoptr(temp);
  288. Result:=plongint(temp)^;
  289. { Skip size }
  290. inc(Temp,4);
  291. { Element count }
  292. Count:=PLongint(Temp)^;
  293. inc(Temp,sizeof(longint));
  294. expectedoffset:=0;
  295. { Process elements with rtti }
  296. for i:=1 to count Do
  297. begin
  298. Info:=PPointer(Temp)^;
  299. inc(Temp,sizeof(Info));
  300. Offset:=PLongint(Temp)^;
  301. if Offset>expectedoffset then
  302. move((Src+expectedoffset)^,(Dest+expectedoffset)^,Offset-expectedoffset);
  303. inc(Temp,sizeof(longint));
  304. copiedsize:=fpc_Copy_internal(Src+Offset,Dest+Offset,Info);
  305. expectedoffset:=Offset+copiedsize;
  306. end;
  307. { elements remaining? }
  308. if result>expectedoffset then
  309. move((Src+expectedoffset)^,(Dest+expectedoffset)^,Result-expectedoffset);
  310. end;
  311. tkDynArray:
  312. begin
  313. fpc_dynarray_Incr_Ref(PPointer(Src)^);
  314. fpc_dynarray_Decr_Ref(PPointer(Dest)^,typeinfo);
  315. PPointer(Dest)^:=PPointer(Src)^;
  316. end;
  317. tkInterface:
  318. begin
  319. Intf_Incr_Ref(PPointer(Src)^);
  320. Intf_Decr_Ref(PPointer(Dest)^);
  321. PPointer(Dest)^:=PPointer(Src)^;
  322. end;
  323. tkVariant:
  324. begin
  325. VarCopyProc(pvardata(dest)^,pvardata(src)^);
  326. result:=sizeof(tvardata);
  327. end;
  328. end;
  329. end;
  330. { For internal use by the compiler, because otherwise $x- can cause trouble. }
  331. { Generally disabling extended syntax checking for all compilerprocs may }
  332. { have unintended side-effects }
  333. procedure fpc_Copy_proc (Src, Dest, TypeInfo : Pointer);compilerproc; inline;
  334. begin
  335. fpc_copy_internal(src,dest,typeinfo);
  336. end;
  337. procedure fpc_finalize_array(data,typeinfo : pointer;count,size : SizeInt); [Public,Alias:'FPC_FINALIZEARRAY']; compilerproc;
  338. var
  339. i : SizeInt;
  340. begin
  341. if not(PByte(typeinfo)^ in [tkInteger,tkChar,tkEnumeration,tkFloat,tkSet,
  342. tkMethod,tkSString,tkLString,tkWChar,tkBool,tkInt64,tkQWord]) then
  343. for i:=0 to count-1 do
  344. int_finalize(data+size*i,typeinfo);
  345. end;