rtti.inc 11 KB

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