ncgrtti.pas 50 KB

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