ncgrtti.pas 48 KB

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