rtti.inc 14 KB

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