ncgrtti.pas 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Routines for the code generation of RTTI data structures
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncgrtti;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,constexp,
  22. aasmbase,
  23. symbase,symconst,symtype,symdef;
  24. type
  25. { TRTTIWriter }
  26. TRTTIWriter=class
  27. private
  28. function fields_count(st:tsymtable;rt:trttitype):longint;
  29. procedure fields_write_rtti(st:tsymtable;rt:trttitype);
  30. procedure fields_write_rtti_data(st:tsymtable;rt:trttitype);
  31. procedure write_rtti_extrasyms(def:Tdef;rt:Trttitype;mainrtti:Tasmsymbol);
  32. procedure published_write_rtti(st:tsymtable;rt:trttitype);
  33. function published_properties_count(st:tsymtable):longint;
  34. procedure published_properties_write_rtti_data(propnamelist:TFPHashObjectList;st:tsymtable);
  35. procedure collect_propnamelist(propnamelist:TFPHashObjectList;objdef:tobjectdef);
  36. procedure write_rtti_name(def:tdef);
  37. procedure write_rtti_data(def:tdef;rt:trttitype);
  38. procedure write_child_rtti_data(def:tdef;rt:trttitype);
  39. function ref_rtti(def:tdef;rt:trttitype):tasmsymbol;
  40. public
  41. procedure write_rtti(def:tdef;rt:trttitype);
  42. function get_rtti_label(def:tdef;rt:trttitype):tasmsymbol;
  43. function get_rtti_label_ord2str(def:tdef;rt:trttitype):tasmsymbol;
  44. function get_rtti_label_str2ord(def:tdef;rt:trttitype):tasmsymbol;
  45. end;
  46. var
  47. RTTIWriter : TRTTIWriter;
  48. implementation
  49. uses
  50. cutils,
  51. globals,globtype,verbose,systems,
  52. fmodule,
  53. symsym,
  54. aasmtai,aasmdata
  55. ;
  56. const
  57. rttidefstate : array[trttitype] of tdefstate = (ds_rtti_table_written,ds_init_table_written);
  58. type
  59. TPropNameListItem = class(TFPHashObject)
  60. propindex : longint;
  61. propowner : TSymtable;
  62. end;
  63. {***************************************************************************
  64. TRTTIWriter
  65. ***************************************************************************}
  66. procedure TRTTIWriter.write_rtti_name(def:tdef);
  67. var
  68. hs : string;
  69. begin
  70. { name }
  71. if assigned(def.typesym) then
  72. begin
  73. hs:=ttypesym(def.typesym).realname;
  74. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(chr(length(hs))+hs));
  75. end
  76. else
  77. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(#0));
  78. end;
  79. function TRTTIWriter.fields_count(st:tsymtable;rt:trttitype):longint;
  80. var
  81. i : longint;
  82. sym : tsym;
  83. begin
  84. result:=0;
  85. for i:=0 to st.SymList.Count-1 do
  86. begin
  87. sym:=tsym(st.SymList[i]);
  88. if (rt=fullrtti) or
  89. (
  90. (tsym(sym).typ=fieldvarsym) and
  91. tfieldvarsym(sym).vardef.needs_inittable
  92. ) then
  93. inc(result);
  94. end;
  95. end;
  96. procedure TRTTIWriter.fields_write_rtti_data(st:tsymtable;rt:trttitype);
  97. var
  98. i : longint;
  99. sym : tsym;
  100. begin
  101. for i:=0 to st.SymList.Count-1 do
  102. begin
  103. sym:=tsym(st.SymList[i]);
  104. if (rt=fullrtti) or
  105. (
  106. (tsym(sym).typ=fieldvarsym) and
  107. tfieldvarsym(sym).vardef.needs_inittable
  108. ) then
  109. begin
  110. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(tfieldvarsym(sym).vardef,rt)));
  111. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tfieldvarsym(sym).fieldoffset));
  112. end;
  113. end;
  114. end;
  115. procedure TRTTIWriter.fields_write_rtti(st:tsymtable;rt:trttitype);
  116. var
  117. i : longint;
  118. sym : tsym;
  119. begin
  120. for i:=0 to st.SymList.Count-1 do
  121. begin
  122. sym:=tsym(st.SymList[i]);
  123. if (rt=fullrtti) or
  124. (
  125. (tsym(sym).typ=fieldvarsym) and
  126. tfieldvarsym(sym).vardef.needs_inittable
  127. ) then
  128. write_rtti(tfieldvarsym(sym).vardef,rt);
  129. end;
  130. end;
  131. procedure TRTTIWriter.published_write_rtti(st:tsymtable;rt:trttitype);
  132. var
  133. i : longint;
  134. sym : tsym;
  135. begin
  136. for i:=0 to st.SymList.Count-1 do
  137. begin
  138. sym:=tsym(st.SymList[i]);
  139. if (sp_published in tsym(sym).symoptions) then
  140. begin
  141. case tsym(sym).typ of
  142. propertysym:
  143. write_rtti(tpropertysym(sym).propdef,rt);
  144. fieldvarsym:
  145. write_rtti(tfieldvarsym(sym).vardef,rt);
  146. end;
  147. end;
  148. end;
  149. end;
  150. function TRTTIWriter.published_properties_count(st:tsymtable):longint;
  151. var
  152. i : longint;
  153. sym : tsym;
  154. begin
  155. result:=0;
  156. for i:=0 to st.SymList.Count-1 do
  157. begin
  158. sym:=tsym(st.SymList[i]);
  159. if (tsym(sym).typ=propertysym) and
  160. (sp_published in tsym(sym).symoptions) then
  161. inc(result);
  162. end;
  163. end;
  164. procedure TRTTIWriter.collect_propnamelist(propnamelist:TFPHashObjectList;objdef:tobjectdef);
  165. var
  166. i : longint;
  167. sym : tsym;
  168. pn : tpropnamelistitem;
  169. begin
  170. if assigned(objdef.childof) then
  171. collect_propnamelist(propnamelist,objdef.childof);
  172. for i:=0 to objdef.symtable.SymList.Count-1 do
  173. begin
  174. sym:=tsym(objdef.symtable.SymList[i]);
  175. if (tsym(sym).typ=propertysym) and
  176. (sp_published in tsym(sym).symoptions) then
  177. begin
  178. pn:=TPropNameListItem(propnamelist.Find(tsym(sym).name));
  179. if not assigned(pn) then
  180. begin
  181. pn:=tpropnamelistitem.create(propnamelist,tsym(sym).name);
  182. pn.propindex:=propnamelist.count-1;
  183. pn.propowner:=tsym(sym).owner;
  184. end;
  185. end;
  186. end;
  187. end;
  188. procedure TRTTIWriter.published_properties_write_rtti_data(propnamelist:TFPHashObjectList;st:tsymtable);
  189. var
  190. i : longint;
  191. sym : tsym;
  192. proctypesinfo : byte;
  193. propnameitem : tpropnamelistitem;
  194. procedure writeaccessproc(pap:tpropaccesslisttypes; shiftvalue : byte; unsetvalue: byte);
  195. var
  196. typvalue : byte;
  197. hp : ppropaccesslistitem;
  198. address,space : longint;
  199. def : tdef;
  200. hpropsym : tpropertysym;
  201. propaccesslist : tpropaccesslist;
  202. begin
  203. hpropsym:=tpropertysym(sym);
  204. repeat
  205. propaccesslist:=hpropsym.propaccesslist[pap];
  206. if not propaccesslist.empty then
  207. break;
  208. hpropsym:=hpropsym.overridenpropsym;
  209. until not assigned(hpropsym);
  210. if not(assigned(propaccesslist) and assigned(propaccesslist.firstsym)) then
  211. begin
  212. current_asmdata.asmlists[al_rtti].concat(Tai_const.create(aitconst_ptr,unsetvalue));
  213. typvalue:=3;
  214. end
  215. else if propaccesslist.firstsym^.sym.typ=fieldvarsym then
  216. begin
  217. address:=0;
  218. hp:=propaccesslist.firstsym;
  219. def:=nil;
  220. while assigned(hp) do
  221. begin
  222. case hp^.sltype of
  223. sl_load :
  224. begin
  225. def:=tfieldvarsym(hp^.sym).vardef;
  226. inc(address,tfieldvarsym(hp^.sym).fieldoffset);
  227. end;
  228. sl_subscript :
  229. begin
  230. if not(assigned(def) and (def.typ=recorddef)) then
  231. internalerror(200402171);
  232. inc(address,tfieldvarsym(hp^.sym).fieldoffset);
  233. def:=tfieldvarsym(hp^.sym).vardef;
  234. end;
  235. sl_vec :
  236. begin
  237. if not(assigned(def) and (def.typ=arraydef)) then
  238. internalerror(200402172);
  239. def:=tarraydef(def).elementdef;
  240. {Hp.value is a Tconstexprint, which can be rather large,
  241. sanity check for longint overflow.}
  242. space:=(high(address)-address) div def.size;
  243. if int64(space)<hp^.value then
  244. internalerror(200706101);
  245. inc(address,int64(def.size*hp^.value));
  246. end;
  247. end;
  248. hp:=hp^.next;
  249. end;
  250. current_asmdata.asmlists[al_rtti].concat(Tai_const.create(aitconst_ptr,address));
  251. typvalue:=0;
  252. end
  253. else
  254. begin
  255. { When there was an error then procdef is not assigned }
  256. if not assigned(propaccesslist.procdef) then
  257. exit;
  258. if not(po_virtualmethod in tprocdef(propaccesslist.procdef).procoptions) then
  259. begin
  260. current_asmdata.asmlists[al_rtti].concat(Tai_const.createname(tprocdef(propaccesslist.procdef).mangledname,0));
  261. typvalue:=1;
  262. end
  263. else
  264. begin
  265. { virtual method, write vmt offset }
  266. current_asmdata.asmlists[al_rtti].concat(Tai_const.create(aitconst_ptr,
  267. tprocdef(propaccesslist.procdef)._class.vmtmethodoffset(tprocdef(propaccesslist.procdef).extnumber)));
  268. typvalue:=2;
  269. end;
  270. end;
  271. proctypesinfo:=proctypesinfo or (typvalue shl shiftvalue);
  272. end;
  273. begin
  274. for i:=0 to st.SymList.Count-1 do
  275. begin
  276. sym:=tsym(st.SymList[i]);
  277. if (sym.typ=propertysym) and
  278. (sp_published in sym.symoptions) then
  279. begin
  280. if ppo_indexed in tpropertysym(sym).propoptions then
  281. proctypesinfo:=$40
  282. else
  283. proctypesinfo:=0;
  284. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(tpropertysym(sym).propdef,fullrtti)));
  285. writeaccessproc(palt_read,0,0);
  286. writeaccessproc(palt_write,2,0);
  287. { is it stored ? }
  288. if not(ppo_stored in tpropertysym(sym).propoptions) then
  289. begin
  290. { no, so put a constant zero }
  291. current_asmdata.asmlists[al_rtti].concat(Tai_const.create(aitconst_ptr,0));
  292. proctypesinfo:=proctypesinfo or (3 shl 4);
  293. end
  294. else
  295. writeaccessproc(palt_stored,4,1); { maybe; if no procedure put a constant 1 (=true) }
  296. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tpropertysym(sym).index));
  297. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tpropertysym(sym).default));
  298. propnameitem:=TPropNameListItem(propnamelist.Find(tpropertysym(sym).name));
  299. if not assigned(propnameitem) then
  300. internalerror(200512201);
  301. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(propnameitem.propindex));
  302. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(proctypesinfo));
  303. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(tpropertysym(sym).realname)));
  304. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(tpropertysym(sym).realname));
  305. if (tf_requires_proper_alignment in target_info.flags) then
  306. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  307. end;
  308. end;
  309. end;
  310. procedure TRTTIWriter.write_rtti_data(def:tdef;rt:trttitype);
  311. procedure unknown_rtti(def:tstoreddef);
  312. begin
  313. current_asmdata.asmlists[al_rtti].concat(tai_const.create_8bit(tkUnknown));
  314. write_rtti_name(def);
  315. end;
  316. procedure variantdef_rtti(def:tvariantdef);
  317. begin
  318. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkVariant));
  319. end;
  320. procedure stringdef_rtti(def:tstringdef);
  321. begin
  322. case def.stringtype of
  323. st_ansistring:
  324. begin
  325. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkAString));
  326. write_rtti_name(def);
  327. end;
  328. st_widestring:
  329. begin
  330. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkWString));
  331. write_rtti_name(def);
  332. end;
  333. st_unicodestring:
  334. begin
  335. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkUString));
  336. write_rtti_name(def);
  337. end;
  338. st_longstring:
  339. begin
  340. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkLString));
  341. write_rtti_name(def);
  342. end;
  343. st_shortstring:
  344. begin
  345. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkSString));
  346. write_rtti_name(def);
  347. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.len));
  348. if (tf_requires_proper_alignment in target_info.flags) then
  349. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  350. end;
  351. end;
  352. end;
  353. procedure enumdef_rtti(def:tenumdef);
  354. var
  355. hp : tenumsym;
  356. begin
  357. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkEnumeration));
  358. write_rtti_name(def);
  359. if (tf_requires_proper_alignment in target_info.flags) then
  360. current_asmdata.asmlists[al_rtti].concat(Cai_align.Create(sizeof(TConstPtrUInt)));
  361. case longint(def.size) of
  362. 1 :
  363. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUByte));
  364. 2 :
  365. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUWord));
  366. 4 :
  367. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otULong));
  368. end;
  369. if (tf_requires_proper_alignment in target_info.flags) then
  370. current_asmdata.asmlists[al_rtti].concat(Cai_align.Create(longint(def.size)));
  371. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.min));
  372. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.max));
  373. if (tf_requires_proper_alignment in target_info.flags) then
  374. current_asmdata.asmlists[al_rtti].concat(Cai_align.Create(sizeof(TConstPtrUint)));
  375. if assigned(def.basedef) then
  376. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.basedef,rt)))
  377. else
  378. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  379. hp:=tenumsym(def.firstenum);
  380. while assigned(hp) do
  381. begin
  382. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(hp.realname)));
  383. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(hp.realname));
  384. hp:=hp.nextenum;
  385. end;
  386. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(0));
  387. end;
  388. procedure orddef_rtti(def:torddef);
  389. procedure dointeger;
  390. const
  391. trans : array[tordtype] of byte =
  392. (otUByte{otNone},
  393. otUByte,otUWord,otULong,otUByte{otNone},
  394. otSByte,otSWord,otSLong,otUByte{otNone},
  395. otUByte,otSByte,otSWord,otSLong,otSByte,
  396. otUByte,otUWord,otUByte);
  397. begin
  398. write_rtti_name(def);
  399. if (tf_requires_proper_alignment in target_info.flags) then
  400. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  401. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(byte(trans[def.ordtype])));
  402. if (tf_requires_proper_alignment in target_info.flags) then
  403. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  404. {Convert to longint to smuggle values in high(longint)+1..high(cardinal) into asmlist.}
  405. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(def.low.svalue)));
  406. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(def.high.svalue)));
  407. end;
  408. begin
  409. case def.ordtype of
  410. s64bit :
  411. begin
  412. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkInt64));
  413. write_rtti_name(def);
  414. if (tf_requires_proper_alignment in target_info.flags) then
  415. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  416. { low }
  417. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(def.low.svalue));
  418. { high }
  419. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(def.high.svalue));
  420. end;
  421. u64bit :
  422. begin
  423. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkQWord));
  424. write_rtti_name(def);
  425. if (tf_requires_proper_alignment in target_info.flags) then
  426. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  427. {use svalue because Create_64bit accepts int64, prevents range checks}
  428. { low }
  429. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(def.low.svalue));
  430. { high }
  431. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(def.high.svalue));
  432. end;
  433. pasbool:
  434. begin
  435. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkBool));
  436. dointeger;
  437. end;
  438. uchar:
  439. begin
  440. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkChar));
  441. dointeger;
  442. end;
  443. uwidechar:
  444. begin
  445. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkWChar));
  446. dointeger;
  447. end;
  448. scurrency:
  449. begin
  450. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkFloat));
  451. write_rtti_name(def);
  452. if (tf_requires_proper_alignment in target_info.flags) then
  453. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  454. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(ftCurr));
  455. end;
  456. else
  457. begin
  458. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkInteger));
  459. dointeger;
  460. end;
  461. end;
  462. end;
  463. procedure floatdef_rtti(def:tfloatdef);
  464. const
  465. {tfloattype = (s32real,s64real,s80real,s64bit,s128bit);}
  466. translate : array[tfloattype] of byte =
  467. (ftSingle,ftDouble,ftExtended,ftComp,ftCurr,ftFloat128);
  468. begin
  469. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkFloat));
  470. write_rtti_name(def);
  471. if (tf_requires_proper_alignment in target_info.flags) then
  472. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  473. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(translate[def.floattype]));
  474. end;
  475. procedure setdef_rtti(def:tsetdef);
  476. begin
  477. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkSet));
  478. write_rtti_name(def);
  479. if (tf_requires_proper_alignment in target_info.flags) then
  480. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  481. case def.size of
  482. 1:
  483. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUByte));
  484. 2:
  485. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUWord));
  486. 4:
  487. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otULong));
  488. end;
  489. if (tf_requires_proper_alignment in target_info.flags) then
  490. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  491. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.elementdef,rt)));
  492. end;
  493. procedure arraydef_rtti(def:tarraydef);
  494. begin
  495. if ado_IsDynamicArray in def.arrayoptions then
  496. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkdynarray))
  497. else
  498. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkarray));
  499. write_rtti_name(def);
  500. if (tf_requires_proper_alignment in target_info.flags) then
  501. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  502. { size of elements }
  503. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_pint(def.elesize));
  504. if not(ado_IsDynamicArray in def.arrayoptions) then
  505. begin
  506. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_pint(pint(def.elecount)));
  507. { element type }
  508. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.elementdef,rt)));
  509. end
  510. else
  511. { write a delphi almost compatible dyn. array entry:
  512. there are two types, eltype and eltype2, the latter is nil if the element type needs
  513. no finalization, the former is always valid, delphi has this swapped, but for
  514. compatibility with older fpc versions we do it different, to be delphi compatible,
  515. the names are swapped in typinfo.pp
  516. }
  517. begin
  518. { element type }
  519. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.elementdef,rt)));
  520. end;
  521. { variant type }
  522. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tstoreddef(def.elementdef).getvardef));
  523. if ado_IsDynamicArray in def.arrayoptions then
  524. begin
  525. { element type }
  526. if def.elementdef.needs_inittable then
  527. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.elementdef,rt)))
  528. else
  529. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_pint(0));
  530. { dummy DynUnitName }
  531. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(0));
  532. end;
  533. end;
  534. procedure recorddef_rtti(def:trecorddef);
  535. var
  536. fieldcnt : longint;
  537. begin
  538. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkrecord));
  539. write_rtti_name(def);
  540. if (tf_requires_proper_alignment in target_info.flags) then
  541. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  542. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  543. fieldcnt:=fields_count(def.symtable,rt);
  544. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(fieldcnt));
  545. fields_write_rtti_data(def.symtable,rt);
  546. end;
  547. procedure procvardef_rtti(def:tprocvardef);
  548. procedure write_para(parasym:tparavarsym);
  549. var
  550. paraspec : byte;
  551. begin
  552. { only store user visible parameters }
  553. if not(vo_is_hidden_para in parasym.varoptions) then
  554. begin
  555. case parasym.varspez of
  556. vs_value: paraspec := 0;
  557. vs_const: paraspec := pfConst;
  558. vs_var : paraspec := pfVar;
  559. vs_out : paraspec := pfOut;
  560. end;
  561. { write flags for current parameter }
  562. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(paraspec));
  563. { write name of current parameter }
  564. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(parasym.realname)));
  565. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(parasym.realname));
  566. { write name of type of current parameter }
  567. write_rtti_name(parasym.vardef);
  568. end;
  569. end;
  570. var
  571. methodkind : byte;
  572. i : integer;
  573. begin
  574. if po_methodpointer in def.procoptions then
  575. begin
  576. { write method id and name }
  577. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkmethod));
  578. write_rtti_name(def);
  579. if (tf_requires_proper_alignment in target_info.flags) then
  580. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  581. { write kind of method (can only be function or procedure)}
  582. if def.returndef = voidtype then
  583. methodkind := mkProcedure
  584. else
  585. methodkind := mkFunction;
  586. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(methodkind));
  587. { write parameter info. The parameters must be written in reverse order
  588. if this method uses right to left parameter pushing! }
  589. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.maxparacount));
  590. if def.proccalloption in pushleftright_pocalls then
  591. begin
  592. for i:=0 to def.paras.count-1 do
  593. write_para(tparavarsym(def.paras[i]));
  594. end
  595. else
  596. begin
  597. for i:=def.paras.count-1 downto 0 do
  598. write_para(tparavarsym(def.paras[i]));
  599. end;
  600. { write name of result type }
  601. write_rtti_name(def.returndef);
  602. end
  603. else
  604. begin
  605. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkprocvar));
  606. write_rtti_name(def);
  607. end;
  608. end;
  609. procedure objectdef_rtti(def:tobjectdef);
  610. procedure objectdef_rtti_class_init(def:tobjectdef);
  611. begin
  612. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  613. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(fields_count(def.symtable,rt)));
  614. fields_write_rtti_data(def.symtable,rt);
  615. end;
  616. procedure objectdef_rtti_interface_init(def:tobjectdef);
  617. begin
  618. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  619. end;
  620. procedure objectdef_rtti_class_full(def:tobjectdef);
  621. var
  622. propnamelist : TFPHashObjectList;
  623. begin
  624. { Collect unique property names with nameindex }
  625. propnamelist:=TFPHashObjectList.Create;
  626. collect_propnamelist(propnamelist,def);
  627. if (oo_has_vmt in def.objectoptions) then
  628. current_asmdata.asmlists[al_rtti].concat(Tai_const.Createname(def.vmt_mangledname,0))
  629. else
  630. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  631. { write parent typeinfo }
  632. if assigned(def.childof) and
  633. (oo_can_have_published in def.childof.objectoptions) then
  634. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.childof,fullrtti)))
  635. else
  636. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  637. { total number of unique properties }
  638. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(propnamelist.count));
  639. { write unit name }
  640. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(current_module.realmodulename^)));
  641. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(current_module.realmodulename^));
  642. if (tf_requires_proper_alignment in target_info.flags) then
  643. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  644. { write published properties for this object }
  645. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(published_properties_count(def.symtable)));
  646. if (tf_requires_proper_alignment in target_info.flags) then
  647. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  648. published_properties_write_rtti_data(propnamelist,def.symtable);
  649. propnamelist.free;
  650. end;
  651. procedure objectdef_rtti_interface_full(def:tobjectdef);
  652. var
  653. i : longint;
  654. propnamelist : TFPHashObjectList;
  655. begin
  656. { Collect unique property names with nameindex }
  657. propnamelist:=TFPHashObjectList.Create;
  658. collect_propnamelist(propnamelist,def);
  659. { write parent typeinfo }
  660. if assigned(def.childof) then
  661. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.childof,fullrtti)))
  662. else
  663. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  664. { interface: write flags, iid and iidstr }
  665. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(
  666. { ugly, but working }
  667. {$ifdef USE_PACKSET1}
  668. byte([
  669. {$else USE_PACKSET1}
  670. longint([
  671. {$endif USE_PACKSET1}
  672. TCompilerIntfFlag(ord(ifHasGuid)*ord(assigned(def.iidguid))),
  673. TCompilerIntfFlag(ord(ifHasStrGUID)*ord(assigned(def.iidstr))),
  674. TCompilerIntfFlag(ord(ifDispInterface)*ord(def.objecttype=odt_dispinterface))
  675. ])
  676. {
  677. ifDispatch, }
  678. ));
  679. if (tf_requires_proper_alignment in target_info.flags) then
  680. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  681. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(def.iidguid^.D1)));
  682. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(def.iidguid^.D2));
  683. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(def.iidguid^.D3));
  684. for i:=Low(def.iidguid^.D4) to High(def.iidguid^.D4) do
  685. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.iidguid^.D4[i]));
  686. { write unit name }
  687. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(current_module.realmodulename^)));
  688. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(current_module.realmodulename^));
  689. if (tf_requires_proper_alignment in target_info.flags) then
  690. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  691. { write iidstr }
  692. if assigned(def.iidstr) then
  693. begin
  694. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(def.iidstr^)));
  695. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(def.iidstr^));
  696. end
  697. else
  698. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(0));
  699. if (tf_requires_proper_alignment in target_info.flags) then
  700. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  701. { write published properties for this object }
  702. published_properties_write_rtti_data(propnamelist,def.symtable);
  703. propnamelist.free;
  704. end;
  705. begin
  706. case def.objecttype of
  707. odt_class:
  708. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkclass));
  709. odt_object:
  710. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkobject));
  711. odt_dispinterface,
  712. odt_interfacecom:
  713. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkinterface));
  714. odt_interfacecorba:
  715. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkinterfaceCorba));
  716. else
  717. internalerror(200611034);
  718. end;
  719. { generate the name }
  720. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(def.objrealname^)));
  721. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(def.objrealname^));
  722. if (tf_requires_proper_alignment in target_info.flags) then
  723. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  724. case rt of
  725. initrtti :
  726. begin
  727. if def.objecttype in [odt_class,odt_object] then
  728. objectdef_rtti_class_init(def)
  729. else
  730. objectdef_rtti_interface_init(def);
  731. end;
  732. fullrtti :
  733. begin
  734. if def.objecttype in [odt_class,odt_object] then
  735. objectdef_rtti_class_full(def)
  736. else
  737. objectdef_rtti_interface_full(def);
  738. end;
  739. end;
  740. end;
  741. begin
  742. case def.typ of
  743. variantdef :
  744. variantdef_rtti(tvariantdef(def));
  745. stringdef :
  746. stringdef_rtti(tstringdef(def));
  747. enumdef :
  748. enumdef_rtti(tenumdef(def));
  749. orddef :
  750. orddef_rtti(torddef(def));
  751. floatdef :
  752. floatdef_rtti(tfloatdef(def));
  753. setdef :
  754. setdef_rtti(tsetdef(def));
  755. procvardef :
  756. procvardef_rtti(tprocvardef(def));
  757. arraydef :
  758. begin
  759. if ado_IsBitPacked in tarraydef(def).arrayoptions then
  760. unknown_rtti(tstoreddef(def))
  761. else
  762. arraydef_rtti(tarraydef(def));
  763. end;
  764. recorddef :
  765. begin
  766. if trecorddef(def).is_packed then
  767. unknown_rtti(tstoreddef(def))
  768. else
  769. recorddef_rtti(trecorddef(def));
  770. end;
  771. objectdef :
  772. objectdef_rtti(tobjectdef(def));
  773. else
  774. unknown_rtti(tstoreddef(def));
  775. end;
  776. end;
  777. procedure TRTTIWriter.write_rtti_extrasyms(def:Tdef;rt:Trttitype;mainrtti:Tasmsymbol);
  778. procedure enumdef_rtti_ord2stringindex(def:Tenumdef);
  779. var rttilab:Tasmsymbol;
  780. t:Tenumsym;
  781. syms:^Tenumsym;
  782. offsets:^longint;
  783. sym_count,sym_alloc:longint;
  784. h,i,p,o,st:longint;
  785. mode:(lookup,search); {Modify with care, ordinal value of enum is written.}
  786. r:single; {Must be real type because of integer overflow risk.}
  787. begin
  788. {Random access needed, put in array.}
  789. getmem(syms,64*sizeof(Tenumsym));
  790. getmem(offsets,64*sizeof(longint));
  791. sym_count:=0;
  792. sym_alloc:=64;
  793. st:=0;
  794. t:=Tenumsym(def.firstenum);
  795. while assigned(t) do
  796. begin
  797. if sym_count>=sym_alloc then
  798. begin
  799. reallocmem(syms,2*sym_alloc*sizeof(Tenumsym));
  800. reallocmem(offsets,2*sym_alloc*sizeof(longint));
  801. sym_alloc:=sym_alloc*2;
  802. end;
  803. syms[sym_count]:=t;
  804. offsets[sym_count]:=st;
  805. inc(sym_count);
  806. st:=st+length(t.realname)+1;
  807. t:=t.nextenum;
  808. end;
  809. {Sort the syms by enum value}
  810. if sym_count>=2 then
  811. begin
  812. p:=1;
  813. while 2*p<sym_count do
  814. p:=2*p;
  815. while p<>0 do
  816. begin
  817. for h:=p to sym_count-1 do
  818. begin
  819. i:=h;
  820. t:=syms[i];
  821. o:=offsets[i];
  822. repeat
  823. if syms[i-p].value<=t.value then
  824. break;
  825. syms[i]:=syms[i-p];
  826. offsets[i]:=offsets[i-p];
  827. dec(i,p);
  828. until i<p;
  829. syms[i]:=t;
  830. offsets[i]:=o;
  831. end;
  832. p:=p shr 1;
  833. end;
  834. end;
  835. {Decide wether a lookup array is size efficient.}
  836. mode:=lookup;
  837. if sym_count>0 then
  838. begin
  839. i:=1;
  840. r:=0;
  841. h:=syms[0].value; {Next expected enum value is min.}
  842. while i<sym_count do
  843. begin
  844. {Calculate size of hole between values. Avoid integer overflows.}
  845. r:=r+(single(syms[i].value)-single(h))-1;
  846. h:=syms[i].value;
  847. inc(i);
  848. end;
  849. if r>sym_count then
  850. mode:=search; {Don't waste more than 50% space.}
  851. end;
  852. {Calculate start of string table.}
  853. st:=1;
  854. if assigned(def.typesym) then
  855. inc(st,length(def.typesym.realname)+1)
  856. else
  857. inc(st);
  858. if (tf_requires_proper_alignment in target_info.flags) then
  859. st:=align(st,sizeof(Tconstptruint));
  860. inc(st);
  861. if (tf_requires_proper_alignment in target_info.flags) then
  862. st:=align(st,sizeof(Tconstptruint));
  863. inc(st,8+sizeof(pint));
  864. { write rtti data }
  865. with current_asmdata do
  866. begin
  867. rttilab:=defineasmsymbol(Tstoreddef(def).rtti_mangledname(rt)+'_o2s',AB_GLOBAL,AT_DATA);
  868. maybe_new_object_file(asmlists[al_rtti]);
  869. new_section(asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(pint)));
  870. asmlists[al_rtti].concat(Tai_symbol.create_global(rttilab,0));
  871. asmlists[al_rtti].concat(Tai_const.create_32bit(longint(mode)));
  872. if mode=lookup then
  873. begin
  874. if (tf_requires_proper_alignment in target_info.flags) then
  875. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  876. o:=syms[0].value; {Start with min value.}
  877. for i:=0 to sym_count-1 do
  878. begin
  879. while o<syms[i].value do
  880. begin
  881. asmlists[al_rtti].concat(Tai_const.create_pint(0));
  882. inc(o);
  883. end;
  884. inc(o);
  885. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  886. end;
  887. end
  888. else
  889. begin
  890. asmlists[al_rtti].concat(Tai_const.create_32bit(sym_count));
  891. for i:=0 to sym_count-1 do
  892. begin
  893. if (tf_requires_proper_alignment in target_info.flags) then
  894. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(4));
  895. asmlists[al_rtti].concat(Tai_const.create_32bit(syms[i].value));
  896. if (tf_requires_proper_alignment in target_info.flags) then
  897. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  898. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  899. end;
  900. end;
  901. asmlists[al_rtti].concat(Tai_symbol_end.create(rttilab));
  902. end;
  903. freemem(syms);
  904. freemem(offsets);
  905. end;
  906. procedure enumdef_rtti_string2ordindex(def:Tenumdef);
  907. var rttilab:Tasmsymbol;
  908. t:Tenumsym;
  909. syms:^Tenumsym;
  910. offsets:^longint;
  911. sym_count,sym_alloc:longint;
  912. h,i,p,o,st:longint;
  913. begin
  914. {Random access needed, put in array.}
  915. getmem(syms,64*sizeof(Tenumsym));
  916. getmem(offsets,64*sizeof(longint));
  917. sym_count:=0;
  918. sym_alloc:=64;
  919. st:=0;
  920. t:=Tenumsym(def.firstenum);
  921. while assigned(t) do
  922. begin
  923. if sym_count>=sym_alloc then
  924. begin
  925. reallocmem(syms,2*sym_alloc*sizeof(Tenumsym));
  926. reallocmem(offsets,2*sym_alloc*sizeof(longint));
  927. sym_alloc:=sym_alloc*2;
  928. end;
  929. syms[sym_count]:=t;
  930. offsets[sym_count]:=st;
  931. inc(sym_count);
  932. st:=st+length(t.realname)+1;
  933. t:=t.nextenum;
  934. end;
  935. {Sort the syms by enum name}
  936. if sym_count>=2 then
  937. begin
  938. p:=1;
  939. while 2*p<sym_count do
  940. p:=2*p;
  941. while p<>0 do
  942. begin
  943. for h:=p to sym_count-1 do
  944. begin
  945. i:=h;
  946. t:=syms[i];
  947. o:=offsets[i];
  948. repeat
  949. if syms[i-p].name<=t.name then
  950. break;
  951. syms[i]:=syms[i-p];
  952. offsets[i]:=offsets[i-p];
  953. dec(i,p);
  954. until i<p;
  955. syms[i]:=t;
  956. offsets[i]:=o;
  957. end;
  958. p:=p shr 1;
  959. end;
  960. end;
  961. {Calculate start of string table.}
  962. st:=1;
  963. if assigned(def.typesym) then
  964. inc(st,length(def.typesym.realname)+1)
  965. else
  966. inc(st);
  967. if (tf_requires_proper_alignment in target_info.flags) then
  968. st:=align(st,sizeof(Tconstptruint));
  969. inc(st);
  970. if (tf_requires_proper_alignment in target_info.flags) then
  971. st:=align(st,sizeof(Tconstptruint));
  972. inc(st,8+sizeof(pint));
  973. { write rtti data }
  974. with current_asmdata do
  975. begin
  976. rttilab:=defineasmsymbol(Tstoreddef(def).rtti_mangledname(rt)+'_s2o',AB_GLOBAL,AT_DATA);
  977. maybe_new_object_file(asmlists[al_rtti]);
  978. new_section(asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(pint)));
  979. asmlists[al_rtti].concat(Tai_symbol.create_global(rttilab,0));
  980. asmlists[al_rtti].concat(Tai_const.create_32bit(sym_count));
  981. for i:=0 to sym_count-1 do
  982. begin
  983. if (tf_requires_proper_alignment in target_info.flags) then
  984. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(4));
  985. asmlists[al_rtti].concat(Tai_const.create_32bit(syms[i].value));
  986. if (tf_requires_proper_alignment in target_info.flags) then
  987. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  988. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  989. end;
  990. asmlists[al_rtti].concat(Tai_symbol_end.create(rttilab));
  991. end;
  992. freemem(syms);
  993. freemem(offsets);
  994. end;
  995. begin
  996. case def.typ of
  997. enumdef:
  998. if rt=fullrtti then
  999. begin
  1000. enumdef_rtti_ord2stringindex(Tenumdef(def));
  1001. enumdef_rtti_string2ordindex(Tenumdef(def));
  1002. end;
  1003. end;
  1004. end;
  1005. procedure TRTTIWriter.write_child_rtti_data(def:tdef;rt:trttitype);
  1006. begin
  1007. case def.typ of
  1008. enumdef :
  1009. if assigned(tenumdef(def).basedef) then
  1010. write_rtti(tenumdef(def).basedef,rt);
  1011. setdef :
  1012. write_rtti(tsetdef(def).elementdef,rt);
  1013. arraydef :
  1014. write_rtti(tarraydef(def).elementdef,rt);
  1015. recorddef :
  1016. fields_write_rtti(trecorddef(def).symtable,rt);
  1017. objectdef :
  1018. begin
  1019. if assigned(tobjectdef(def).childof) then
  1020. write_rtti(tobjectdef(def).childof,rt);
  1021. if rt=initrtti then
  1022. fields_write_rtti(tobjectdef(def).symtable,rt)
  1023. else
  1024. published_write_rtti(tobjectdef(def).symtable,rt);
  1025. end;
  1026. end;
  1027. end;
  1028. function TRTTIWriter.ref_rtti(def:tdef;rt:trttitype):tasmsymbol;
  1029. begin
  1030. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt));
  1031. end;
  1032. procedure TRTTIWriter.write_rtti(def:tdef;rt:trttitype);
  1033. var
  1034. rttilab : tasmsymbol;
  1035. begin
  1036. { only write rtti of definitions from the current module }
  1037. if not findunitsymtable(def.owner).iscurrentunit then
  1038. exit;
  1039. { prevent recursion }
  1040. if rttidefstate[rt] in def.defstates then
  1041. exit;
  1042. include(def.defstates,rttidefstate[rt]);
  1043. { write first all dependencies }
  1044. write_child_rtti_data(def,rt);
  1045. { write rtti data }
  1046. rttilab:=current_asmdata.DefineAsmSymbol(tstoreddef(def).rtti_mangledname(rt),AB_GLOBAL,AT_DATA);
  1047. maybe_new_object_file(current_asmdata.asmlists[al_rtti]);
  1048. new_section(current_asmdata.asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(pint)));
  1049. current_asmdata.asmlists[al_rtti].concat(Tai_symbol.Create_global(rttilab,0));
  1050. write_rtti_data(def,rt);
  1051. current_asmdata.asmlists[al_rtti].concat(Tai_symbol_end.Create(rttilab));
  1052. write_rtti_extrasyms(def,rt,rttilab);
  1053. end;
  1054. function TRTTIWriter.get_rtti_label(def:tdef;rt:trttitype):tasmsymbol;
  1055. begin
  1056. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt));
  1057. end;
  1058. function TRTTIWriter.get_rtti_label_ord2str(def:tdef;rt:trttitype):tasmsymbol;
  1059. begin
  1060. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt)+'_o2s');
  1061. end;
  1062. function TRTTIWriter.get_rtti_label_str2ord(def:tdef;rt:trttitype):tasmsymbol;
  1063. begin
  1064. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt)+'_s2o');
  1065. end;
  1066. end.