ncgrtti.pas 53 KB

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