rtti.inc 11 KB

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