2
0

rtti.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  124. tkDynArray,
  125. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  126. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  127. tkAstring,
  128. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  129. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  130. tkWstring,tkUString,
  131. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  132. tkInterface:
  133. PPchar(Data)^:=Nil;
  134. tkArray:
  135. arrayrtti(data,typeinfo,@int_initialize);
  136. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  137. tkObject,
  138. {$endif FPC_HAS_FEATURE_OBJECTS}
  139. tkRecord:
  140. recordrtti(data,typeinfo,@int_initialize);
  141. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  142. tkVariant:
  143. variant_init(PVarData(Data)^);
  144. {$endif FPC_HAS_FEATURE_VARIANTS}
  145. end;
  146. end;
  147. Procedure fpc_finalize (Data,TypeInfo: Pointer);[Public,Alias : 'FPC_FINALIZE']; compilerproc;
  148. begin
  149. case PByte(TypeInfo)^ of
  150. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  151. tkAstring :
  152. begin
  153. fpc_AnsiStr_Decr_Ref(PPointer(Data)^);
  154. PPointer(Data)^:=nil;
  155. end;
  156. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  157. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  158. {$ifndef VER2_2}
  159. tkUstring :
  160. begin
  161. fpc_UnicodeStr_Decr_Ref(PPointer(Data)^);
  162. PPointer(Data)^:=nil;
  163. end;
  164. {$endif VER2_2}
  165. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  166. tkWstring :
  167. begin
  168. fpc_WideStr_Decr_Ref(PPointer(Data)^);
  169. PPointer(Data)^:=nil;
  170. end;
  171. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  172. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  173. tkArray :
  174. arrayrtti(data,typeinfo,@int_finalize);
  175. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  176. tkObject,
  177. {$endif FPC_HAS_FEATURE_OBJECTS}
  178. tkRecord:
  179. recordrtti(data,typeinfo,@int_finalize);
  180. tkInterface:
  181. begin
  182. Intf_Decr_Ref(PPointer(Data)^);
  183. PPointer(Data)^:=nil;
  184. end;
  185. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  186. tkDynArray:
  187. begin
  188. fpc_dynarray_decr_ref(PPointer(Data)^,TypeInfo);
  189. PPointer(Data)^:=nil;
  190. end;
  191. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  192. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  193. tkVariant:
  194. variant_clear(PVarData(Data)^);
  195. {$endif FPC_HAS_FEATURE_VARIANTS}
  196. end;
  197. end;
  198. Procedure fpc_Addref (Data,TypeInfo : Pointer); [Public,alias : 'FPC_ADDREF']; compilerproc;
  199. begin
  200. case PByte(TypeInfo)^ of
  201. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  202. tkAstring :
  203. fpc_AnsiStr_Incr_Ref(PPointer(Data)^);
  204. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  205. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  206. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  207. tkWstring :
  208. fpc_WideStr_Incr_Ref(PPointer(Data)^);
  209. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  210. {$ifndef VER2_2}
  211. tkUstring :
  212. fpc_UnicodeStr_Incr_Ref(PPointer(Data)^);
  213. {$endif VER2_2}
  214. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  215. tkArray :
  216. arrayrtti(data,typeinfo,@int_addref);
  217. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  218. tkobject,
  219. {$endif FPC_HAS_FEATURE_OBJECTS}
  220. tkrecord :
  221. recordrtti(data,typeinfo,@int_addref);
  222. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  223. tkDynArray:
  224. fpc_dynarray_incr_ref(PPointer(Data)^);
  225. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  226. tkInterface:
  227. Intf_Incr_Ref(PPointer(Data)^);
  228. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  229. tkVariant:
  230. variant_addref(pvardata(Data)^);
  231. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  232. end;
  233. end;
  234. { alias for internal use }
  235. { we use another name else the compiler gets puzzled because of the wrong forward def }
  236. procedure fpc_systemDecRef (Data, TypeInfo : Pointer);[external name 'FPC_DECREF'];
  237. Procedure fpc_DecRef (Data, TypeInfo : Pointer);[Public,alias : 'FPC_DECREF']; compilerproc;
  238. begin
  239. case PByte(TypeInfo)^ of
  240. { see AddRef for comment about below construct (JM) }
  241. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  242. tkAstring:
  243. fpc_AnsiStr_Decr_Ref(PPointer(Data)^);
  244. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  245. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  246. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  247. tkWstring:
  248. fpc_WideStr_Decr_Ref(PPointer(Data)^);
  249. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  250. {$ifndef VER2_2}
  251. tkUString:
  252. fpc_UnicodeStr_Decr_Ref(PPointer(Data)^);
  253. {$endif VER2_2}
  254. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  255. tkArray:
  256. arrayrtti(data,typeinfo,@fpc_systemDecRef);
  257. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  258. tkobject,
  259. {$endif FPC_HAS_FEATURE_OBJECTS}
  260. tkrecord:
  261. recordrtti(data,typeinfo,@fpc_systemDecRef);
  262. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  263. tkDynArray:
  264. fpc_dynarray_decr_ref(PPointer(Data)^,TypeInfo);
  265. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  266. tkInterface:
  267. Intf_Decr_Ref(PPointer(Data)^);
  268. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  269. tkVariant:
  270. variant_clear(pvardata(data)^);
  271. {$endif FPC_HAS_FEATURE_VARIANTS}
  272. end;
  273. end;
  274. { define alias for internal use in the system unit }
  275. Function fpc_Copy_internal (Src, Dest, TypeInfo : Pointer) : SizeInt;[external name 'FPC_COPY'];
  276. Function fpc_Copy (Src, Dest, TypeInfo : Pointer) : SizeInt;[Public,alias : 'FPC_COPY']; compilerproc;
  277. var
  278. Temp : pbyte;
  279. namelen : byte;
  280. copiedsize,
  281. expectedoffset,
  282. count,
  283. offset,
  284. size,
  285. i : SizeInt;
  286. info : pointer;
  287. begin
  288. result:=sizeof(pointer);
  289. case PByte(TypeInfo)^ of
  290. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  291. tkAstring:
  292. begin
  293. fpc_AnsiStr_Incr_Ref(PPointer(Src)^);
  294. fpc_AnsiStr_Decr_Ref(PPointer(Dest)^);
  295. PPointer(Dest)^:=PPointer(Src)^;
  296. end;
  297. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  298. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  299. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  300. tkWstring:
  301. fpc_WideStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  302. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  303. {$ifndef VER2_2}
  304. tkUstring:
  305. fpc_UnicodeStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  306. {$endif VER2_2}
  307. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  308. tkArray:
  309. begin
  310. Temp:=PByte(TypeInfo);
  311. inc(Temp);
  312. { Skip Name }
  313. namelen:=Temp^;
  314. inc(temp,namelen+1);
  315. temp:=aligntoptr(temp);
  316. { Element size }
  317. size:=PSizeInt(Temp)^;
  318. inc(Temp,sizeof(Size));
  319. { Element count }
  320. Count:=PSizeInt(Temp)^;
  321. inc(Temp,sizeof(Count));
  322. Info:=PPointer(Temp)^;
  323. inc(Temp,sizeof(Info));
  324. { Process elements }
  325. for I:=0 to Count-1 do
  326. fpc_Copy_internal(Src+(I*size),Dest+(I*size),Info);
  327. Result:=size*count;
  328. end;
  329. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  330. tkobject,
  331. {$endif FPC_HAS_FEATURE_OBJECTS}
  332. tkrecord:
  333. begin
  334. Temp:=PByte(TypeInfo);
  335. inc(Temp);
  336. { Skip Name }
  337. namelen:=Temp^;
  338. inc(temp,namelen+1);
  339. temp:=aligntoptr(temp);
  340. Result:=plongint(temp)^;
  341. { Skip size }
  342. inc(Temp,4);
  343. { Element count }
  344. Count:=PLongint(Temp)^;
  345. inc(Temp,sizeof(longint));
  346. expectedoffset:=0;
  347. { Process elements with rtti }
  348. for i:=1 to count Do
  349. begin
  350. Info:=PPointer(Temp)^;
  351. inc(Temp,sizeof(Info));
  352. Offset:=PLongint(Temp)^;
  353. if Offset>expectedoffset then
  354. move((Src+expectedoffset)^,(Dest+expectedoffset)^,Offset-expectedoffset);
  355. inc(Temp,sizeof(longint));
  356. copiedsize:=fpc_Copy_internal(Src+Offset,Dest+Offset,Info);
  357. expectedoffset:=Offset+copiedsize;
  358. end;
  359. { elements remaining? }
  360. if result>expectedoffset then
  361. move((Src+expectedoffset)^,(Dest+expectedoffset)^,Result-expectedoffset);
  362. end;
  363. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  364. tkDynArray:
  365. begin
  366. fpc_dynarray_Incr_Ref(PPointer(Src)^);
  367. fpc_dynarray_Decr_Ref(PPointer(Dest)^,typeinfo);
  368. PPointer(Dest)^:=PPointer(Src)^;
  369. end;
  370. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  371. tkInterface:
  372. begin
  373. Intf_Incr_Ref(PPointer(Src)^);
  374. Intf_Decr_Ref(PPointer(Dest)^);
  375. PPointer(Dest)^:=PPointer(Src)^;
  376. end;
  377. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  378. tkVariant:
  379. begin
  380. VarCopyProc(pvardata(dest)^,pvardata(src)^);
  381. result:=sizeof(tvardata);
  382. end;
  383. {$endif FPC_HAS_FEATURE_VARIANTS}
  384. end;
  385. end;
  386. { For internal use by the compiler, because otherwise $x- can cause trouble. }
  387. { Generally disabling extended syntax checking for all compilerprocs may }
  388. { have unintended side-effects }
  389. procedure fpc_Copy_proc (Src, Dest, TypeInfo : Pointer);compilerproc; inline;
  390. begin
  391. fpc_copy_internal(src,dest,typeinfo);
  392. end;
  393. procedure fpc_finalize_array(data,typeinfo : pointer;count,size : SizeInt); [Public,Alias:'FPC_FINALIZEARRAY']; compilerproc;
  394. var
  395. i : SizeInt;
  396. begin
  397. if not(PByte(typeinfo)^ in [tkInteger,tkChar,tkEnumeration,tkFloat,tkSet,
  398. tkMethod,tkSString,tkLString,tkWChar,tkBool,tkInt64,tkQWord]) then
  399. for i:=0 to count-1 do
  400. int_finalize(data+size*i,typeinfo);
  401. end;