rtti.inc 9.7 KB

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