rtti.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. PRecordInfo=^TRecordInfo;
  38. TRecordInfo=
  39. {$ifdef USE_PACKED}
  40. packed
  41. {$endif USE_PACKED}
  42. record
  43. Size: Longint;
  44. Count: Longint;
  45. { Elements: array[count] of TRecordElement }
  46. end;
  47. PArrayInfo=^TArrayInfo;
  48. TArrayInfo=
  49. {$ifdef USE_PACKED}
  50. packed
  51. {$endif USE_PACKED}
  52. record
  53. Size: SizeInt;
  54. ElCount: SizeInt;
  55. {$ifdef VER3_0}
  56. ElInfo: Pointer;
  57. {$else}
  58. ElInfo: PPointer;
  59. {$endif}
  60. DimCount: Byte;
  61. Dims:array[0..255] of Pointer;
  62. end;
  63. function RTTIArraySize(typeInfo: Pointer): SizeInt;
  64. begin
  65. typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  66. {$ifdef VER2_6}
  67. result:=PArrayInfo(typeInfo)^.Size*PArrayInfo(typeInfo)^.ElCount;
  68. {$else}
  69. result:=PArrayInfo(typeInfo)^.Size;
  70. {$endif}
  71. end;
  72. function RTTIRecordSize(typeInfo: Pointer): SizeInt;
  73. begin
  74. typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  75. result:=PRecordInfo(typeInfo)^.Size;
  76. end;
  77. function RTTISize(typeInfo: Pointer): SizeInt;
  78. begin
  79. case PByte(typeinfo)^ of
  80. tkAString,tkWString,tkUString,
  81. tkInterface,tkDynarray:
  82. result:=sizeof(Pointer);
  83. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  84. tkVariant:
  85. result:=sizeof(TVarData);
  86. {$endif FPC_HAS_FEATURE_VARIANTS}
  87. tkArray:
  88. result:=RTTIArraySize(typeinfo);
  89. tkObject,tkRecord:
  90. result:=RTTIRecordSize(typeinfo);
  91. else
  92. result:=-1;
  93. end;
  94. end;
  95. { if you modify this procedure, fpc_copy must be probably modified as well }
  96. procedure RecordRTTI(Data,TypeInfo:Pointer;rttiproc:TRTTIProc);
  97. var
  98. count,
  99. i : longint;
  100. begin
  101. typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  102. Count:=PRecordInfo(typeInfo)^.Count;
  103. Inc(PRecordInfo(typeInfo));
  104. { Process elements }
  105. for i:=1 to count Do
  106. begin
  107. rttiproc(Data+PRecordElement(typeInfo)^.Offset,PRecordElement(typeInfo)^.TypeInfo{$ifndef VER3_0}^{$endif});
  108. Inc(PRecordElement(typeInfo));
  109. end;
  110. end;
  111. { if you modify this procedure, fpc_copy must be probably modified as well }
  112. {$ifdef VER2_6}
  113. procedure ArrayRTTI(Data,TypeInfo:Pointer;rttiproc:TRTTIProc);
  114. var
  115. i: SizeInt;
  116. begin
  117. typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  118. { Process elements }
  119. for I:=0 to PArrayInfo(typeInfo)^.ElCount-1 do
  120. rttiproc(Data+(I*PArrayInfo(typeInfo)^.Size),PArrayInfo(typeInfo)^.ElInfo);
  121. end;
  122. {$else}
  123. procedure ArrayRTTI(Data,TypeInfo:Pointer;rttiproc:TRTTIProc);
  124. var
  125. i,Count,ElSize: SizeInt;
  126. Info: Pointer;
  127. begin
  128. typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  129. Count:=PArrayInfo(typeInfo)^.ElCount;
  130. { no elements to process => exit }
  131. if Count = 0 then
  132. Exit;
  133. ElSize:=PArrayInfo(typeInfo)^.Size div Count;
  134. Info:=PArrayInfo(typeInfo)^.ElInfo{$ifndef VER3_0}^{$endif};
  135. { Process elements }
  136. for I:=0 to Count-1 do
  137. rttiproc(Data+(I*ElSize),Info);
  138. end;
  139. {$endif}
  140. Procedure fpc_Initialize (Data,TypeInfo : pointer);[Public,Alias : 'FPC_INITIALIZE']; compilerproc;
  141. begin
  142. case PByte(TypeInfo)^ of
  143. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  144. tkDynArray,
  145. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  146. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  147. tkAstring,
  148. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  149. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  150. tkWstring,tkUString,
  151. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  152. tkInterface:
  153. PPchar(Data)^:=Nil;
  154. tkArray:
  155. arrayrtti(data,typeinfo,@int_initialize);
  156. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  157. tkObject,
  158. {$endif FPC_HAS_FEATURE_OBJECTS}
  159. tkRecord:
  160. recordrtti(data,typeinfo,@int_initialize);
  161. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  162. tkVariant:
  163. variant_init(PVarData(Data)^);
  164. {$endif FPC_HAS_FEATURE_VARIANTS}
  165. end;
  166. end;
  167. Procedure fpc_finalize (Data,TypeInfo: Pointer);[Public,Alias : 'FPC_FINALIZE']; compilerproc;
  168. begin
  169. case PByte(TypeInfo)^ of
  170. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  171. tkAstring :
  172. fpc_AnsiStr_Decr_Ref(PPointer(Data)^);
  173. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  174. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  175. tkUstring :
  176. fpc_UnicodeStr_Decr_Ref(PPointer(Data)^);
  177. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  178. tkWstring :
  179. fpc_WideStr_Decr_Ref(PPointer(Data)^);
  180. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  181. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  182. tkArray :
  183. arrayrtti(data,typeinfo,@int_finalize);
  184. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  185. tkObject,
  186. {$endif FPC_HAS_FEATURE_OBJECTS}
  187. tkRecord:
  188. recordrtti(data,typeinfo,@int_finalize);
  189. tkInterface:
  190. Intf_Decr_Ref(PPointer(Data)^);
  191. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  192. tkDynArray:
  193. fpc_dynarray_clear(PPointer(Data)^,TypeInfo);
  194. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  195. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  196. tkVariant:
  197. variant_clear(PVarData(Data)^);
  198. {$endif FPC_HAS_FEATURE_VARIANTS}
  199. end;
  200. end;
  201. Procedure fpc_Addref (Data,TypeInfo : Pointer); [Public,alias : 'FPC_ADDREF']; compilerproc;
  202. begin
  203. case PByte(TypeInfo)^ of
  204. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  205. tkAstring :
  206. fpc_AnsiStr_Incr_Ref(PPointer(Data)^);
  207. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  208. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  209. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  210. tkWstring :
  211. fpc_WideStr_Incr_Ref(PPointer(Data)^);
  212. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  213. tkUstring :
  214. fpc_UnicodeStr_Incr_Ref(PPointer(Data)^);
  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. { define alias for internal use in the system unit }
  236. Function fpc_Copy_internal (Src, Dest, TypeInfo : Pointer) : SizeInt;[external name 'FPC_COPY'];
  237. Function fpc_Copy (Src, Dest, TypeInfo : Pointer) : SizeInt;[Public,alias : 'FPC_COPY']; compilerproc;
  238. var
  239. Temp: pbyte;
  240. copiedsize,
  241. expectedoffset,
  242. count,
  243. offset,
  244. i: SizeInt;
  245. info: pointer;
  246. begin
  247. result:=sizeof(pointer);
  248. case PByte(TypeInfo)^ of
  249. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  250. tkAstring:
  251. fpc_AnsiStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  252. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  253. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  254. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  255. tkWstring:
  256. fpc_WideStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  257. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  258. tkUstring:
  259. fpc_UnicodeStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  260. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  261. tkArray:
  262. begin
  263. Temp:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  264. {$ifdef VER2_6}
  265. { Process elements }
  266. for I:=0 to PArrayInfo(Temp)^.ElCount-1 do
  267. fpc_Copy_internal(Src+(I*PArrayInfo(Temp)^.Size),Dest+(I*PArrayInfo(Temp)^.Size),PArrayInfo(Temp)^.ElInfo);
  268. Result:=PArrayInfo(Temp)^.Size*PArrayInfo(Temp)^.ElCount;
  269. {$else}
  270. Result:=PArrayInfo(Temp)^.Size;
  271. Count:=PArrayInfo(Temp)^.ElCount;
  272. { no elements to process => exit }
  273. if Count = 0 then
  274. Exit;
  275. Info:=PArrayInfo(Temp)^.ElInfo{$ifndef VER3_0}^{$endif};
  276. copiedsize:=Result div Count;
  277. Offset:=0;
  278. { Process elements }
  279. for I:=1 to Count do
  280. begin
  281. fpc_Copy_internal(Src+Offset,Dest+Offset,Info);
  282. inc(Offset,copiedsize);
  283. end;
  284. {$endif}
  285. end;
  286. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  287. tkobject,
  288. {$endif FPC_HAS_FEATURE_OBJECTS}
  289. tkrecord:
  290. begin
  291. Temp:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  292. Result:=PRecordInfo(Temp)^.Size;
  293. Count:=PRecordInfo(Temp)^.Count;
  294. Inc(PRecordInfo(Temp));
  295. expectedoffset:=0;
  296. { Process elements with rtti }
  297. for i:=1 to count Do
  298. begin
  299. Info:=PRecordElement(Temp)^.TypeInfo{$ifndef VER3_0}^{$endif};
  300. Offset:=PRecordElement(Temp)^.Offset;
  301. Inc(PRecordElement(Temp));
  302. if Offset>expectedoffset then
  303. move((Src+expectedoffset)^,(Dest+expectedoffset)^,Offset-expectedoffset);
  304. copiedsize:=fpc_Copy_internal(Src+Offset,Dest+Offset,Info);
  305. expectedoffset:=Offset+copiedsize;
  306. end;
  307. { elements remaining? }
  308. if result>expectedoffset then
  309. move((Src+expectedoffset)^,(Dest+expectedoffset)^,Result-expectedoffset);
  310. end;
  311. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  312. tkDynArray:
  313. fpc_dynarray_assign(PPointer(Dest)^,PPointer(Src)^,typeinfo);
  314. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  315. tkInterface:
  316. fpc_intf_assign(PPointer(Dest)^,PPointer(Src)^);
  317. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  318. tkVariant:
  319. begin
  320. VarCopyProc(pvardata(dest)^,pvardata(src)^);
  321. result:=sizeof(tvardata);
  322. end;
  323. {$endif FPC_HAS_FEATURE_VARIANTS}
  324. end;
  325. end;
  326. { For internal use by the compiler, because otherwise $x- can cause trouble. }
  327. { Generally disabling extended syntax checking for all compilerprocs may }
  328. { have unintended side-effects }
  329. procedure fpc_Copy_proc (Src, Dest, TypeInfo : Pointer);compilerproc; inline;
  330. begin
  331. fpc_copy_internal(src,dest,typeinfo);
  332. end;
  333. procedure fpc_initialize_array(data,typeinfo : pointer;count : SizeInt); [public,alias:'FPC_INITIALIZE_ARRAY'] compilerproc;
  334. var
  335. i, size : SizeInt;
  336. begin
  337. size:=RTTISize(typeinfo);
  338. if size>0 then
  339. for i:=0 to count-1 do
  340. int_initialize(data+size*i,typeinfo);
  341. end;
  342. procedure fpc_finalize_array(data,typeinfo : pointer;count : SizeInt); [Public,Alias:'FPC_FINALIZE_ARRAY']; compilerproc;
  343. var
  344. i, size: SizeInt;
  345. begin
  346. size:=RTTISize(typeinfo);
  347. if size>0 then
  348. for i:=0 to count-1 do
  349. int_finalize(data+size*i,typeinfo);
  350. end;
  351. procedure fpc_addref_array(data,typeinfo: pointer; count: SizeInt); [public,alias:'FPC_ADDREF_ARRAY']; compilerproc;
  352. var
  353. i, size: SizeInt;
  354. begin
  355. size:=RTTISize(typeinfo);
  356. if size>0 then
  357. for i:=0 to count-1 do
  358. int_addref(data+size*i,typeinfo);
  359. end;
  360. { The following two procedures are now obsolete, needed only for bootstrapping }
  361. procedure fpc_decref (Data, TypeInfo : Pointer);[Public,alias : 'FPC_DECREF']; compilerproc;
  362. begin
  363. int_finalize(Data,TypeInfo);
  364. end;
  365. procedure fpc_decref_array(data,typeinfo: pointer; count: SizeInt); [public,alias:'FPC_DECREF_ARRAY']; compilerproc;
  366. begin
  367. int_finalizeArray(data,typeinfo,count);
  368. end;
  369. procedure InitializeArray(p, typeInfo: Pointer; count: SizeInt);
  370. external name 'FPC_INITIALIZE_ARRAY';
  371. procedure FinalizeArray(p, typeInfo: Pointer; count: SizeInt);
  372. external name 'FPC_FINALIZE_ARRAY';
  373. procedure CopyArray(dest, source, typeInfo: Pointer; count: SizeInt);
  374. var
  375. i, size: SizeInt;
  376. begin
  377. size:=RTTISize(typeInfo);
  378. if size>0 then
  379. for i:=0 to count-1 do
  380. fpc_Copy_internal(source+size*i, dest+size*i, typeInfo);
  381. end;