rtti.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. tkUstring :
  125. begin
  126. fpc_UnicodeStr_Decr_Ref(PPointer(Data)^);
  127. PPointer(Data)^:=nil;
  128. end;
  129. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  130. tkWstring :
  131. begin
  132. fpc_WideStr_Decr_Ref(PPointer(Data)^);
  133. PPointer(Data)^:=nil;
  134. end;
  135. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  136. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  137. tkArray :
  138. arrayrtti(data,typeinfo,@int_finalize);
  139. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  140. tkObject,
  141. {$endif FPC_HAS_FEATURE_OBJECTS}
  142. tkRecord:
  143. recordrtti(data,typeinfo,@int_finalize);
  144. tkInterface:
  145. begin
  146. Intf_Decr_Ref(PPointer(Data)^);
  147. PPointer(Data)^:=nil;
  148. end;
  149. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  150. tkDynArray:
  151. begin
  152. fpc_dynarray_decr_ref(PPointer(Data)^,TypeInfo);
  153. PPointer(Data)^:=nil;
  154. end;
  155. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  156. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  157. tkVariant:
  158. variant_clear(PVarData(Data)^);
  159. {$endif FPC_HAS_FEATURE_VARIANTS}
  160. end;
  161. end;
  162. Procedure fpc_Addref (Data,TypeInfo : Pointer); [Public,alias : 'FPC_ADDREF']; compilerproc;
  163. begin
  164. case PByte(TypeInfo)^ of
  165. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  166. tkAstring :
  167. fpc_AnsiStr_Incr_Ref(PPointer(Data)^);
  168. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  169. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  170. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  171. tkWstring :
  172. fpc_WideStr_Incr_Ref(PPointer(Data)^);
  173. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  174. tkUstring :
  175. fpc_UnicodeStr_Incr_Ref(PPointer(Data)^);
  176. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  177. tkArray :
  178. arrayrtti(data,typeinfo,@int_addref);
  179. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  180. tkobject,
  181. {$endif FPC_HAS_FEATURE_OBJECTS}
  182. tkrecord :
  183. recordrtti(data,typeinfo,@int_addref);
  184. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  185. tkDynArray:
  186. fpc_dynarray_incr_ref(PPointer(Data)^);
  187. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  188. tkInterface:
  189. Intf_Incr_Ref(PPointer(Data)^);
  190. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  191. tkVariant:
  192. variant_addref(pvardata(Data)^);
  193. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  194. end;
  195. end;
  196. { alias for internal use }
  197. { we use another name else the compiler gets puzzled because of the wrong forward def }
  198. procedure fpc_systemDecRef (Data, TypeInfo : Pointer);[external name 'FPC_DECREF'];
  199. Procedure fpc_DecRef (Data, TypeInfo : Pointer);[Public,alias : 'FPC_DECREF']; compilerproc;
  200. begin
  201. case PByte(TypeInfo)^ of
  202. { see AddRef for comment about below construct (JM) }
  203. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  204. tkAstring:
  205. fpc_AnsiStr_Decr_Ref(PPointer(Data)^);
  206. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  207. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  208. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  209. tkWstring:
  210. fpc_WideStr_Decr_Ref(PPointer(Data)^);
  211. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  212. tkUString:
  213. fpc_UnicodeStr_Decr_Ref(PPointer(Data)^);
  214. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  215. tkArray:
  216. arrayrtti(data,typeinfo,@fpc_systemDecRef);
  217. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  218. tkobject,
  219. {$endif FPC_HAS_FEATURE_OBJECTS}
  220. tkrecord:
  221. recordrtti(data,typeinfo,@fpc_systemDecRef);
  222. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  223. tkDynArray:
  224. fpc_dynarray_decr_ref(PPointer(Data)^,TypeInfo);
  225. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  226. tkInterface:
  227. Intf_Decr_Ref(PPointer(Data)^);
  228. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  229. tkVariant:
  230. variant_clear(pvardata(data)^);
  231. {$endif FPC_HAS_FEATURE_VARIANTS}
  232. end;
  233. end;
  234. { define alias for internal use in the system unit }
  235. Function fpc_Copy_internal (Src, Dest, TypeInfo : Pointer) : SizeInt;[external name 'FPC_COPY'];
  236. Function fpc_Copy (Src, Dest, TypeInfo : Pointer) : SizeInt;[Public,alias : 'FPC_COPY']; compilerproc;
  237. var
  238. ArrayInfo: PArrayInfo;
  239. Temp : pbyte;
  240. copiedsize,
  241. expectedoffset,
  242. count,
  243. offset,
  244. i : SizeInt;
  245. info : pointer;
  246. begin
  247. result:=sizeof(pointer);
  248. case PByte(TypeInfo)^ of
  249. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  250. tkAstring:
  251. begin
  252. fpc_AnsiStr_Incr_Ref(PPointer(Src)^);
  253. fpc_AnsiStr_Decr_Ref(PPointer(Dest)^);
  254. PPointer(Dest)^:=PPointer(Src)^;
  255. end;
  256. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  257. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  258. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  259. tkWstring:
  260. fpc_WideStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  261. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  262. tkUstring:
  263. fpc_UnicodeStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  264. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  265. tkArray:
  266. begin
  267. ArrayInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  268. { Process elements }
  269. for I:=0 to ArrayInfo^.ElCount-1 do
  270. fpc_Copy_internal(Src+(I*ArrayInfo^.ElSize),Dest+(I*ArrayInfo^.ElSize),ArrayInfo^.ElInfo);
  271. Result:=ArrayInfo^.ElSize*ArrayInfo^.ElCount;
  272. end;
  273. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  274. tkobject,
  275. {$endif FPC_HAS_FEATURE_OBJECTS}
  276. tkrecord:
  277. begin
  278. Temp:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  279. Result:=PRecordInfo(Temp)^.Size;
  280. Count:=PRecordInfo(Temp)^.Count;
  281. Inc(PRecordInfo(Temp));
  282. expectedoffset:=0;
  283. { Process elements with rtti }
  284. for i:=1 to count Do
  285. begin
  286. Info:=PRecordElement(Temp)^.TypeInfo;
  287. Offset:=PRecordElement(Temp)^.Offset;
  288. Inc(PRecordElement(Temp));
  289. if Offset>expectedoffset then
  290. move((Src+expectedoffset)^,(Dest+expectedoffset)^,Offset-expectedoffset);
  291. copiedsize:=fpc_Copy_internal(Src+Offset,Dest+Offset,Info);
  292. expectedoffset:=Offset+copiedsize;
  293. end;
  294. { elements remaining? }
  295. if result>expectedoffset then
  296. move((Src+expectedoffset)^,(Dest+expectedoffset)^,Result-expectedoffset);
  297. end;
  298. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  299. tkDynArray:
  300. begin
  301. fpc_dynarray_Incr_Ref(PPointer(Src)^);
  302. fpc_dynarray_Decr_Ref(PPointer(Dest)^,typeinfo);
  303. PPointer(Dest)^:=PPointer(Src)^;
  304. end;
  305. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  306. tkInterface:
  307. begin
  308. Intf_Incr_Ref(PPointer(Src)^);
  309. Intf_Decr_Ref(PPointer(Dest)^);
  310. PPointer(Dest)^:=PPointer(Src)^;
  311. end;
  312. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  313. tkVariant:
  314. begin
  315. VarCopyProc(pvardata(dest)^,pvardata(src)^);
  316. result:=sizeof(tvardata);
  317. end;
  318. {$endif FPC_HAS_FEATURE_VARIANTS}
  319. end;
  320. end;
  321. { For internal use by the compiler, because otherwise $x- can cause trouble. }
  322. { Generally disabling extended syntax checking for all compilerprocs may }
  323. { have unintended side-effects }
  324. procedure fpc_Copy_proc (Src, Dest, TypeInfo : Pointer);compilerproc; inline;
  325. begin
  326. fpc_copy_internal(src,dest,typeinfo);
  327. end;
  328. procedure fpc_initialize_array(data,typeinfo : pointer;count : SizeInt); [public,alias:'FPC_INITIALIZE_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_initialize(data+size*i,typeinfo);
  336. end;
  337. procedure fpc_finalize_array(data,typeinfo : pointer;count : SizeInt); [Public,Alias:'FPC_FINALIZE_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_finalize(data+size*i,typeinfo);
  345. end;
  346. procedure fpc_addref_array(data,typeinfo: pointer; count: SizeInt); [public,alias:'FPC_ADDREF_ARRAY']; compilerproc;
  347. var
  348. i, size: SizeInt;
  349. begin
  350. size:=RTTISize(typeinfo);
  351. if size>0 then
  352. for i:=0 to count-1 do
  353. int_addref(data+size*i,typeinfo);
  354. end;
  355. procedure fpc_decref_array(data,typeinfo: pointer; count: SizeInt); [public,alias:'FPC_DECREF_ARRAY']; compilerproc;
  356. var
  357. i, size: SizeInt;
  358. begin
  359. size:=RTTISize(typeinfo);
  360. if size>0 then
  361. for i:=0 to count-1 do
  362. int_decref(data+size*i,typeinfo);
  363. end;