rtti.inc 15 KB

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