ncgrtti.pas 47 KB

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