rtti.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. ElSize:=PArrayInfo(typeInfo)^.Size div Count;
  105. Info:=PArrayInfo(typeInfo)^.ElInfo;
  106. { Process elements }
  107. for I:=0 to Count-1 do
  108. rttiproc(Data+(I*ElSize),Info);
  109. end;
  110. {$endif}
  111. Procedure fpc_Initialize (Data,TypeInfo : pointer);[Public,Alias : 'FPC_INITIALIZE']; compilerproc;
  112. begin
  113. case PByte(TypeInfo)^ of
  114. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  115. tkDynArray,
  116. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  117. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  118. tkAstring,
  119. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  120. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  121. tkWstring,tkUString,
  122. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  123. tkInterface:
  124. PPchar(Data)^:=Nil;
  125. tkArray:
  126. arrayrtti(data,typeinfo,@int_initialize);
  127. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  128. tkObject,
  129. {$endif FPC_HAS_FEATURE_OBJECTS}
  130. tkRecord:
  131. recordrtti(data,typeinfo,@int_initialize);
  132. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  133. tkVariant:
  134. variant_init(PVarData(Data)^);
  135. {$endif FPC_HAS_FEATURE_VARIANTS}
  136. end;
  137. end;
  138. Procedure fpc_finalize (Data,TypeInfo: Pointer);[Public,Alias : 'FPC_FINALIZE']; compilerproc;
  139. begin
  140. case PByte(TypeInfo)^ of
  141. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  142. tkAstring :
  143. fpc_AnsiStr_Decr_Ref(PPointer(Data)^);
  144. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  145. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  146. tkUstring :
  147. fpc_UnicodeStr_Decr_Ref(PPointer(Data)^);
  148. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  149. tkWstring :
  150. fpc_WideStr_Decr_Ref(PPointer(Data)^);
  151. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  152. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  153. tkArray :
  154. arrayrtti(data,typeinfo,@int_finalize);
  155. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  156. tkObject,
  157. {$endif FPC_HAS_FEATURE_OBJECTS}
  158. tkRecord:
  159. recordrtti(data,typeinfo,@int_finalize);
  160. tkInterface:
  161. Intf_Decr_Ref(PPointer(Data)^);
  162. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  163. tkDynArray:
  164. fpc_dynarray_clear(PPointer(Data)^,TypeInfo);
  165. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  166. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  167. tkVariant:
  168. variant_clear(PVarData(Data)^);
  169. {$endif FPC_HAS_FEATURE_VARIANTS}
  170. end;
  171. end;
  172. Procedure fpc_Addref (Data,TypeInfo : Pointer); [Public,alias : 'FPC_ADDREF']; compilerproc;
  173. begin
  174. case PByte(TypeInfo)^ of
  175. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  176. tkAstring :
  177. fpc_AnsiStr_Incr_Ref(PPointer(Data)^);
  178. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  179. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  180. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  181. tkWstring :
  182. fpc_WideStr_Incr_Ref(PPointer(Data)^);
  183. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  184. tkUstring :
  185. fpc_UnicodeStr_Incr_Ref(PPointer(Data)^);
  186. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  187. tkArray :
  188. arrayrtti(data,typeinfo,@int_addref);
  189. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  190. tkobject,
  191. {$endif FPC_HAS_FEATURE_OBJECTS}
  192. tkrecord :
  193. recordrtti(data,typeinfo,@int_addref);
  194. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  195. tkDynArray:
  196. fpc_dynarray_incr_ref(PPointer(Data)^);
  197. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  198. tkInterface:
  199. Intf_Incr_Ref(PPointer(Data)^);
  200. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  201. tkVariant:
  202. variant_addref(pvardata(Data)^);
  203. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  204. end;
  205. end;
  206. { define alias for internal use in the system unit }
  207. Function fpc_Copy_internal (Src, Dest, TypeInfo : Pointer) : SizeInt;[external name 'FPC_COPY'];
  208. Function fpc_Copy (Src, Dest, TypeInfo : Pointer) : SizeInt;[Public,alias : 'FPC_COPY']; compilerproc;
  209. var
  210. Temp: pbyte;
  211. copiedsize,
  212. expectedoffset,
  213. count,
  214. offset,
  215. i: SizeInt;
  216. info: pointer;
  217. begin
  218. result:=sizeof(pointer);
  219. case PByte(TypeInfo)^ of
  220. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  221. tkAstring:
  222. fpc_AnsiStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  223. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  224. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  225. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  226. tkWstring:
  227. fpc_WideStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  228. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  229. tkUstring:
  230. fpc_UnicodeStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  231. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  232. tkArray:
  233. begin
  234. Temp:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  235. {$ifdef VER2_6}
  236. { Process elements }
  237. for I:=0 to PArrayInfo(Temp)^.ElCount-1 do
  238. fpc_Copy_internal(Src+(I*PArrayInfo(Temp)^.Size),Dest+(I*PArrayInfo(Temp)^.Size),PArrayInfo(Temp)^.ElInfo);
  239. Result:=PArrayInfo(Temp)^.Size*PArrayInfo(Temp)^.ElCount;
  240. {$else}
  241. Result:=PArrayInfo(Temp)^.Size;
  242. Count:=PArrayInfo(Temp)^.ElCount;
  243. Info:=PArrayInfo(Temp)^.ElInfo;
  244. copiedsize:=Result div Count;
  245. Offset:=0;
  246. { Process elements }
  247. for I:=1 to Count do
  248. begin
  249. fpc_Copy_internal(Src+Offset,Dest+Offset,Info);
  250. inc(Offset,copiedsize);
  251. end;
  252. {$endif}
  253. end;
  254. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  255. tkobject,
  256. {$endif FPC_HAS_FEATURE_OBJECTS}
  257. tkrecord:
  258. begin
  259. Temp:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  260. Result:=PRecordInfo(Temp)^.Size;
  261. Count:=PRecordInfo(Temp)^.Count;
  262. Inc(PRecordInfo(Temp));
  263. expectedoffset:=0;
  264. { Process elements with rtti }
  265. for i:=1 to count Do
  266. begin
  267. Info:=PRecordElement(Temp)^.TypeInfo;
  268. Offset:=PRecordElement(Temp)^.Offset;
  269. Inc(PRecordElement(Temp));
  270. if Offset>expectedoffset then
  271. move((Src+expectedoffset)^,(Dest+expectedoffset)^,Offset-expectedoffset);
  272. copiedsize:=fpc_Copy_internal(Src+Offset,Dest+Offset,Info);
  273. expectedoffset:=Offset+copiedsize;
  274. end;
  275. { elements remaining? }
  276. if result>expectedoffset then
  277. move((Src+expectedoffset)^,(Dest+expectedoffset)^,Result-expectedoffset);
  278. end;
  279. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  280. tkDynArray:
  281. fpc_dynarray_assign(PPointer(Dest)^,PPointer(Src)^,typeinfo);
  282. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  283. tkInterface:
  284. fpc_intf_assign(PPointer(Dest)^,PPointer(Src)^);
  285. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  286. tkVariant:
  287. begin
  288. VarCopyProc(pvardata(dest)^,pvardata(src)^);
  289. result:=sizeof(tvardata);
  290. end;
  291. {$endif FPC_HAS_FEATURE_VARIANTS}
  292. end;
  293. end;
  294. { For internal use by the compiler, because otherwise $x- can cause trouble. }
  295. { Generally disabling extended syntax checking for all compilerprocs may }
  296. { have unintended side-effects }
  297. procedure fpc_Copy_proc (Src, Dest, TypeInfo : Pointer);compilerproc; inline;
  298. begin
  299. fpc_copy_internal(src,dest,typeinfo);
  300. end;
  301. procedure fpc_initialize_array(data,typeinfo : pointer;count : SizeInt); [public,alias:'FPC_INITIALIZE_ARRAY'] compilerproc;
  302. var
  303. i, size : SizeInt;
  304. begin
  305. size:=RTTISize(typeinfo);
  306. if size>0 then
  307. for i:=0 to count-1 do
  308. int_initialize(data+size*i,typeinfo);
  309. end;
  310. procedure fpc_finalize_array(data,typeinfo : pointer;count : SizeInt); [Public,Alias:'FPC_FINALIZE_ARRAY']; compilerproc;
  311. var
  312. i, size: SizeInt;
  313. begin
  314. size:=RTTISize(typeinfo);
  315. if size>0 then
  316. for i:=0 to count-1 do
  317. int_finalize(data+size*i,typeinfo);
  318. end;
  319. procedure fpc_addref_array(data,typeinfo: pointer; count: SizeInt); [public,alias:'FPC_ADDREF_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_addref(data+size*i,typeinfo);
  327. end;
  328. { The following two procedures are now obsolete, needed only for bootstrapping }
  329. procedure fpc_decref (Data, TypeInfo : Pointer);[Public,alias : 'FPC_DECREF']; compilerproc;
  330. begin
  331. int_finalize(Data,TypeInfo);
  332. end;
  333. procedure fpc_decref_array(data,typeinfo: pointer; count: SizeInt); [public,alias:'FPC_DECREF_ARRAY']; compilerproc;
  334. begin
  335. int_finalizeArray(data,typeinfo,count);
  336. end;