rtti.inc 11 KB

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