rtti.inc 10 KB

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