rtti.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. fpc_AnsiStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  252. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  253. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  254. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  255. tkWstring:
  256. fpc_WideStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  257. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  258. tkUstring:
  259. fpc_UnicodeStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  260. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  261. tkArray:
  262. begin
  263. ArrayInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  264. { Process elements }
  265. for I:=0 to ArrayInfo^.ElCount-1 do
  266. fpc_Copy_internal(Src+(I*ArrayInfo^.ElSize),Dest+(I*ArrayInfo^.ElSize),ArrayInfo^.ElInfo);
  267. Result:=ArrayInfo^.ElSize*ArrayInfo^.ElCount;
  268. end;
  269. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  270. tkobject,
  271. {$endif FPC_HAS_FEATURE_OBJECTS}
  272. tkrecord:
  273. begin
  274. Temp:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  275. Result:=PRecordInfo(Temp)^.Size;
  276. Count:=PRecordInfo(Temp)^.Count;
  277. Inc(PRecordInfo(Temp));
  278. expectedoffset:=0;
  279. { Process elements with rtti }
  280. for i:=1 to count Do
  281. begin
  282. Info:=PRecordElement(Temp)^.TypeInfo;
  283. Offset:=PRecordElement(Temp)^.Offset;
  284. Inc(PRecordElement(Temp));
  285. if Offset>expectedoffset then
  286. move((Src+expectedoffset)^,(Dest+expectedoffset)^,Offset-expectedoffset);
  287. copiedsize:=fpc_Copy_internal(Src+Offset,Dest+Offset,Info);
  288. expectedoffset:=Offset+copiedsize;
  289. end;
  290. { elements remaining? }
  291. if result>expectedoffset then
  292. move((Src+expectedoffset)^,(Dest+expectedoffset)^,Result-expectedoffset);
  293. end;
  294. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  295. tkDynArray:
  296. fpc_dynarray_assign(PPointer(Dest)^,PPointer(Src)^,typeinfo);
  297. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  298. tkInterface:
  299. fpc_intf_assign(PPointer(Dest)^,PPointer(Src)^);
  300. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  301. tkVariant:
  302. begin
  303. VarCopyProc(pvardata(dest)^,pvardata(src)^);
  304. result:=sizeof(tvardata);
  305. end;
  306. {$endif FPC_HAS_FEATURE_VARIANTS}
  307. end;
  308. end;
  309. { For internal use by the compiler, because otherwise $x- can cause trouble. }
  310. { Generally disabling extended syntax checking for all compilerprocs may }
  311. { have unintended side-effects }
  312. procedure fpc_Copy_proc (Src, Dest, TypeInfo : Pointer);compilerproc; inline;
  313. begin
  314. fpc_copy_internal(src,dest,typeinfo);
  315. end;
  316. procedure fpc_initialize_array(data,typeinfo : pointer;count : SizeInt); [public,alias:'FPC_INITIALIZE_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_initialize(data+size*i,typeinfo);
  324. end;
  325. procedure fpc_finalize_array(data,typeinfo : pointer;count : SizeInt); [Public,Alias:'FPC_FINALIZE_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_finalize(data+size*i,typeinfo);
  333. end;
  334. procedure fpc_addref_array(data,typeinfo: pointer; count: SizeInt); [public,alias:'FPC_ADDREF_ARRAY']; compilerproc;
  335. var
  336. i, size: SizeInt;
  337. begin
  338. size:=RTTISize(typeinfo);
  339. if size>0 then
  340. for i:=0 to count-1 do
  341. int_addref(data+size*i,typeinfo);
  342. end;
  343. procedure fpc_decref_array(data,typeinfo: pointer; count: SizeInt); [public,alias:'FPC_DECREF_ARRAY']; compilerproc;
  344. var
  345. i, size: SizeInt;
  346. begin
  347. size:=RTTISize(typeinfo);
  348. if size>0 then
  349. for i:=0 to count-1 do
  350. int_decref(data+size*i,typeinfo);
  351. end;