rtti.inc 14 KB

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