rtti.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 fpc_Initialize (Data,TypeInfo : pointer);[Public,Alias : 'FPC_INITIALIZE']; compilerproc;
  95. var
  96. ri: PRecordInfoInit;
  97. begin
  98. case PTypeKind(TypeInfo)^ of
  99. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  100. tkDynArray,
  101. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  102. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  103. tkAstring,
  104. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  105. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  106. tkWstring,tkUString,
  107. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  108. tkInterface:
  109. PPAnsiChar(Data)^:=Nil;
  110. tkArray:
  111. with PArrayInfo(aligntoqword(typeInfo+2+PByte(typeInfo)[1]))^ do
  112. int_InitializeArray(data,ElInfo^,ElCount);
  113. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  114. tkObject,
  115. {$endif FPC_HAS_FEATURE_OBJECTS}
  116. tkRecord:
  117. begin
  118. ri:=RTTIRecordInfoInit(typeinfo);
  119. recordrtti(data,ri,@int_initialize);
  120. if Assigned(ri^.recordop) and Assigned(ri^.recordop^.Initialize) then
  121. ri^.recordop^.Initialize(data);
  122. end;
  123. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  124. tkVariant:
  125. variant_init(PVarData(Data)^);
  126. {$endif FPC_HAS_FEATURE_VARIANTS}
  127. end;
  128. end;
  129. Procedure fpc_finalize (Data,TypeInfo: Pointer);[Public,Alias : 'FPC_FINALIZE']; compilerproc;
  130. var
  131. ri: PRecordInfoInit;
  132. begin
  133. case PTypeKind(TypeInfo)^ of
  134. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  135. tkAstring :
  136. fpc_AnsiStr_Decr_Ref(PPointer(Data)^);
  137. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  138. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  139. tkUstring :
  140. fpc_UnicodeStr_Decr_Ref(PPointer(Data)^);
  141. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  142. tkWstring :
  143. fpc_WideStr_Decr_Ref(PPointer(Data)^);
  144. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  145. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  146. tkArray :
  147. with PArrayInfo(aligntoqword(typeInfo+2+PByte(typeInfo)[1]))^ do
  148. int_FinalizeArray(data,ElInfo^,ElCount);
  149. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  150. tkObject,
  151. {$endif FPC_HAS_FEATURE_OBJECTS}
  152. tkRecord:
  153. begin
  154. ri:=RTTIRecordInfoInit(typeinfo);
  155. if Assigned(ri^.recordop) and Assigned(ri^.recordop^.Finalize) then
  156. ri^.recordop^.Finalize(data);
  157. recordrtti(data,ri,@int_finalize);
  158. end;
  159. {$ifdef FPC_HAS_FEATURE_CLASSES}
  160. tkInterface:
  161. Intf_Decr_Ref(PPointer(Data)^);
  162. {$endif FPC_HAS_FEATURE_CLASSES}
  163. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  164. tkDynArray:
  165. fpc_dynarray_clear(PPointer(Data)^,TypeInfo);
  166. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  167. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  168. tkVariant:
  169. variant_clear(PVarData(Data)^);
  170. {$endif FPC_HAS_FEATURE_VARIANTS}
  171. end;
  172. end;
  173. Procedure fpc_Addref (Data,TypeInfo : Pointer); [Public,alias : 'FPC_ADDREF']; compilerproc;
  174. var
  175. ri: PRecordInfoInit;
  176. begin
  177. case PTypeKind(TypeInfo)^ of
  178. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  179. tkAstring :
  180. fpc_AnsiStr_Incr_Ref(PPointer(Data)^);
  181. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  182. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  183. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  184. tkWstring :
  185. fpc_WideStr_Incr_Ref(PPointer(Data)^);
  186. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  187. tkUstring :
  188. fpc_UnicodeStr_Incr_Ref(PPointer(Data)^);
  189. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  190. tkArray :
  191. with PArrayInfo(aligntoqword(typeInfo+2+PByte(typeInfo)[1]))^ do
  192. int_AddRefArray(data,ElInfo^,ElCount);
  193. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  194. tkobject,
  195. {$endif FPC_HAS_FEATURE_OBJECTS}
  196. tkrecord :
  197. begin
  198. ri:=RTTIRecordInfoInit(typeinfo);
  199. recordrtti(data,ri,@int_addref);
  200. if Assigned(ri^.recordop) and Assigned(ri^.recordop^.AddRef) then
  201. ri^.recordop^.AddRef(Data);
  202. end;
  203. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  204. tkDynArray:
  205. fpc_dynarray_incr_ref(PPointer(Data)^);
  206. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  207. {$ifdef FPC_HAS_FEATURE_CLASSES}
  208. tkInterface:
  209. Intf_Incr_Ref(PPointer(Data)^);
  210. {$endif FPC_HAS_FEATURE_CLASSES}
  211. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  212. tkVariant:
  213. variant_addref(pvardata(Data)^);
  214. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  215. end;
  216. end;
  217. Function fpc_Copy (Src, Dest, TypeInfo : Pointer) : SizeInt;[Public,alias : 'FPC_COPY']; compilerproc;
  218. var
  219. copiedsize,
  220. expectedoffset,
  221. EleCount,
  222. offset,
  223. i: SizeInt;
  224. Temp,
  225. info: pointer;
  226. begin
  227. result:=sizeof(pointer);
  228. case PTypeKind(TypeInfo)^ of
  229. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  230. tkAstring:
  231. fpc_AnsiStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  232. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  233. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  234. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  235. tkWstring:
  236. fpc_WideStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  237. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  238. tkUstring:
  239. fpc_UnicodeStr_Assign(PPointer(Dest)^,PPointer(Src)^);
  240. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  241. tkArray:
  242. begin
  243. Temp:=aligntoqword(typeInfo+2+PByte(typeInfo)[1]);
  244. Result:=PArrayInfo(Temp)^.Size;
  245. EleCount:=PArrayInfo(Temp)^.ElCount;
  246. { no elements to process => exit }
  247. if EleCount = 0 then
  248. Exit;
  249. Info:=PArrayInfo(Temp)^.ElInfo^;
  250. copiedsize:=Result div EleCount;
  251. Offset:=0;
  252. { Process elements }
  253. for I:=1 to EleCount do
  254. begin
  255. int_Copy(Src+Offset,Dest+Offset,Info);
  256. inc(Offset,copiedsize);
  257. end;
  258. end;
  259. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  260. tkobject,
  261. {$endif FPC_HAS_FEATURE_OBJECTS}
  262. tkrecord:
  263. begin
  264. Temp:=RTTIRecordInfoInit(typeinfo);
  265. Result:=PRecordInfoInit(Temp)^.Size;
  266. if Assigned(PRecordInfoInit(Temp)^.recordop) and Assigned(PRecordInfoInit(Temp)^.recordop^.Copy) then
  267. PRecordInfoInit(Temp)^.recordop^.Copy(Src,Dest)
  268. else
  269. begin
  270. EleCount:=PRecordInfoInit(Temp)^.Count;
  271. { Get element info, hacky, but what else can we do? }
  272. Temp:=AlignTypeData(Pointer(@PRecordInfoInit(Temp)^.Count)+SizeOf(PRecordInfoInit(Temp)^.Count));
  273. expectedoffset:=0;
  274. { Process elements with rtti }
  275. for i:=1 to EleCount Do
  276. begin
  277. Offset:=PRecordElement(Temp)^.Offset;
  278. if Offset>expectedoffset then
  279. move((Src+expectedoffset)^,(Dest+expectedoffset)^,Offset-expectedoffset);
  280. expectedoffset:=Offset+int_Copy(Src+Offset,Dest+Offset,PRecordElement(Temp)^.TypeInfo^);
  281. Inc(PRecordElement(Temp));
  282. end;
  283. { elements remaining? }
  284. if result>expectedoffset then
  285. move((Src+expectedoffset)^,(Dest+expectedoffset)^,Result-expectedoffset);
  286. end;
  287. end;
  288. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  289. tkDynArray:
  290. fpc_dynarray_assign(PPointer(Dest)^,PPointer(Src)^,typeinfo);
  291. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  292. {$ifdef FPC_HAS_FEATURE_CLASSES}
  293. tkInterface:
  294. fpc_intf_assign(PPointer(Dest)^,PPointer(Src)^);
  295. {$endif FPC_HAS_FEATURE_CLASSES}
  296. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  297. tkVariant:
  298. begin
  299. VarCopyProc(pvardata(dest)^,pvardata(src)^);
  300. result:=sizeof(tvardata);
  301. end;
  302. {$endif FPC_HAS_FEATURE_VARIANTS}
  303. end;
  304. end;
  305. { For internal use by the compiler, because otherwise $x- can cause trouble. }
  306. { Generally disabling extended syntax checking for all compilerprocs may }
  307. { have unintended side-effects }
  308. procedure fpc_Copy_proc (Src, Dest, TypeInfo : Pointer);compilerproc; inline;
  309. begin
  310. int_copy(src,dest,typeinfo);
  311. end;
  312. {$ifdef FPC_MANAGED_MOVE}
  313. function fpc_Copy_with_move_semantics (Src, Dest, TypeInfo : Pointer) : SizeInt;[Public,alias : 'FPC_COPY_WITH_MOVE_SEMANTICS']; compilerproc;
  314. var
  315. tki : pointer;
  316. begin
  317. tki:=aligntoqword(typeInfo+2+PByte(typeInfo)[1]);
  318. if PTypeKind(TypeInfo)^=tkArray then { Only tkArray, tkObject, tkRecord are possible, search for 'fpc_copy_proc' in compiler/nld.pas. }
  319. result:=PArrayInfo(tki)^.Size
  320. else
  321. result:=PRecordInfoInit(tki)^.Size;
  322. int_finalize(Dest,TypeInfo);
  323. move(src^,dest^,result);
  324. int_initialize(Src,TypeInfo);
  325. end;
  326. procedure fpc_Copy_with_move_semantics_proc (Src, Dest, TypeInfo : Pointer);compilerproc; inline;
  327. begin
  328. int_Copy_with_move_semantics(src,dest,typeinfo);
  329. end;
  330. {$endif FPC_MANAGED_MOVE}
  331. procedure fpc_initialize_array(data,typeinfo : pointer;count : SizeInt); [public,alias:'FPC_INITIALIZE_ARRAY']; compilerproc;
  332. var
  333. sample,size,i : SizeInt;
  334. begin
  335. sample:=RTTIManagedSizes[PTypeKind(typeinfo)^];
  336. if sample<>RTTISpecialSize then
  337. FillChar(data^,sample*count,0)
  338. else if RTTIManagementAndSize(typeinfo, rotInitialize, size, false) then
  339. for i:=0 to count-1 do
  340. int_initialize(data+size*i,typeinfo);
  341. end;
  342. procedure fpc_finalize_array(data,typeinfo : pointer;count : SizeInt); [Public,Alias:'FPC_FINALIZE_ARRAY']; compilerproc;
  343. var
  344. size,i : SizeInt;
  345. begin
  346. if RTTIManagementAndSize(typeinfo, rotFinalize, size, false) then
  347. for i:=0 to count-1 do
  348. int_finalize(data+size*i,typeinfo);
  349. end;
  350. procedure fpc_addref_array(data,typeinfo: pointer; count: SizeInt); [public,alias:'FPC_ADDREF_ARRAY']; compilerproc;
  351. var
  352. size,i : SizeInt;
  353. begin
  354. if RTTIManagementAndSize(typeinfo, rotAddRef, size, false) then
  355. for i:=0 to count-1 do
  356. int_addref(data+size*i,typeinfo);
  357. end;
  358. { The following two procedures are now obsolete, needed only for bootstrapping }
  359. procedure fpc_decref (Data, TypeInfo : Pointer);[Public,alias : 'FPC_DECREF']; compilerproc;
  360. begin
  361. int_finalize(Data,TypeInfo);
  362. end;
  363. procedure fpc_decref_array(data,typeinfo: pointer; count: SizeInt); [public,alias:'FPC_DECREF_ARRAY']; compilerproc;
  364. begin
  365. int_finalizeArray(data,typeinfo,count);
  366. end;
  367. procedure InitializeArray(p, typeInfo: Pointer; count: SizeInt);
  368. external name 'FPC_INITIALIZE_ARRAY';
  369. procedure FinalizeArray(p, typeInfo: Pointer; count: SizeInt);
  370. external name 'FPC_FINALIZE_ARRAY';
  371. procedure CopyArray(dest, source, typeInfo: Pointer; count: SizeInt);
  372. var
  373. i, size: SizeInt;
  374. begin
  375. if RTTIManagementAndSize(typeinfo, rotCopy, size, false) then
  376. for i:=0 to count-1 do
  377. int_Copy(source+size*i, dest+size*i, typeInfo);
  378. end;