rtti.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  14. {$define USE_PACKED}
  15. {$endif}
  16. {$ifdef VER2_6}
  17. {$define USE_PACKED}
  18. {$endif}
  19. type
  20. PRecordElement=^TRecordElement;
  21. TRecordElement=
  22. {$ifdef USE_PACKED}
  23. packed
  24. {$endif USE_PACKED}
  25. record
  26. {$ifdef VER3_0}
  27. TypeInfo: Pointer;
  28. {$else}
  29. TypeInfo: PPointer;
  30. {$endif}
  31. {$ifdef VER2_6}
  32. Offset: Longint;
  33. {$else}
  34. Offset: SizeInt;
  35. {$endif}
  36. end;
  37. PRecordInfoFull=^TRecordInfoFull;
  38. TRecordInfoFull=
  39. {$ifdef USE_PACKED}
  40. packed
  41. {$endif USE_PACKED}
  42. record
  43. {$ifndef VER3_0}
  44. InitTable: PPointer;
  45. {$endif VER3_0}
  46. Size: Longint;
  47. Count: Longint;
  48. { Elements: array[count] of TRecordElement }
  49. end;
  50. PRecordInfoInit=^TRecordInfoInit;
  51. {$ifndef VER3_0}
  52. TRecordInfoInit=
  53. {$ifdef USE_PACKED}
  54. packed
  55. {$endif USE_PACKED}
  56. record
  57. Terminator: Pointer;
  58. Size: Longint;
  59. Count: Longint;
  60. { Elements: array[count] of TRecordElement }
  61. end;
  62. {$else VER3_0}
  63. TRecordInfoInit=TRecordInfoFull;
  64. {$endif VER3_0}
  65. PArrayInfo=^TArrayInfo;
  66. TArrayInfo=
  67. {$ifdef USE_PACKED}
  68. packed
  69. {$endif USE_PACKED}
  70. record
  71. Size: SizeInt;
  72. ElCount: SizeInt;
  73. {$ifdef VER3_0}
  74. ElInfo: Pointer;
  75. {$else}
  76. ElInfo: PPointer;
  77. {$endif}
  78. DimCount: Byte;
  79. Dims:array[0..255] of Pointer;
  80. end;
  81. function RTTIArraySize(typeInfo: Pointer): SizeInt;
  82. begin
  83. typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  84. {$ifdef VER2_6}
  85. result:=PArrayInfo(typeInfo)^.Size*PArrayInfo(typeInfo)^.ElCount;
  86. {$else}
  87. result:=PArrayInfo(typeInfo)^.Size;
  88. {$endif}
  89. end;
  90. function RTTIRecordSize(typeInfo: Pointer): SizeInt;
  91. begin
  92. typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  93. { for size field init table is compatible with rtti table }
  94. result:=PRecordInfoFull(typeInfo)^.Size;
  95. end;
  96. function RTTIRecordRttiInfoToInitInfo(typeInfo: Pointer): Pointer; inline;
  97. begin
  98. result:=typeInfo;
  99. {$ifndef VER3_0}
  100. { find init table }
  101. typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  102. { check terminator, maybe we are already in init table }
  103. if Assigned(PRecordInfoInit(typeInfo)^.Terminator) then
  104. { point to more optimal initrtti }
  105. result:=PRecordInfoFull(result)^.InitTable^;
  106. {$endif VER3_0}
  107. end;
  108. function RTTISize(typeInfo: Pointer): SizeInt;
  109. begin
  110. case PByte(typeinfo)^ of
  111. tkAString,tkWString,tkUString,
  112. tkInterface,tkDynarray:
  113. result:=sizeof(Pointer);
  114. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  115. tkVariant:
  116. result:=sizeof(TVarData);
  117. {$endif FPC_HAS_FEATURE_VARIANTS}
  118. tkArray:
  119. result:=RTTIArraySize(typeinfo);
  120. tkObject,tkRecord:
  121. result:=RTTIRecordSize(typeinfo);
  122. else
  123. result:=-1;
  124. end;
  125. end;
  126. { if you modify this procedure, fpc_copy must be probably modified as well }
  127. procedure RecordRTTI(Data,TypeInfo:Pointer;rttiproc:TRTTIProc);
  128. var
  129. count,
  130. i : longint;
  131. begin
  132. typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  133. Count:=PRecordInfoInit(typeInfo)^.Count;
  134. Inc(PRecordInfoInit(typeInfo));
  135. { Process elements }
  136. for i:=1 to count Do
  137. begin
  138. rttiproc(Data+PRecordElement(typeInfo)^.Offset,PRecordElement(typeInfo)^.TypeInfo{$ifndef VER3_0}^{$endif});
  139. Inc(PRecordElement(typeInfo));
  140. end;
  141. end;
  142. { if you modify this procedure, fpc_copy must be probably modified as well }
  143. {$ifdef VER2_6}
  144. procedure ArrayRTTI(Data,TypeInfo:Pointer;rttiproc:TRTTIProc);
  145. var
  146. i: SizeInt;
  147. begin
  148. typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  149. { Process elements }
  150. for I:=0 to PArrayInfo(typeInfo)^.ElCount-1 do
  151. rttiproc(Data+(I*PArrayInfo(typeInfo)^.Size),PArrayInfo(typeInfo)^.ElInfo);
  152. end;
  153. {$else}
  154. procedure ArrayRTTI(Data,TypeInfo:Pointer;rttiproc:TRTTIProc);
  155. var
  156. i,Count,ElSize: SizeInt;
  157. Info: Pointer;
  158. begin
  159. typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  160. Count:=PArrayInfo(typeInfo)^.ElCount;
  161. { no elements to process => exit }
  162. if Count = 0 then
  163. Exit;
  164. ElSize:=PArrayInfo(typeInfo)^.Size div Count;
  165. Info:=PArrayInfo(typeInfo)^.ElInfo{$ifndef VER3_0}^{$endif};
  166. { Process elements }
  167. for I:=0 to Count-1 do
  168. rttiproc(Data+(I*ElSize),Info);
  169. end;
  170. {$endif}
  171. Procedure fpc_Initialize (Data,TypeInfo : pointer);[Public,Alias : 'FPC_INITIALIZE']; compilerproc;
  172. begin
  173. case PByte(TypeInfo)^ of
  174. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  175. tkDynArray,
  176. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  177. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  178. tkAstring,
  179. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  180. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  181. tkWstring,tkUString,
  182. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  183. tkInterface:
  184. PPchar(Data)^:=Nil;
  185. tkArray:
  186. arrayrtti(data,typeinfo,@int_initialize);
  187. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  188. tkObject,
  189. {$endif FPC_HAS_FEATURE_OBJECTS}
  190. tkRecord:
  191. begin
  192. typeinfo:=RTTIRecordRttiInfoToInitInfo(typeinfo);
  193. recordrtti(data,typeinfo,@int_initialize);
  194. end;
  195. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  196. tkVariant:
  197. variant_init(PVarData(Data)^);
  198. {$endif FPC_HAS_FEATURE_VARIANTS}
  199. end;
  200. end;
  201. Procedure fpc_finalize (Data,TypeInfo: Pointer);[Public,Alias : 'FPC_FINALIZE']; compilerproc;
  202. begin
  203. case PByte(TypeInfo)^ of
  204. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  205. tkAstring :
  206. fpc_AnsiStr_Decr_Ref(PPointer(Data)^);
  207. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  208. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  209. tkUstring :
  210. fpc_UnicodeStr_Decr_Ref(PPointer(Data)^);
  211. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  212. tkWstring :
  213. fpc_WideStr_Decr_Ref(PPointer(Data)^);
  214. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  215. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  216. tkArray :
  217. arrayrtti(data,typeinfo,@int_finalize);
  218. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  219. tkObject,
  220. {$endif FPC_HAS_FEATURE_OBJECTS}
  221. tkRecord:
  222. begin
  223. typeinfo:=RTTIRecordRttiInfoToInitInfo(typeinfo);
  224. recordrtti(data,typeinfo,@int_finalize);
  225. end;
  226. tkInterface:
  227. Intf_Decr_Ref(PPointer(Data)^);
  228. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  229. tkDynArray:
  230. fpc_dynarray_clear(PPointer(Data)^,TypeInfo);
  231. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  232. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  233. tkVariant:
  234. variant_clear(PVarData(Data)^);
  235. {$endif FPC_HAS_FEATURE_VARIANTS}
  236. end;
  237. end;
  238. Procedure fpc_Addref (Data,TypeInfo : Pointer); [Public,alias : 'FPC_ADDREF']; compilerproc;
  239. begin
  240. case PByte(TypeInfo)^ of
  241. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  242. tkAstring :
  243. fpc_AnsiStr_Incr_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_Incr_Ref(PPointer(Data)^);
  249. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  250. tkUstring :
  251. fpc_UnicodeStr_Incr_Ref(PPointer(Data)^);
  252. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  253. tkArray :
  254. arrayrtti(data,typeinfo,@int_addref);
  255. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  256. tkobject,
  257. {$endif FPC_HAS_FEATURE_OBJECTS}
  258. tkrecord :
  259. begin
  260. typeinfo:=RTTIRecordRttiInfoToInitInfo(typeinfo);
  261. recordrtti(data,typeinfo,@int_addref);
  262. end;
  263. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  264. tkDynArray:
  265. fpc_dynarray_incr_ref(PPointer(Data)^);
  266. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  267. tkInterface:
  268. Intf_Incr_Ref(PPointer(Data)^);
  269. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  270. tkVariant:
  271. variant_addref(pvardata(Data)^);
  272. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  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. copiedsize,
  281. expectedoffset,
  282. count,
  283. offset,
  284. i: SizeInt;
  285. info: pointer;
  286. begin
  287. result:=sizeof(pointer);
  288. case PByte(TypeInfo)^ of
  289. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  290. tkAstring:
  291. fpc_AnsiStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  292. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  293. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  294. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  295. tkWstring:
  296. fpc_WideStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  297. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  298. tkUstring:
  299. fpc_UnicodeStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  300. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  301. tkArray:
  302. begin
  303. Temp:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  304. {$ifdef VER2_6}
  305. { Process elements }
  306. for I:=0 to PArrayInfo(Temp)^.ElCount-1 do
  307. fpc_Copy_internal(Src+(I*PArrayInfo(Temp)^.Size),Dest+(I*PArrayInfo(Temp)^.Size),PArrayInfo(Temp)^.ElInfo);
  308. Result:=PArrayInfo(Temp)^.Size*PArrayInfo(Temp)^.ElCount;
  309. {$else}
  310. Result:=PArrayInfo(Temp)^.Size;
  311. Count:=PArrayInfo(Temp)^.ElCount;
  312. { no elements to process => exit }
  313. if Count = 0 then
  314. Exit;
  315. Info:=PArrayInfo(Temp)^.ElInfo{$ifndef VER3_0}^{$endif};
  316. copiedsize:=Result div Count;
  317. Offset:=0;
  318. { Process elements }
  319. for I:=1 to Count do
  320. begin
  321. fpc_Copy_internal(Src+Offset,Dest+Offset,Info);
  322. inc(Offset,copiedsize);
  323. end;
  324. {$endif}
  325. end;
  326. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  327. tkobject,
  328. {$endif FPC_HAS_FEATURE_OBJECTS}
  329. tkrecord:
  330. begin
  331. typeInfo:=RTTIRecordRttiInfoToInitInfo(typeInfo);
  332. Temp:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  333. Result:=PRecordInfoInit(Temp)^.Size;
  334. Count:=PRecordInfoInit(Temp)^.Count;
  335. Inc(PRecordInfoInit(Temp));
  336. expectedoffset:=0;
  337. { Process elements with rtti }
  338. for i:=1 to count Do
  339. begin
  340. Info:=PRecordElement(Temp)^.TypeInfo{$ifndef VER3_0}^{$endif};
  341. Offset:=PRecordElement(Temp)^.Offset;
  342. Inc(PRecordElement(Temp));
  343. if Offset>expectedoffset then
  344. move((Src+expectedoffset)^,(Dest+expectedoffset)^,Offset-expectedoffset);
  345. copiedsize:=fpc_Copy_internal(Src+Offset,Dest+Offset,Info);
  346. expectedoffset:=Offset+copiedsize;
  347. end;
  348. { elements remaining? }
  349. if result>expectedoffset then
  350. move((Src+expectedoffset)^,(Dest+expectedoffset)^,Result-expectedoffset);
  351. end;
  352. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  353. tkDynArray:
  354. fpc_dynarray_assign(PPointer(Dest)^,PPointer(Src)^,typeinfo);
  355. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  356. tkInterface:
  357. fpc_intf_assign(PPointer(Dest)^,PPointer(Src)^);
  358. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  359. tkVariant:
  360. begin
  361. VarCopyProc(pvardata(dest)^,pvardata(src)^);
  362. result:=sizeof(tvardata);
  363. end;
  364. {$endif FPC_HAS_FEATURE_VARIANTS}
  365. end;
  366. end;
  367. { For internal use by the compiler, because otherwise $x- can cause trouble. }
  368. { Generally disabling extended syntax checking for all compilerprocs may }
  369. { have unintended side-effects }
  370. procedure fpc_Copy_proc (Src, Dest, TypeInfo : Pointer);compilerproc; inline;
  371. begin
  372. fpc_copy_internal(src,dest,typeinfo);
  373. end;
  374. procedure fpc_initialize_array(data,typeinfo : pointer;count : SizeInt); [public,alias:'FPC_INITIALIZE_ARRAY']; compilerproc;
  375. var
  376. i, size : SizeInt;
  377. begin
  378. size:=RTTISize(typeinfo);
  379. if size>0 then
  380. for i:=0 to count-1 do
  381. int_initialize(data+size*i,typeinfo);
  382. end;
  383. procedure fpc_finalize_array(data,typeinfo : pointer;count : SizeInt); [Public,Alias:'FPC_FINALIZE_ARRAY']; compilerproc;
  384. var
  385. i, size: SizeInt;
  386. begin
  387. size:=RTTISize(typeinfo);
  388. if size>0 then
  389. for i:=0 to count-1 do
  390. int_finalize(data+size*i,typeinfo);
  391. end;
  392. procedure fpc_addref_array(data,typeinfo: pointer; count: SizeInt); [public,alias:'FPC_ADDREF_ARRAY']; compilerproc;
  393. var
  394. i, size: SizeInt;
  395. begin
  396. size:=RTTISize(typeinfo);
  397. if size>0 then
  398. for i:=0 to count-1 do
  399. int_addref(data+size*i,typeinfo);
  400. end;
  401. { The following two procedures are now obsolete, needed only for bootstrapping }
  402. procedure fpc_decref (Data, TypeInfo : Pointer);[Public,alias : 'FPC_DECREF']; compilerproc;
  403. begin
  404. int_finalize(Data,TypeInfo);
  405. end;
  406. procedure fpc_decref_array(data,typeinfo: pointer; count: SizeInt); [public,alias:'FPC_DECREF_ARRAY']; compilerproc;
  407. begin
  408. int_finalizeArray(data,typeinfo,count);
  409. end;
  410. procedure InitializeArray(p, typeInfo: Pointer; count: SizeInt);
  411. external name 'FPC_INITIALIZE_ARRAY';
  412. procedure FinalizeArray(p, typeInfo: Pointer; count: SizeInt);
  413. external name 'FPC_FINALIZE_ARRAY';
  414. procedure CopyArray(dest, source, typeInfo: Pointer; count: SizeInt);
  415. var
  416. i, size: SizeInt;
  417. begin
  418. size:=RTTISize(typeInfo);
  419. if size>0 then
  420. for i:=0 to count-1 do
  421. fpc_Copy_internal(source+size*i, dest+size*i, typeInfo);
  422. end;