rtti.inc 12 KB

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