rtti.inc 9.7 KB

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