rtti.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. function RTTIArraySize(typeInfo: Pointer): SizeInt;
  13. begin
  14. typeInfo:=aligntoqword(typeInfo+2+PByte(typeInfo)[1]);
  15. result:=PArrayInfo(typeInfo)^.Size;
  16. end;
  17. function RTTIRecordSize(typeInfo: Pointer): SizeInt;
  18. begin
  19. typeInfo:=aligntoqword(typeInfo+2+PByte(typeInfo)[1]);
  20. { for size field init table is compatible with rtti table }
  21. result:=PRecordInfoFull(typeInfo)^.Size;
  22. end;
  23. function RTTIRecordOp(typeInfo: Pointer; var initrtti: Pointer): PRecordInfoInit; inline;
  24. begin
  25. { find init table and management operators }
  26. typeInfo:=aligntoqword(typeInfo+2+PByte(typeInfo)[1]);
  27. result:=typeInfo;
  28. { check terminator, maybe we are already in init table }
  29. if Assigned(result^.Terminator) then
  30. begin
  31. { point to more optimal initrtti }
  32. initrtti:=PRecordInfoFull(result)^.InitTable;
  33. { and point to management operators in our init table }
  34. result:=aligntoqword(initrtti+2+PByte(initrtti)[1]);
  35. end
  36. end;
  37. { result = manBuiltin means e.g. that initialization is simply zeroing and can be omitted if the memory is already zeroed, as in dynarr.inc. }
  38. function RTTIManagementAndSize(typeInfo: Pointer; op: TRTTIRecOpType; out size: SizeInt): TRTTIManagement;
  39. var
  40. ri: PRecordInfoInit;
  41. elem, eElem: PRecordElement;
  42. newMan: TRTTIManagement;
  43. _initrtti: pointer;
  44. _size: SizeInt;
  45. begin
  46. case PTypeKind(typeinfo)^ of
  47. tkAString,tkWString,tkUString,
  48. tkInterface,tkDynarray:
  49. begin
  50. size:=sizeof(Pointer);
  51. result:=manBuiltin;
  52. end;
  53. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  54. tkVariant:
  55. begin
  56. size:=sizeof(TVarData);
  57. result:=manBuiltin;
  58. end;
  59. {$endif FPC_HAS_FEATURE_VARIANTS}
  60. tkArray:
  61. begin
  62. typeInfo:=aligntoqword(typeInfo+2+PByte(typeInfo)[1]);
  63. size:=PArrayInfo(typeInfo)^.Size;
  64. result:=RTTIManagementAndSize(PArrayInfo(typeInfo)^.ElInfo^, op, _size);
  65. end;
  66. tkObject,
  67. tkRecord:
  68. begin
  69. ri:=RTTIRecordOp(typeInfo, _initrtti);
  70. size:=ri^.Size;
  71. if Assigned(ri^.RecordOp) and Assigned(ri^.RecordOp^.Ops[op]) then
  72. result:=manCustom
  73. else
  74. begin
  75. result:=manNone;
  76. elem:=AlignTypeData(Pointer(@ri^.Count)+SizeOf(ri^.Count));
  77. eElem:=elem+ri^.Count;
  78. while elem<>eElem do
  79. begin
  80. newMan:=RTTIManagementAndSize(elem^.TypeInfo^, op, _size);
  81. if newMan<>manNone then
  82. result:=newMan;
  83. if newMan=manCustom then
  84. break;
  85. inc(elem);
  86. end;
  87. end;
  88. end;
  89. else
  90. result:=manNone; { Size undefined, ultimately can be always correct to support unmanaged scenarios. }
  91. end;
  92. end;
  93. { if you modify this procedure, fpc_copy must be probably modified as well }
  94. procedure RecordRTTI(Data,TypeInfo:Pointer;rttiproc:TRTTIProc);
  95. var
  96. count,
  97. i : longint;
  98. begin
  99. typeInfo:=aligntoqword(typeInfo+2+PByte(typeInfo)[1]);
  100. Count:=PRecordInfoInit(typeInfo)^.Count;
  101. { Get element info, hacky, but what else can we do? }
  102. typeInfo:=AlignTypeData(Pointer(@PRecordInfoInit(typeInfo)^.Count)+SizeOf(PRecordInfoInit(typeInfo)^.Count));
  103. { Process elements }
  104. for i:=1 to count Do
  105. begin
  106. rttiproc(Data+PRecordElement(typeInfo)^.Offset,PRecordElement(typeInfo)^.TypeInfo^);
  107. Inc(PRecordElement(typeInfo));
  108. end;
  109. end;
  110. function RTTIRecordMopInitTable(ti: Pointer): PRTTIRecordOpOffsetTable;
  111. begin
  112. ti:=aligntoqword(ti+2+PByte(ti)[1]);
  113. Result:=PRecordInfoInit(ti)^.InitRecordOpTable;
  114. end;
  115. { if you modify this procedure, fpc_copy must be probably modified as well }
  116. procedure ArrayRTTI(Data,TypeInfo:Pointer;rttiproc:TRTTIProc);
  117. var
  118. i,Count,ElSize: SizeInt;
  119. Info: Pointer;
  120. begin
  121. typeInfo:=aligntoqword(typeInfo+2+PByte(typeInfo)[1]);
  122. Count:=PArrayInfo(typeInfo)^.ElCount;
  123. { no elements to process => exit }
  124. if Count = 0 then
  125. Exit;
  126. ElSize:=PArrayInfo(typeInfo)^.Size div Count;
  127. Info:=PArrayInfo(typeInfo)^.ElInfo^;
  128. { Process elements }
  129. for I:=0 to Count-1 do
  130. rttiproc(Data+(I*ElSize),Info);
  131. end;
  132. Procedure fpc_Initialize (Data,TypeInfo : pointer);[Public,Alias : 'FPC_INITIALIZE']; compilerproc;
  133. begin
  134. case PTypeKind(TypeInfo)^ of
  135. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  136. tkDynArray,
  137. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  138. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  139. tkAstring,
  140. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  141. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  142. tkWstring,tkUString,
  143. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  144. tkInterface:
  145. PPAnsiChar(Data)^:=Nil;
  146. tkArray:
  147. arrayrtti(data,typeinfo,@int_initialize);
  148. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  149. tkObject,
  150. {$endif FPC_HAS_FEATURE_OBJECTS}
  151. tkRecord:
  152. { if possible try to use more optimal initrtti }
  153. with RTTIRecordOp(typeinfo, typeinfo)^ do
  154. begin
  155. recordrtti(data,typeinfo,@int_initialize);
  156. if Assigned(recordop) and Assigned(recordop^.Initialize) then
  157. recordop^.Initialize(data);
  158. end;
  159. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  160. tkVariant:
  161. variant_init(PVarData(Data)^);
  162. {$endif FPC_HAS_FEATURE_VARIANTS}
  163. end;
  164. end;
  165. Procedure fpc_finalize (Data,TypeInfo: Pointer);[Public,Alias : 'FPC_FINALIZE']; compilerproc;
  166. begin
  167. case PTypeKind(TypeInfo)^ of
  168. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  169. tkAstring :
  170. fpc_AnsiStr_Decr_Ref(PPointer(Data)^);
  171. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  172. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  173. tkUstring :
  174. fpc_UnicodeStr_Decr_Ref(PPointer(Data)^);
  175. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  176. tkWstring :
  177. fpc_WideStr_Decr_Ref(PPointer(Data)^);
  178. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  179. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  180. tkArray :
  181. arrayrtti(data,typeinfo,@int_finalize);
  182. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  183. tkObject,
  184. {$endif FPC_HAS_FEATURE_OBJECTS}
  185. tkRecord:
  186. { if possible try to use more optimal initrtti }
  187. with RTTIRecordOp(typeinfo, typeinfo)^ do
  188. begin
  189. if Assigned(recordop) and Assigned(recordop^.Finalize) then
  190. recordop^.Finalize(data);
  191. recordrtti(data,typeinfo,@int_finalize);
  192. end;
  193. {$ifdef FPC_HAS_FEATURE_CLASSES}
  194. tkInterface:
  195. Intf_Decr_Ref(PPointer(Data)^);
  196. {$endif FPC_HAS_FEATURE_CLASSES}
  197. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  198. tkDynArray:
  199. fpc_dynarray_clear(PPointer(Data)^,TypeInfo);
  200. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  201. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  202. tkVariant:
  203. variant_clear(PVarData(Data)^);
  204. {$endif FPC_HAS_FEATURE_VARIANTS}
  205. end;
  206. end;
  207. Procedure fpc_Addref (Data,TypeInfo : Pointer); [Public,alias : 'FPC_ADDREF']; compilerproc;
  208. begin
  209. case PTypeKind(TypeInfo)^ of
  210. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  211. tkAstring :
  212. fpc_AnsiStr_Incr_Ref(PPointer(Data)^);
  213. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  214. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  215. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  216. tkWstring :
  217. fpc_WideStr_Incr_Ref(PPointer(Data)^);
  218. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  219. tkUstring :
  220. fpc_UnicodeStr_Incr_Ref(PPointer(Data)^);
  221. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  222. tkArray :
  223. arrayrtti(data,typeinfo,@int_addref);
  224. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  225. tkobject,
  226. {$endif FPC_HAS_FEATURE_OBJECTS}
  227. tkrecord :
  228. { find init table }
  229. with RTTIRecordOp(typeinfo, typeinfo)^ do
  230. begin
  231. recordrtti(data,typeinfo,@int_addref);
  232. if Assigned(recordop) and Assigned(recordop^.AddRef) then
  233. recordop^.AddRef(Data);
  234. end;
  235. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  236. tkDynArray:
  237. fpc_dynarray_incr_ref(PPointer(Data)^);
  238. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  239. {$ifdef FPC_HAS_FEATURE_CLASSES}
  240. tkInterface:
  241. Intf_Incr_Ref(PPointer(Data)^);
  242. {$endif FPC_HAS_FEATURE_CLASSES}
  243. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  244. tkVariant:
  245. variant_addref(pvardata(Data)^);
  246. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  247. end;
  248. end;
  249. { define alias for internal use in the system unit }
  250. Function fpc_Copy_internal (Src, Dest, TypeInfo : Pointer) : SizeInt;[external name 'FPC_COPY'];
  251. Function fpc_Copy (Src, Dest, TypeInfo : Pointer) : SizeInt;[Public,alias : 'FPC_COPY']; compilerproc;
  252. var
  253. Temp: pbyte;
  254. copiedsize,
  255. expectedoffset,
  256. EleCount,
  257. offset,
  258. i: SizeInt;
  259. info: pointer;
  260. begin
  261. result:=sizeof(pointer);
  262. case PTypeKind(TypeInfo)^ of
  263. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  264. tkAstring:
  265. fpc_AnsiStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  266. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  267. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  268. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  269. tkWstring:
  270. fpc_WideStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  271. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  272. tkUstring:
  273. fpc_UnicodeStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  274. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  275. tkArray:
  276. begin
  277. Temp:=aligntoqword(typeInfo+2+PByte(typeInfo)[1]);
  278. Result:=PArrayInfo(Temp)^.Size;
  279. EleCount:=PArrayInfo(Temp)^.ElCount;
  280. { no elements to process => exit }
  281. if EleCount = 0 then
  282. Exit;
  283. Info:=PArrayInfo(Temp)^.ElInfo^;
  284. copiedsize:=Result div EleCount;
  285. Offset:=0;
  286. { Process elements }
  287. for I:=1 to EleCount do
  288. begin
  289. fpc_Copy_internal(Src+Offset,Dest+Offset,Info);
  290. inc(Offset,copiedsize);
  291. end;
  292. end;
  293. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  294. tkobject,
  295. {$endif FPC_HAS_FEATURE_OBJECTS}
  296. tkrecord:
  297. { find init table }
  298. with RTTIRecordOp(typeinfo, typeinfo)^ do
  299. begin
  300. Temp:=aligntoqword(typeInfo+2+PByte(typeInfo)[1]);
  301. if Assigned(recordop) and Assigned(recordop^.Copy) then
  302. begin
  303. recordop^.Copy(Src,Dest);
  304. Result:=PRecordInfoFull(Temp)^.Size;
  305. end
  306. else
  307. begin
  308. Result:=PRecordInfoInit(Temp)^.Size;
  309. EleCount:=PRecordInfoInit(Temp)^.Count;
  310. { Get element info, hacky, but what else can we do? }
  311. Temp:=AlignTypeData(Pointer(@PRecordInfoInit(Temp)^.Count)+SizeOf(PRecordInfoInit(Temp)^.Count));
  312. expectedoffset:=0;
  313. { Process elements with rtti }
  314. for i:=1 to EleCount Do
  315. begin
  316. Offset:=PRecordElement(Temp)^.Offset;
  317. if Offset>expectedoffset then
  318. move((Src+expectedoffset)^,(Dest+expectedoffset)^,Offset-expectedoffset);
  319. expectedoffset:=Offset+fpc_Copy_internal(Src+Offset,Dest+Offset,PRecordElement(Temp)^.TypeInfo^);
  320. Inc(PRecordElement(Temp));
  321. end;
  322. { elements remaining? }
  323. if result>expectedoffset then
  324. move((Src+expectedoffset)^,(Dest+expectedoffset)^,Result-expectedoffset);
  325. end;
  326. end;
  327. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  328. tkDynArray:
  329. fpc_dynarray_assign(PPointer(Dest)^,PPointer(Src)^,typeinfo);
  330. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  331. {$ifdef FPC_HAS_FEATURE_CLASSES}
  332. tkInterface:
  333. fpc_intf_assign(PPointer(Dest)^,PPointer(Src)^);
  334. {$endif FPC_HAS_FEATURE_CLASSES}
  335. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  336. tkVariant:
  337. begin
  338. VarCopyProc(pvardata(dest)^,pvardata(src)^);
  339. result:=sizeof(tvardata);
  340. end;
  341. {$endif FPC_HAS_FEATURE_VARIANTS}
  342. end;
  343. end;
  344. { For internal use by the compiler, because otherwise $x- can cause trouble. }
  345. { Generally disabling extended syntax checking for all compilerprocs may }
  346. { have unintended side-effects }
  347. procedure fpc_Copy_proc (Src, Dest, TypeInfo : Pointer);compilerproc; inline;
  348. begin
  349. fpc_copy_internal(src,dest,typeinfo);
  350. end;
  351. procedure fpc_initialize_array(data,typeinfo : pointer;count : SizeInt); [public,alias:'FPC_INITIALIZE_ARRAY']; compilerproc;
  352. var
  353. i, size : SizeInt;
  354. begin
  355. if RTTIManagementAndSize(typeinfo, rotInitialize, size)<>manNone then
  356. for i:=0 to count-1 do
  357. int_initialize(data+size*i,typeinfo);
  358. end;
  359. procedure fpc_finalize_array(data,typeinfo : pointer;count : SizeInt); [Public,Alias:'FPC_FINALIZE_ARRAY']; compilerproc;
  360. var
  361. i, size : SizeInt;
  362. begin
  363. if RTTIManagementAndSize(typeinfo, rotFinalize, size)<>manNone then
  364. for i:=0 to count-1 do
  365. int_finalize(data+size*i,typeinfo);
  366. end;
  367. procedure fpc_addref_array(data,typeinfo: pointer; count: SizeInt); [public,alias:'FPC_ADDREF_ARRAY']; compilerproc;
  368. var
  369. i, size : SizeInt;
  370. begin
  371. if RTTIManagementAndSize(typeinfo, rotAddRef, size)<>manNone then
  372. for i:=0 to count-1 do
  373. int_addref(data+size*i,typeinfo);
  374. end;
  375. { The following two procedures are now obsolete, needed only for bootstrapping }
  376. procedure fpc_decref (Data, TypeInfo : Pointer);[Public,alias : 'FPC_DECREF']; compilerproc;
  377. begin
  378. int_finalize(Data,TypeInfo);
  379. end;
  380. procedure fpc_decref_array(data,typeinfo: pointer; count: SizeInt); [public,alias:'FPC_DECREF_ARRAY']; compilerproc;
  381. begin
  382. int_finalizeArray(data,typeinfo,count);
  383. end;
  384. procedure InitializeArray(p, typeInfo: Pointer; count: SizeInt);
  385. external name 'FPC_INITIALIZE_ARRAY';
  386. procedure FinalizeArray(p, typeInfo: Pointer; count: SizeInt);
  387. external name 'FPC_FINALIZE_ARRAY';
  388. procedure CopyArray(dest, source, typeInfo: Pointer; count: SizeInt);
  389. var
  390. i, size: SizeInt;
  391. begin
  392. if RTTIManagementAndSize(typeinfo, rotCopy, size)<>manNone then
  393. for i:=0 to count-1 do
  394. fpc_Copy_internal(source+size*i, dest+size*i, typeInfo);
  395. end;