ncgrtti.pas 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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 (sym.visibility=vis_published) 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. (sym.visibility=vis_published) 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. (sym.visibility=vis_published) 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. (sym.visibility=vis_published) then
  279. begin
  280. if ppo_indexed in tpropertysym(sym).propoptions then
  281. proctypesinfo:=$40
  282. else
  283. proctypesinfo:=0;
  284. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(tpropertysym(sym).propdef,fullrtti)));
  285. writeaccessproc(palt_read,0,0);
  286. writeaccessproc(palt_write,2,0);
  287. { is it stored ? }
  288. if not(ppo_stored in tpropertysym(sym).propoptions) then
  289. begin
  290. { no, so put a constant zero }
  291. current_asmdata.asmlists[al_rtti].concat(Tai_const.create(aitconst_ptr,0));
  292. proctypesinfo:=proctypesinfo or (3 shl 4);
  293. end
  294. else
  295. writeaccessproc(palt_stored,4,1); { maybe; if no procedure put a constant 1 (=true) }
  296. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tpropertysym(sym).index));
  297. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tpropertysym(sym).default));
  298. propnameitem:=TPropNameListItem(propnamelist.Find(tpropertysym(sym).name));
  299. if not assigned(propnameitem) then
  300. internalerror(200512201);
  301. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(propnameitem.propindex));
  302. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(proctypesinfo));
  303. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(tpropertysym(sym).realname)));
  304. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(tpropertysym(sym).realname));
  305. if (tf_requires_proper_alignment in target_info.flags) then
  306. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  307. end;
  308. end;
  309. end;
  310. procedure TRTTIWriter.write_rtti_data(def:tdef;rt:trttitype);
  311. procedure unknown_rtti(def:tstoreddef);
  312. begin
  313. current_asmdata.asmlists[al_rtti].concat(tai_const.create_8bit(tkUnknown));
  314. write_rtti_name(def);
  315. end;
  316. procedure variantdef_rtti(def:tvariantdef);
  317. begin
  318. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkVariant));
  319. end;
  320. procedure stringdef_rtti(def:tstringdef);
  321. begin
  322. case def.stringtype of
  323. st_ansistring:
  324. begin
  325. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkAString));
  326. write_rtti_name(def);
  327. end;
  328. st_widestring:
  329. begin
  330. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkWString));
  331. write_rtti_name(def);
  332. end;
  333. st_unicodestring:
  334. begin
  335. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkUString));
  336. write_rtti_name(def);
  337. end;
  338. st_longstring:
  339. begin
  340. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkLString));
  341. write_rtti_name(def);
  342. end;
  343. st_shortstring:
  344. begin
  345. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkSString));
  346. write_rtti_name(def);
  347. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.len));
  348. if (tf_requires_proper_alignment in target_info.flags) then
  349. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  350. end;
  351. end;
  352. end;
  353. procedure enumdef_rtti(def:tenumdef);
  354. var
  355. hp : tenumsym;
  356. begin
  357. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkEnumeration));
  358. write_rtti_name(def);
  359. if (tf_requires_proper_alignment in target_info.flags) then
  360. current_asmdata.asmlists[al_rtti].concat(Cai_align.Create(sizeof(TConstPtrUInt)));
  361. case longint(def.size) of
  362. 1 :
  363. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUByte));
  364. 2 :
  365. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUWord));
  366. 4 :
  367. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otULong));
  368. end;
  369. if (tf_requires_proper_alignment in target_info.flags) then
  370. current_asmdata.asmlists[al_rtti].concat(Cai_align.Create(longint(def.size)));
  371. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.min));
  372. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.max));
  373. if (tf_requires_proper_alignment in target_info.flags) then
  374. current_asmdata.asmlists[al_rtti].concat(Cai_align.Create(sizeof(TConstPtrUint)));
  375. if assigned(def.basedef) then
  376. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.basedef,rt)))
  377. else
  378. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  379. hp:=tenumsym(def.firstenum);
  380. while assigned(hp) do
  381. begin
  382. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(hp.realname)));
  383. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(hp.realname));
  384. hp:=hp.nextenum;
  385. end;
  386. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(0));
  387. end;
  388. procedure orddef_rtti(def:torddef);
  389. procedure dointeger;
  390. const
  391. trans : array[tordtype] of byte =
  392. (otUByte{otNone},
  393. otUByte,otUWord,otULong,otUByte{otNone},
  394. otSByte,otSWord,otSLong,otUByte{otNone},
  395. otUByte,otSByte,otSWord,otSLong,otSByte,
  396. otUByte,otUWord,otUByte);
  397. begin
  398. write_rtti_name(def);
  399. if (tf_requires_proper_alignment in target_info.flags) then
  400. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  401. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(byte(trans[def.ordtype])));
  402. if (tf_requires_proper_alignment in target_info.flags) then
  403. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  404. {Convert to longint to smuggle values in high(longint)+1..high(cardinal) into asmlist.}
  405. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(def.low.svalue)));
  406. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(def.high.svalue)));
  407. end;
  408. begin
  409. case def.ordtype of
  410. s64bit :
  411. begin
  412. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkInt64));
  413. write_rtti_name(def);
  414. if (tf_requires_proper_alignment in target_info.flags) then
  415. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  416. { low }
  417. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(def.low.svalue));
  418. { high }
  419. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(def.high.svalue));
  420. end;
  421. u64bit :
  422. begin
  423. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkQWord));
  424. write_rtti_name(def);
  425. if (tf_requires_proper_alignment in target_info.flags) then
  426. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  427. {use svalue because Create_64bit accepts int64, prevents range checks}
  428. { low }
  429. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(def.low.svalue));
  430. { high }
  431. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(def.high.svalue));
  432. end;
  433. pasbool:
  434. begin
  435. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkBool));
  436. dointeger;
  437. end;
  438. uchar:
  439. begin
  440. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkChar));
  441. dointeger;
  442. end;
  443. uwidechar:
  444. begin
  445. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkWChar));
  446. dointeger;
  447. end;
  448. scurrency:
  449. begin
  450. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkFloat));
  451. write_rtti_name(def);
  452. if (tf_requires_proper_alignment in target_info.flags) then
  453. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  454. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(ftCurr));
  455. end;
  456. else
  457. begin
  458. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkInteger));
  459. dointeger;
  460. end;
  461. end;
  462. end;
  463. procedure floatdef_rtti(def:tfloatdef);
  464. const
  465. {tfloattype = (s32real,s64real,s80real,s64bit,s128bit);}
  466. translate : array[tfloattype] of byte =
  467. (ftSingle,ftDouble,ftExtended,ftComp,ftCurr,ftFloat128);
  468. begin
  469. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkFloat));
  470. write_rtti_name(def);
  471. if (tf_requires_proper_alignment in target_info.flags) then
  472. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  473. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(translate[def.floattype]));
  474. end;
  475. procedure setdef_rtti(def:tsetdef);
  476. begin
  477. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkSet));
  478. write_rtti_name(def);
  479. if (tf_requires_proper_alignment in target_info.flags) then
  480. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  481. case def.size of
  482. 1:
  483. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUByte));
  484. 2:
  485. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUWord));
  486. 4:
  487. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otULong));
  488. else
  489. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUByte));
  490. end;
  491. if (tf_requires_proper_alignment in target_info.flags) then
  492. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  493. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.elementdef,rt)));
  494. end;
  495. procedure arraydef_rtti(def:tarraydef);
  496. begin
  497. if ado_IsDynamicArray in def.arrayoptions then
  498. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkdynarray))
  499. else
  500. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkarray));
  501. write_rtti_name(def);
  502. if (tf_requires_proper_alignment in target_info.flags) then
  503. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  504. { size of elements }
  505. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_pint(def.elesize));
  506. if not(ado_IsDynamicArray in def.arrayoptions) then
  507. begin
  508. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_pint(pint(def.elecount)));
  509. { element type }
  510. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.elementdef,rt)));
  511. end
  512. else
  513. { write a delphi almost compatible dyn. array entry:
  514. there are two types, eltype and eltype2, the latter is nil if the element type needs
  515. no finalization, the former is always valid, delphi has this swapped, but for
  516. compatibility with older fpc versions we do it different, to be delphi compatible,
  517. the names are swapped in typinfo.pp
  518. }
  519. begin
  520. { element type }
  521. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.elementdef,rt)));
  522. end;
  523. { variant type }
  524. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tstoreddef(def.elementdef).getvardef));
  525. if ado_IsDynamicArray in def.arrayoptions then
  526. begin
  527. { element type }
  528. if def.elementdef.needs_inittable then
  529. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.elementdef,rt)))
  530. else
  531. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_pint(0));
  532. { dummy DynUnitName }
  533. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(0));
  534. end;
  535. end;
  536. procedure recorddef_rtti(def:trecorddef);
  537. var
  538. fieldcnt : longint;
  539. begin
  540. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkrecord));
  541. write_rtti_name(def);
  542. if (tf_requires_proper_alignment in target_info.flags) then
  543. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  544. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  545. fieldcnt:=fields_count(def.symtable,rt);
  546. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(fieldcnt));
  547. fields_write_rtti_data(def.symtable,rt);
  548. end;
  549. procedure procvardef_rtti(def:tprocvardef);
  550. procedure write_para(parasym:tparavarsym);
  551. var
  552. paraspec : byte;
  553. begin
  554. { only store user visible parameters }
  555. if not(vo_is_hidden_para in parasym.varoptions) then
  556. begin
  557. case parasym.varspez of
  558. vs_value: paraspec := 0;
  559. vs_const: paraspec := pfConst;
  560. vs_var : paraspec := pfVar;
  561. vs_out : paraspec := pfOut;
  562. end;
  563. { write flags for current parameter }
  564. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(paraspec));
  565. { write name of current parameter }
  566. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(parasym.realname)));
  567. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(parasym.realname));
  568. { write name of type of current parameter }
  569. write_rtti_name(parasym.vardef);
  570. end;
  571. end;
  572. var
  573. methodkind : byte;
  574. i : integer;
  575. begin
  576. if po_methodpointer in def.procoptions then
  577. begin
  578. { write method id and name }
  579. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkmethod));
  580. write_rtti_name(def);
  581. if (tf_requires_proper_alignment in target_info.flags) then
  582. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  583. { write kind of method (can only be function or procedure)}
  584. if def.returndef = voidtype then
  585. methodkind := mkProcedure
  586. else
  587. methodkind := mkFunction;
  588. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(methodkind));
  589. { write parameter info. The parameters must be written in reverse order
  590. if this method uses right to left parameter pushing! }
  591. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.maxparacount));
  592. if def.proccalloption in pushleftright_pocalls then
  593. begin
  594. for i:=0 to def.paras.count-1 do
  595. write_para(tparavarsym(def.paras[i]));
  596. end
  597. else
  598. begin
  599. for i:=def.paras.count-1 downto 0 do
  600. write_para(tparavarsym(def.paras[i]));
  601. end;
  602. { write name of result type }
  603. write_rtti_name(def.returndef);
  604. end
  605. else
  606. begin
  607. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkprocvar));
  608. write_rtti_name(def);
  609. end;
  610. end;
  611. procedure objectdef_rtti(def:tobjectdef);
  612. procedure objectdef_rtti_class_init(def:tobjectdef);
  613. begin
  614. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  615. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(fields_count(def.symtable,rt)));
  616. fields_write_rtti_data(def.symtable,rt);
  617. end;
  618. procedure objectdef_rtti_interface_init(def:tobjectdef);
  619. begin
  620. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  621. end;
  622. procedure objectdef_rtti_class_full(def:tobjectdef);
  623. var
  624. propnamelist : TFPHashObjectList;
  625. begin
  626. { Collect unique property names with nameindex }
  627. propnamelist:=TFPHashObjectList.Create;
  628. collect_propnamelist(propnamelist,def);
  629. if (oo_has_vmt in def.objectoptions) then
  630. current_asmdata.asmlists[al_rtti].concat(Tai_const.Createname(def.vmt_mangledname,0))
  631. else
  632. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  633. { write parent typeinfo }
  634. if assigned(def.childof) and
  635. (oo_can_have_published in def.childof.objectoptions) then
  636. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.childof,fullrtti)))
  637. else
  638. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  639. { total number of unique properties }
  640. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(propnamelist.count));
  641. { write unit name }
  642. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(current_module.realmodulename^)));
  643. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(current_module.realmodulename^));
  644. if (tf_requires_proper_alignment in target_info.flags) then
  645. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  646. { write published properties for this object }
  647. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(published_properties_count(def.symtable)));
  648. if (tf_requires_proper_alignment in target_info.flags) then
  649. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  650. published_properties_write_rtti_data(propnamelist,def.symtable);
  651. propnamelist.free;
  652. end;
  653. procedure objectdef_rtti_interface_full(def:tobjectdef);
  654. var
  655. i : longint;
  656. propnamelist : TFPHashObjectList;
  657. begin
  658. { Collect unique property names with nameindex }
  659. propnamelist:=TFPHashObjectList.Create;
  660. collect_propnamelist(propnamelist,def);
  661. { write parent typeinfo }
  662. if assigned(def.childof) then
  663. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.childof,fullrtti)))
  664. else
  665. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  666. { interface: write flags, iid and iidstr }
  667. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(
  668. { ugly, but working }
  669. {$ifdef USE_PACKSET1}
  670. byte([
  671. {$else USE_PACKSET1}
  672. longint([
  673. {$endif USE_PACKSET1}
  674. TCompilerIntfFlag(ord(ifHasGuid)*ord(assigned(def.iidguid))),
  675. TCompilerIntfFlag(ord(ifHasStrGUID)*ord(assigned(def.iidstr))),
  676. TCompilerIntfFlag(ord(ifDispInterface)*ord(def.objecttype=odt_dispinterface))
  677. ])
  678. {
  679. ifDispatch, }
  680. ));
  681. if (tf_requires_proper_alignment in target_info.flags) then
  682. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  683. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(def.iidguid^.D1)));
  684. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(def.iidguid^.D2));
  685. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(def.iidguid^.D3));
  686. for i:=Low(def.iidguid^.D4) to High(def.iidguid^.D4) do
  687. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.iidguid^.D4[i]));
  688. { write unit name }
  689. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(current_module.realmodulename^)));
  690. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(current_module.realmodulename^));
  691. if (tf_requires_proper_alignment in target_info.flags) then
  692. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  693. { write iidstr }
  694. if assigned(def.iidstr) then
  695. begin
  696. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(def.iidstr^)));
  697. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(def.iidstr^));
  698. end
  699. else
  700. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(0));
  701. if (tf_requires_proper_alignment in target_info.flags) then
  702. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  703. { write published properties for this object }
  704. published_properties_write_rtti_data(propnamelist,def.symtable);
  705. propnamelist.free;
  706. end;
  707. begin
  708. case def.objecttype of
  709. odt_class:
  710. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkclass));
  711. odt_object:
  712. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkobject));
  713. odt_dispinterface,
  714. odt_interfacecom:
  715. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkinterface));
  716. odt_interfacecorba:
  717. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkinterfaceCorba));
  718. else
  719. internalerror(200611034);
  720. end;
  721. { generate the name }
  722. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(def.objrealname^)));
  723. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(def.objrealname^));
  724. if (tf_requires_proper_alignment in target_info.flags) then
  725. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  726. case rt of
  727. initrtti :
  728. begin
  729. if def.objecttype in [odt_class,odt_object] then
  730. objectdef_rtti_class_init(def)
  731. else
  732. objectdef_rtti_interface_init(def);
  733. end;
  734. fullrtti :
  735. begin
  736. if def.objecttype in [odt_class,odt_object] then
  737. objectdef_rtti_class_full(def)
  738. else
  739. objectdef_rtti_interface_full(def);
  740. end;
  741. end;
  742. end;
  743. begin
  744. case def.typ of
  745. variantdef :
  746. variantdef_rtti(tvariantdef(def));
  747. stringdef :
  748. stringdef_rtti(tstringdef(def));
  749. enumdef :
  750. enumdef_rtti(tenumdef(def));
  751. orddef :
  752. orddef_rtti(torddef(def));
  753. floatdef :
  754. floatdef_rtti(tfloatdef(def));
  755. setdef :
  756. setdef_rtti(tsetdef(def));
  757. procvardef :
  758. procvardef_rtti(tprocvardef(def));
  759. arraydef :
  760. begin
  761. if ado_IsBitPacked in tarraydef(def).arrayoptions then
  762. unknown_rtti(tstoreddef(def))
  763. else
  764. arraydef_rtti(tarraydef(def));
  765. end;
  766. recorddef :
  767. begin
  768. if trecorddef(def).is_packed then
  769. unknown_rtti(tstoreddef(def))
  770. else
  771. recorddef_rtti(trecorddef(def));
  772. end;
  773. objectdef :
  774. objectdef_rtti(tobjectdef(def));
  775. else
  776. unknown_rtti(tstoreddef(def));
  777. end;
  778. end;
  779. procedure TRTTIWriter.write_rtti_extrasyms(def:Tdef;rt:Trttitype;mainrtti:Tasmsymbol);
  780. procedure enumdef_rtti_ord2stringindex(def:Tenumdef);
  781. var rttilab:Tasmsymbol;
  782. t:Tenumsym;
  783. syms:^Tenumsym;
  784. offsets:^longint;
  785. sym_count,sym_alloc:longint;
  786. h,i,p,o,st:longint;
  787. mode:(lookup,search); {Modify with care, ordinal value of enum is written.}
  788. r:single; {Must be real type because of integer overflow risk.}
  789. begin
  790. {Random access needed, put in array.}
  791. getmem(syms,64*sizeof(Tenumsym));
  792. getmem(offsets,64*sizeof(longint));
  793. sym_count:=0;
  794. sym_alloc:=64;
  795. st:=0;
  796. t:=Tenumsym(def.firstenum);
  797. while assigned(t) do
  798. begin
  799. if sym_count>=sym_alloc then
  800. begin
  801. reallocmem(syms,2*sym_alloc*sizeof(Tenumsym));
  802. reallocmem(offsets,2*sym_alloc*sizeof(longint));
  803. sym_alloc:=sym_alloc*2;
  804. end;
  805. syms[sym_count]:=t;
  806. offsets[sym_count]:=st;
  807. inc(sym_count);
  808. st:=st+length(t.realname)+1;
  809. t:=t.nextenum;
  810. end;
  811. {Sort the syms by enum value}
  812. if sym_count>=2 then
  813. begin
  814. p:=1;
  815. while 2*p<sym_count do
  816. p:=2*p;
  817. while p<>0 do
  818. begin
  819. for h:=p to sym_count-1 do
  820. begin
  821. i:=h;
  822. t:=syms[i];
  823. o:=offsets[i];
  824. repeat
  825. if syms[i-p].value<=t.value then
  826. break;
  827. syms[i]:=syms[i-p];
  828. offsets[i]:=offsets[i-p];
  829. dec(i,p);
  830. until i<p;
  831. syms[i]:=t;
  832. offsets[i]:=o;
  833. end;
  834. p:=p shr 1;
  835. end;
  836. end;
  837. {Decide wether a lookup array is size efficient.}
  838. mode:=lookup;
  839. if sym_count>0 then
  840. begin
  841. i:=1;
  842. r:=0;
  843. h:=syms[0].value; {Next expected enum value is min.}
  844. while i<sym_count do
  845. begin
  846. {Calculate size of hole between values. Avoid integer overflows.}
  847. r:=r+(single(syms[i].value)-single(h))-1;
  848. h:=syms[i].value;
  849. inc(i);
  850. end;
  851. if r>sym_count then
  852. mode:=search; {Don't waste more than 50% space.}
  853. end;
  854. {Calculate start of string table.}
  855. st:=1;
  856. if assigned(def.typesym) then
  857. inc(st,length(def.typesym.realname)+1)
  858. else
  859. inc(st);
  860. if (tf_requires_proper_alignment in target_info.flags) then
  861. st:=align(st,sizeof(Tconstptruint));
  862. inc(st);
  863. if (tf_requires_proper_alignment in target_info.flags) then
  864. st:=align(st,sizeof(Tconstptruint));
  865. inc(st,8+sizeof(pint));
  866. { write rtti data }
  867. with current_asmdata do
  868. begin
  869. rttilab:=defineasmsymbol(Tstoreddef(def).rtti_mangledname(rt)+'_o2s',AB_GLOBAL,AT_DATA);
  870. maybe_new_object_file(asmlists[al_rtti]);
  871. new_section(asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(pint)));
  872. asmlists[al_rtti].concat(Tai_symbol.create_global(rttilab,0));
  873. asmlists[al_rtti].concat(Tai_const.create_32bit(longint(mode)));
  874. if mode=lookup then
  875. begin
  876. if (tf_requires_proper_alignment in target_info.flags) then
  877. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  878. o:=syms[0].value; {Start with min value.}
  879. for i:=0 to sym_count-1 do
  880. begin
  881. while o<syms[i].value do
  882. begin
  883. asmlists[al_rtti].concat(Tai_const.create_pint(0));
  884. inc(o);
  885. end;
  886. inc(o);
  887. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  888. end;
  889. end
  890. else
  891. begin
  892. asmlists[al_rtti].concat(Tai_const.create_32bit(sym_count));
  893. for i:=0 to sym_count-1 do
  894. begin
  895. if (tf_requires_proper_alignment in target_info.flags) then
  896. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(4));
  897. asmlists[al_rtti].concat(Tai_const.create_32bit(syms[i].value));
  898. if (tf_requires_proper_alignment in target_info.flags) then
  899. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  900. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  901. end;
  902. end;
  903. asmlists[al_rtti].concat(Tai_symbol_end.create(rttilab));
  904. end;
  905. freemem(syms);
  906. freemem(offsets);
  907. end;
  908. procedure enumdef_rtti_string2ordindex(def:Tenumdef);
  909. var rttilab:Tasmsymbol;
  910. t:Tenumsym;
  911. syms:^Tenumsym;
  912. offsets:^longint;
  913. sym_count,sym_alloc:longint;
  914. h,i,p,o,st:longint;
  915. begin
  916. {Random access needed, put in array.}
  917. getmem(syms,64*sizeof(Tenumsym));
  918. getmem(offsets,64*sizeof(longint));
  919. sym_count:=0;
  920. sym_alloc:=64;
  921. st:=0;
  922. t:=Tenumsym(def.firstenum);
  923. while assigned(t) do
  924. begin
  925. if sym_count>=sym_alloc then
  926. begin
  927. reallocmem(syms,2*sym_alloc*sizeof(Tenumsym));
  928. reallocmem(offsets,2*sym_alloc*sizeof(longint));
  929. sym_alloc:=sym_alloc*2;
  930. end;
  931. syms[sym_count]:=t;
  932. offsets[sym_count]:=st;
  933. inc(sym_count);
  934. st:=st+length(t.realname)+1;
  935. t:=t.nextenum;
  936. end;
  937. {Sort the syms by enum name}
  938. if sym_count>=2 then
  939. begin
  940. p:=1;
  941. while 2*p<sym_count do
  942. p:=2*p;
  943. while p<>0 do
  944. begin
  945. for h:=p to sym_count-1 do
  946. begin
  947. i:=h;
  948. t:=syms[i];
  949. o:=offsets[i];
  950. repeat
  951. if syms[i-p].name<=t.name then
  952. break;
  953. syms[i]:=syms[i-p];
  954. offsets[i]:=offsets[i-p];
  955. dec(i,p);
  956. until i<p;
  957. syms[i]:=t;
  958. offsets[i]:=o;
  959. end;
  960. p:=p shr 1;
  961. end;
  962. end;
  963. {Calculate start of string table.}
  964. st:=1;
  965. if assigned(def.typesym) then
  966. inc(st,length(def.typesym.realname)+1)
  967. else
  968. inc(st);
  969. if (tf_requires_proper_alignment in target_info.flags) then
  970. st:=align(st,sizeof(Tconstptruint));
  971. inc(st);
  972. if (tf_requires_proper_alignment in target_info.flags) then
  973. st:=align(st,sizeof(Tconstptruint));
  974. inc(st,8+sizeof(pint));
  975. { write rtti data }
  976. with current_asmdata do
  977. begin
  978. rttilab:=defineasmsymbol(Tstoreddef(def).rtti_mangledname(rt)+'_s2o',AB_GLOBAL,AT_DATA);
  979. maybe_new_object_file(asmlists[al_rtti]);
  980. new_section(asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(pint)));
  981. asmlists[al_rtti].concat(Tai_symbol.create_global(rttilab,0));
  982. asmlists[al_rtti].concat(Tai_const.create_32bit(sym_count));
  983. for i:=0 to sym_count-1 do
  984. begin
  985. if (tf_requires_proper_alignment in target_info.flags) then
  986. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(4));
  987. asmlists[al_rtti].concat(Tai_const.create_32bit(syms[i].value));
  988. if (tf_requires_proper_alignment in target_info.flags) then
  989. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  990. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  991. end;
  992. asmlists[al_rtti].concat(Tai_symbol_end.create(rttilab));
  993. end;
  994. freemem(syms);
  995. freemem(offsets);
  996. end;
  997. begin
  998. case def.typ of
  999. enumdef:
  1000. if rt=fullrtti then
  1001. begin
  1002. enumdef_rtti_ord2stringindex(Tenumdef(def));
  1003. enumdef_rtti_string2ordindex(Tenumdef(def));
  1004. end;
  1005. end;
  1006. end;
  1007. procedure TRTTIWriter.write_child_rtti_data(def:tdef;rt:trttitype);
  1008. begin
  1009. case def.typ of
  1010. enumdef :
  1011. if assigned(tenumdef(def).basedef) then
  1012. write_rtti(tenumdef(def).basedef,rt);
  1013. setdef :
  1014. write_rtti(tsetdef(def).elementdef,rt);
  1015. arraydef :
  1016. write_rtti(tarraydef(def).elementdef,rt);
  1017. recorddef :
  1018. fields_write_rtti(trecorddef(def).symtable,rt);
  1019. objectdef :
  1020. begin
  1021. if assigned(tobjectdef(def).childof) then
  1022. write_rtti(tobjectdef(def).childof,rt);
  1023. if rt=initrtti then
  1024. fields_write_rtti(tobjectdef(def).symtable,rt)
  1025. else
  1026. published_write_rtti(tobjectdef(def).symtable,rt);
  1027. end;
  1028. end;
  1029. end;
  1030. function TRTTIWriter.ref_rtti(def:tdef;rt:trttitype):tasmsymbol;
  1031. begin
  1032. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt));
  1033. end;
  1034. procedure TRTTIWriter.write_rtti(def:tdef;rt:trttitype);
  1035. var
  1036. rttilab : tasmsymbol;
  1037. begin
  1038. { only write rtti of definitions from the current module }
  1039. if not findunitsymtable(def.owner).iscurrentunit then
  1040. exit;
  1041. { prevent recursion }
  1042. if rttidefstate[rt] in def.defstates then
  1043. exit;
  1044. include(def.defstates,rttidefstate[rt]);
  1045. { write first all dependencies }
  1046. write_child_rtti_data(def,rt);
  1047. { write rtti data }
  1048. rttilab:=current_asmdata.DefineAsmSymbol(tstoreddef(def).rtti_mangledname(rt),AB_GLOBAL,AT_DATA);
  1049. maybe_new_object_file(current_asmdata.asmlists[al_rtti]);
  1050. new_section(current_asmdata.asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(pint)));
  1051. current_asmdata.asmlists[al_rtti].concat(Tai_symbol.Create_global(rttilab,0));
  1052. write_rtti_data(def,rt);
  1053. current_asmdata.asmlists[al_rtti].concat(Tai_symbol_end.Create(rttilab));
  1054. write_rtti_extrasyms(def,rt,rttilab);
  1055. end;
  1056. function TRTTIWriter.get_rtti_label(def:tdef;rt:trttitype):tasmsymbol;
  1057. begin
  1058. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt));
  1059. end;
  1060. function TRTTIWriter.get_rtti_label_ord2str(def:tdef;rt:trttitype):tasmsymbol;
  1061. begin
  1062. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt)+'_o2s');
  1063. end;
  1064. function TRTTIWriter.get_rtti_label_str2ord(def:tdef;rt:trttitype):tasmsymbol;
  1065. begin
  1066. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt)+'_s2o');
  1067. end;
  1068. end.