ncgrtti.pas 48 KB

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