rtti.inc 11 KB

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