rtti.inc 12 KB

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