rtti.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. Function fpc_Copy (Src, Dest, TypeInfo : Pointer) : SizeInt;[Public,alias : 'FPC_COPY']; compilerproc;
  237. var
  238. copiedsize,
  239. expectedoffset,
  240. EleCount,
  241. offset,
  242. i: SizeInt;
  243. Temp,
  244. info: pointer;
  245. begin
  246. result:=sizeof(pointer);
  247. case PTypeKind(TypeInfo)^ of
  248. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  249. tkAstring:
  250. fpc_AnsiStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  251. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  252. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  253. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  254. tkWstring:
  255. fpc_WideStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  256. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  257. tkUstring:
  258. fpc_UnicodeStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  259. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  260. tkArray:
  261. begin
  262. Temp:=aligntoqword(typeInfo+2+PByte(typeInfo)[1]);
  263. Result:=PArrayInfo(Temp)^.Size;
  264. EleCount:=PArrayInfo(Temp)^.ElCount;
  265. { no elements to process => exit }
  266. if EleCount = 0 then
  267. Exit;
  268. Info:=PArrayInfo(Temp)^.ElInfo^;
  269. copiedsize:=Result div EleCount;
  270. Offset:=0;
  271. { Process elements }
  272. for I:=1 to EleCount do
  273. begin
  274. int_Copy(Src+Offset,Dest+Offset,Info);
  275. inc(Offset,copiedsize);
  276. end;
  277. end;
  278. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  279. tkobject,
  280. {$endif FPC_HAS_FEATURE_OBJECTS}
  281. tkrecord:
  282. begin
  283. Temp:=RTTIRecordInfoInit(typeinfo);
  284. Result:=PRecordInfoInit(Temp)^.Size;
  285. if Assigned(PRecordInfoInit(Temp)^.recordop) and Assigned(PRecordInfoInit(Temp)^.recordop^.Copy) then
  286. PRecordInfoInit(Temp)^.recordop^.Copy(Src,Dest)
  287. else
  288. begin
  289. EleCount:=PRecordInfoInit(Temp)^.Count;
  290. { Get element info, hacky, but what else can we do? }
  291. Temp:=AlignTypeData(Pointer(@PRecordInfoInit(Temp)^.Count)+SizeOf(PRecordInfoInit(Temp)^.Count));
  292. expectedoffset:=0;
  293. { Process elements with rtti }
  294. for i:=1 to EleCount Do
  295. begin
  296. Offset:=PRecordElement(Temp)^.Offset;
  297. if Offset>expectedoffset then
  298. move((Src+expectedoffset)^,(Dest+expectedoffset)^,Offset-expectedoffset);
  299. expectedoffset:=Offset+int_Copy(Src+Offset,Dest+Offset,PRecordElement(Temp)^.TypeInfo^);
  300. Inc(PRecordElement(Temp));
  301. end;
  302. { elements remaining? }
  303. if result>expectedoffset then
  304. move((Src+expectedoffset)^,(Dest+expectedoffset)^,Result-expectedoffset);
  305. end;
  306. end;
  307. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  308. tkDynArray:
  309. fpc_dynarray_assign(PPointer(Dest)^,PPointer(Src)^,typeinfo);
  310. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  311. {$ifdef FPC_HAS_FEATURE_CLASSES}
  312. tkInterface:
  313. fpc_intf_assign(PPointer(Dest)^,PPointer(Src)^);
  314. {$endif FPC_HAS_FEATURE_CLASSES}
  315. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  316. tkVariant:
  317. begin
  318. VarCopyProc(pvardata(dest)^,pvardata(src)^);
  319. result:=sizeof(tvardata);
  320. end;
  321. {$endif FPC_HAS_FEATURE_VARIANTS}
  322. end;
  323. end;
  324. { For internal use by the compiler, because otherwise $x- can cause trouble. }
  325. { Generally disabling extended syntax checking for all compilerprocs may }
  326. { have unintended side-effects }
  327. procedure fpc_Copy_proc (Src, Dest, TypeInfo : Pointer);compilerproc; inline;
  328. begin
  329. int_copy(src,dest,typeinfo);
  330. end;
  331. {$ifdef FPC_MANAGED_MOVE}
  332. function fpc_Copy_with_move_semantics (Src, Dest, TypeInfo : Pointer) : SizeInt;[Public,alias : 'FPC_COPY_WITH_MOVE_SEMANTICS']; compilerproc;
  333. var
  334. tki : pointer;
  335. begin
  336. tki:=aligntoqword(typeInfo+2+PByte(typeInfo)[1]);
  337. if PTypeKind(TypeInfo)^=tkArray then { Only tkArray, tkObject, tkRecord are possible, search for 'fpc_copy_proc' in compiler/nld.pas. }
  338. result:=PArrayInfo(tki)^.Size
  339. else
  340. result:=PRecordInfoInit(tki)^.Size;
  341. int_finalize(Dest,TypeInfo);
  342. move(src^,dest^,result);
  343. int_initialize(Src,TypeInfo);
  344. end;
  345. procedure fpc_Copy_with_move_semantics_proc (Src, Dest, TypeInfo : Pointer);compilerproc; inline;
  346. begin
  347. int_Copy_with_move_semantics(src,dest,typeinfo);
  348. end;
  349. {$endif FPC_MANAGED_MOVE}
  350. procedure fpc_initialize_array(data,typeinfo : pointer;count : SizeInt); [public,alias:'FPC_INITIALIZE_ARRAY']; compilerproc;
  351. var
  352. i, size : SizeInt;
  353. begin
  354. if RTTIManagementAndSize(typeinfo, rotInitialize, size, manBuiltin)<>manNone then
  355. for i:=0 to count-1 do
  356. int_initialize(data+size*i,typeinfo);
  357. end;
  358. procedure fpc_finalize_array(data,typeinfo : pointer;count : SizeInt); [Public,Alias:'FPC_FINALIZE_ARRAY']; compilerproc;
  359. var
  360. i, size : SizeInt;
  361. begin
  362. if RTTIManagementAndSize(typeinfo, rotFinalize, size, manBuiltin)<>manNone then
  363. for i:=0 to count-1 do
  364. int_finalize(data+size*i,typeinfo);
  365. end;
  366. procedure fpc_addref_array(data,typeinfo: pointer; count: SizeInt); [public,alias:'FPC_ADDREF_ARRAY']; compilerproc;
  367. var
  368. i, size : SizeInt;
  369. begin
  370. if RTTIManagementAndSize(typeinfo, rotAddRef, size, manBuiltin)<>manNone then
  371. for i:=0 to count-1 do
  372. int_addref(data+size*i,typeinfo);
  373. end;
  374. { The following two procedures are now obsolete, needed only for bootstrapping }
  375. procedure fpc_decref (Data, TypeInfo : Pointer);[Public,alias : 'FPC_DECREF']; compilerproc;
  376. begin
  377. int_finalize(Data,TypeInfo);
  378. end;
  379. procedure fpc_decref_array(data,typeinfo: pointer; count: SizeInt); [public,alias:'FPC_DECREF_ARRAY']; compilerproc;
  380. begin
  381. int_finalizeArray(data,typeinfo,count);
  382. end;
  383. procedure InitializeArray(p, typeInfo: Pointer; count: SizeInt);
  384. external name 'FPC_INITIALIZE_ARRAY';
  385. procedure FinalizeArray(p, typeInfo: Pointer; count: SizeInt);
  386. external name 'FPC_FINALIZE_ARRAY';
  387. procedure CopyArray(dest, source, typeInfo: Pointer; count: SizeInt);
  388. var
  389. i, size: SizeInt;
  390. begin
  391. if RTTIManagementAndSize(typeinfo, rotCopy, size, manBuiltin)<>manNone then
  392. for i:=0 to count-1 do
  393. int_Copy(source+size*i, dest+size*i, typeInfo);
  394. end;