rtti.inc 13 KB

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