ncgrtti.pas 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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. { write zero which is required by RTL }
  434. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(0));
  435. end;
  436. procedure orddef_rtti(def:torddef);
  437. procedure dointeger(typekind: byte);
  438. const
  439. trans : array[tordtype] of byte =
  440. (otUByte{otNone},
  441. otUByte,otUWord,otULong,otUByte{otNone},
  442. otSByte,otSWord,otSLong,otUByte{otNone},
  443. otUByte,otSByte,otSWord,otSLong,otSByte,
  444. otUByte,otUWord,otUByte);
  445. begin
  446. write_header(def,typekind);
  447. maybe_write_align;
  448. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(byte(trans[def.ordtype])));
  449. maybe_write_align;
  450. {Convert to longint to smuggle values in high(longint)+1..high(cardinal) into asmlist.}
  451. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(def.low.svalue)));
  452. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(def.high.svalue)));
  453. end;
  454. begin
  455. case def.ordtype of
  456. s64bit :
  457. begin
  458. write_header(def,tkInt64);
  459. maybe_write_align;
  460. { low }
  461. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(def.low.svalue));
  462. { high }
  463. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(def.high.svalue));
  464. end;
  465. u64bit :
  466. begin
  467. write_header(def,tkQWord);
  468. maybe_write_align;
  469. {use svalue because Create_64bit accepts int64, prevents range checks}
  470. { low }
  471. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(def.low.svalue));
  472. { high }
  473. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(def.high.svalue));
  474. end;
  475. pasbool:
  476. dointeger(tkBool);
  477. uchar:
  478. dointeger(tkChar);
  479. uwidechar:
  480. dointeger(tkWChar);
  481. scurrency:
  482. begin
  483. write_header(def,tkFloat);
  484. maybe_write_align;
  485. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(ftCurr));
  486. end;
  487. else
  488. dointeger(tkInteger);
  489. end;
  490. end;
  491. procedure floatdef_rtti(def:tfloatdef);
  492. const
  493. {tfloattype = (s32real,s64real,s80real,sc80real,s64bit,s128bit);}
  494. translate : array[tfloattype] of byte =
  495. (ftSingle,ftDouble,ftExtended,ftExtended,ftComp,ftCurr,ftFloat128);
  496. begin
  497. write_header(def,tkFloat);
  498. maybe_write_align;
  499. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(translate[def.floattype]));
  500. end;
  501. procedure setdef_rtti(def:tsetdef);
  502. begin
  503. write_header(def,tkSet);
  504. maybe_write_align;
  505. case def.size of
  506. 1:
  507. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUByte));
  508. 2:
  509. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUWord));
  510. 4:
  511. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otULong));
  512. else
  513. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUByte));
  514. end;
  515. maybe_write_align;
  516. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.elementdef,rt)));
  517. end;
  518. procedure arraydef_rtti(def:tarraydef);
  519. begin
  520. if ado_IsDynamicArray in def.arrayoptions then
  521. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkdynarray))
  522. else
  523. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkarray));
  524. write_rtti_name(def);
  525. maybe_write_align;
  526. { size of elements }
  527. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_pint(def.elesize));
  528. if not(ado_IsDynamicArray in def.arrayoptions) then
  529. begin
  530. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_pint(pint(def.elecount)));
  531. { element type }
  532. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.elementdef,rt)));
  533. end
  534. else
  535. { write a delphi almost compatible dyn. array entry:
  536. there are two types, eltype and eltype2, the latter is nil if the element type needs
  537. no finalization, the former is always valid, delphi has this swapped, but for
  538. compatibility with older fpc versions we do it different, to be delphi compatible,
  539. the names are swapped in typinfo.pp
  540. }
  541. begin
  542. { element type }
  543. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.elementdef,rt)));
  544. end;
  545. { variant type }
  546. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tstoreddef(def.elementdef).getvardef));
  547. if ado_IsDynamicArray in def.arrayoptions then
  548. begin
  549. { element type }
  550. if def.elementdef.needs_inittable then
  551. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.elementdef,rt)))
  552. else
  553. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_pint(0));
  554. { write unit name }
  555. write_string(current_module.realmodulename^);
  556. end;
  557. end;
  558. procedure recorddef_rtti(def:trecorddef);
  559. begin
  560. write_header(def,tkRecord);
  561. maybe_write_align;
  562. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  563. fields_write_rtti_data(def,rt);
  564. end;
  565. procedure procvardef_rtti(def:tprocvardef);
  566. const
  567. ProcCallOptionToCallConv: array[tproccalloption] of byte = (
  568. { pocall_none } 0,
  569. { pocall_cdecl } 1,
  570. { pocall_cppdecl } 5,
  571. { pocall_far16 } 6,
  572. { pocall_oldfpccall } 7,
  573. { pocall_internproc } 8,
  574. { pocall_syscall } 9,
  575. { pocall_pascal } 2,
  576. { pocall_register } 0,
  577. { pocall_safecall } 4,
  578. { pocall_stdcall } 3,
  579. { pocall_softfloat } 10,
  580. { pocall_mwpascal } 11,
  581. { pocall_interrupt } 12
  582. );
  583. procedure write_para(parasym:tparavarsym);
  584. var
  585. paraspec : byte;
  586. begin
  587. { only store user visible parameters }
  588. if not(vo_is_hidden_para in parasym.varoptions) then
  589. begin
  590. case parasym.varspez of
  591. vs_value : paraspec := 0;
  592. vs_const : paraspec := pfConst;
  593. vs_var : paraspec := pfVar;
  594. vs_out : paraspec := pfOut;
  595. vs_constref: paraspec := pfConstRef;
  596. end;
  597. { Kylix also seems to always add both pfArray and pfReference
  598. in this case
  599. }
  600. if is_open_array(parasym.vardef) then
  601. paraspec:=paraspec or pfArray or pfReference;
  602. { and these for classes and interfaces (maybe because they
  603. are themselves addresses?)
  604. }
  605. if is_class_or_interface(parasym.vardef) then
  606. paraspec:=paraspec or pfAddress;
  607. { set bits run from the highest to the lowest bit on
  608. big endian systems
  609. }
  610. if (target_info.endian = endian_big) then
  611. paraspec:=reverse_byte(paraspec);
  612. { write flags for current parameter }
  613. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(paraspec));
  614. { write name of current parameter }
  615. write_string(parasym.realname);
  616. { write name of type of current parameter }
  617. write_rtti_name(parasym.vardef);
  618. end;
  619. end;
  620. var
  621. methodkind : byte;
  622. i : integer;
  623. begin
  624. if po_methodpointer in def.procoptions then
  625. begin
  626. { write method id and name }
  627. write_header(def,tkMethod);
  628. maybe_write_align;
  629. { write kind of method }
  630. case def.proctypeoption of
  631. potype_constructor: methodkind:=mkConstructor;
  632. potype_destructor: methodkind:=mkDestructor;
  633. potype_class_constructor: methodkind:=mkClassConstructor;
  634. potype_class_destructor: methodkind:=mkClassDestructor;
  635. potype_operator: methodkind:=mkOperatorOverload;
  636. potype_procedure:
  637. if po_classmethod in def.procoptions then
  638. methodkind:=mkClassProcedure
  639. else
  640. methodkind:=mkProcedure;
  641. potype_function:
  642. if po_classmethod in def.procoptions then
  643. methodkind:=mkClassFunction
  644. else
  645. methodkind:=mkFunction;
  646. else
  647. begin
  648. if def.returndef = voidtype then
  649. methodkind:=mkProcedure
  650. else
  651. methodkind:=mkFunction;
  652. end;
  653. end;
  654. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(methodkind));
  655. { write parameter info. The parameters must be written in reverse order
  656. if this method uses right to left parameter pushing! }
  657. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.maxparacount));
  658. for i:=0 to def.paras.count-1 do
  659. write_para(tparavarsym(def.paras[i]));
  660. if (methodkind=mkFunction) or (methodkind=mkClassFunction) then
  661. begin
  662. { write name of result type }
  663. write_rtti_name(def.returndef);
  664. maybe_write_align;
  665. { write result typeinfo }
  666. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.returndef,fullrtti)))
  667. end;
  668. { write calling convention }
  669. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(ProcCallOptionToCallConv[def.proccalloption]));
  670. maybe_write_align;
  671. { write params typeinfo }
  672. for i:=0 to def.paras.count-1 do
  673. if not(vo_is_hidden_para in tparavarsym(def.paras[i]).varoptions) then
  674. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(tparavarsym(def.paras[i]).vardef,fullrtti)));
  675. end
  676. else
  677. write_header(def,tkProcvar);
  678. end;
  679. procedure objectdef_rtti(def:tobjectdef);
  680. procedure objectdef_rtti_fields(def:tobjectdef);
  681. begin
  682. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  683. fields_write_rtti_data(def,rt);
  684. end;
  685. procedure objectdef_rtti_interface_init(def:tobjectdef);
  686. begin
  687. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  688. end;
  689. procedure objectdef_rtti_class_full(def:tobjectdef);
  690. var
  691. propnamelist : TFPHashObjectList;
  692. begin
  693. { Collect unique property names with nameindex }
  694. propnamelist:=TFPHashObjectList.Create;
  695. collect_propnamelist(propnamelist,def);
  696. if not is_objectpascal_helper(def) then
  697. if (oo_has_vmt in def.objectoptions) then
  698. current_asmdata.asmlists[al_rtti].concat(Tai_const.Createname(def.vmt_mangledname,0))
  699. else
  700. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  701. { write parent typeinfo }
  702. if assigned(def.childof) then
  703. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.childof,fullrtti)))
  704. else
  705. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  706. { write typeinfo of extended type }
  707. if is_objectpascal_helper(def) then
  708. if assigned(def.extendeddef) then
  709. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.extendeddef,fullrtti)))
  710. else
  711. InternalError(2011033001);
  712. { total number of unique properties }
  713. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(propnamelist.count));
  714. { write unit name }
  715. write_string(current_module.realmodulename^);
  716. maybe_write_align;
  717. { write published properties for this object }
  718. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(published_properties_count(def.symtable)));
  719. maybe_write_align;
  720. published_properties_write_rtti_data(propnamelist,def.symtable);
  721. propnamelist.free;
  722. end;
  723. procedure objectdef_rtti_interface_full(def:tobjectdef);
  724. var
  725. i : longint;
  726. propnamelist : TFPHashObjectList;
  727. { if changed to a set, make sure it's still a byte large, and
  728. swap appropriately when cross-compiling
  729. }
  730. IntfFlags: byte;
  731. begin
  732. { Collect unique property names with nameindex }
  733. propnamelist:=TFPHashObjectList.Create;
  734. collect_propnamelist(propnamelist,def);
  735. { write parent typeinfo }
  736. if assigned(def.childof) then
  737. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.childof,fullrtti)))
  738. else
  739. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  740. { interface: write flags, iid and iidstr }
  741. IntfFlags:=0;
  742. if assigned(def.iidguid) then
  743. IntfFlags:=IntfFlags or (1 shl ord(ifHasGuid));
  744. if assigned(def.iidstr) then
  745. IntfFlags:=IntfFlags or (1 shl ord(ifHasStrGUID));
  746. if (def.objecttype=odt_dispinterface) then
  747. IntfFlags:=IntfFlags or (1 shl ord(ifDispInterface));
  748. if (target_info.endian=endian_big) then
  749. IntfFlags:=reverse_byte(IntfFlags);
  750. {
  751. ifDispatch, }
  752. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(IntfFlags));
  753. maybe_write_align;
  754. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(def.iidguid^.D1)));
  755. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(def.iidguid^.D2));
  756. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(def.iidguid^.D3));
  757. for i:=Low(def.iidguid^.D4) to High(def.iidguid^.D4) do
  758. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.iidguid^.D4[i]));
  759. { write unit name }
  760. write_string(current_module.realmodulename^);
  761. maybe_write_align;
  762. { write iidstr }
  763. if assigned(def.iidstr) then
  764. write_string(def.iidstr^)
  765. else
  766. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(0));
  767. maybe_write_align;
  768. { write published properties for this object }
  769. published_properties_write_rtti_data(propnamelist,def.symtable);
  770. propnamelist.free;
  771. end;
  772. begin
  773. case def.objecttype of
  774. odt_class:
  775. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkclass));
  776. odt_object:
  777. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkobject));
  778. odt_dispinterface,
  779. odt_interfacecom:
  780. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkinterface));
  781. odt_interfacecorba:
  782. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkinterfaceCorba));
  783. odt_helper:
  784. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkhelper));
  785. else
  786. internalerror(200611034);
  787. end;
  788. { generate the name }
  789. write_string(def.objrealname^);
  790. maybe_write_align;
  791. case rt of
  792. initrtti :
  793. begin
  794. if def.objecttype in [odt_class,odt_object,odt_helper] then
  795. objectdef_rtti_fields(def)
  796. else
  797. objectdef_rtti_interface_init(def);
  798. end;
  799. fullrtti :
  800. begin
  801. case def.objecttype of
  802. odt_helper,
  803. odt_class:
  804. objectdef_rtti_class_full(def);
  805. odt_object:
  806. objectdef_rtti_fields(def);
  807. else
  808. objectdef_rtti_interface_full(def);
  809. end;
  810. end;
  811. end;
  812. end;
  813. begin
  814. case def.typ of
  815. variantdef :
  816. variantdef_rtti(tvariantdef(def));
  817. stringdef :
  818. stringdef_rtti(tstringdef(def));
  819. enumdef :
  820. enumdef_rtti(tenumdef(def));
  821. orddef :
  822. orddef_rtti(torddef(def));
  823. floatdef :
  824. floatdef_rtti(tfloatdef(def));
  825. setdef :
  826. setdef_rtti(tsetdef(def));
  827. procvardef :
  828. procvardef_rtti(tprocvardef(def));
  829. arraydef :
  830. begin
  831. if ado_IsBitPacked in tarraydef(def).arrayoptions then
  832. unknown_rtti(tstoreddef(def))
  833. else
  834. arraydef_rtti(tarraydef(def));
  835. end;
  836. recorddef :
  837. begin
  838. if trecorddef(def).is_packed then
  839. unknown_rtti(tstoreddef(def))
  840. else
  841. recorddef_rtti(trecorddef(def));
  842. end;
  843. objectdef :
  844. objectdef_rtti(tobjectdef(def));
  845. else
  846. unknown_rtti(tstoreddef(def));
  847. end;
  848. end;
  849. procedure TRTTIWriter.write_rtti_extrasyms(def:Tdef;rt:Trttitype;mainrtti:Tasmsymbol);
  850. type Penumsym = ^Tenumsym;
  851. function enumdef_rtti_calcstringtablestart(const def : Tenumdef) : integer;
  852. begin
  853. { the alignment calls must correspond to the ones used during generating the
  854. actual data structure created elsewhere in this file }
  855. result:=1;
  856. if assigned(def.typesym) then
  857. inc(result,length(def.typesym.realname)+1)
  858. else
  859. inc(result);
  860. if (tf_requires_proper_alignment in target_info.flags) then
  861. result:=align(result,sizeof(Tconstptruint));
  862. inc(result);
  863. if (tf_requires_proper_alignment in target_info.flags) then
  864. result:=align(result,sizeof(Tconstptruint));
  865. inc(result, sizeof(longint) * 2);
  866. if (tf_requires_proper_alignment in target_info.flags) then
  867. result:=align(result,sizeof(Tconstptruint));
  868. inc(result, sizeof(pint));
  869. end;
  870. { Writes a helper table for accelerated conversion of ordinal enum values to strings.
  871. If you change something in this method, make sure to adapt the corresponding code
  872. in sstrings.inc. }
  873. procedure enumdef_rtti_ord2stringindex(const sym_count:longint; const offsets:plongint; const syms:Penumsym; const st:longint);
  874. var rttilab:Tasmsymbol;
  875. h,i,o:longint;
  876. mode:(lookup,search); {Modify with care, ordinal value of enum is written.}
  877. r:single; {Must be real type because of integer overflow risk.}
  878. begin
  879. {Decide wether a lookup array is size efficient.}
  880. mode:=lookup;
  881. if sym_count>0 then
  882. begin
  883. i:=1;
  884. r:=0;
  885. h:=syms[0].value; {Next expected enum value is min.}
  886. while i<sym_count do
  887. begin
  888. {Calculate size of hole between values. Avoid integer overflows.}
  889. r:=r+(single(syms[i].value)-single(h))-1;
  890. h:=syms[i].value;
  891. inc(i);
  892. end;
  893. if r>sym_count then
  894. mode:=search; {Don't waste more than 50% space.}
  895. end;
  896. { write rtti data; make sure that the alignment matches the corresponding data structure
  897. in the code that uses it (if alignment is required). }
  898. with current_asmdata do
  899. begin
  900. rttilab:=defineasmsymbol(Tstoreddef(def).rtti_mangledname(rt)+'_o2s',AB_GLOBAL,AT_DATA);
  901. maybe_new_object_file(asmlists[al_rtti]);
  902. new_section(asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(pint)));
  903. asmlists[al_rtti].concat(Tai_symbol.create_global(rttilab,0));
  904. asmlists[al_rtti].concat(Tai_const.create_32bit(longint(mode)));
  905. if mode=lookup then
  906. begin
  907. maybe_write_align;
  908. o:=syms[0].value; {Start with min value.}
  909. for i:=0 to sym_count-1 do
  910. begin
  911. while o<syms[i].value do
  912. begin
  913. asmlists[al_rtti].concat(Tai_const.create_pint(0));
  914. inc(o);
  915. end;
  916. inc(o);
  917. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  918. end;
  919. end
  920. else
  921. begin
  922. maybe_write_align;
  923. asmlists[al_rtti].concat(Tai_const.create_32bit(sym_count));
  924. for i:=0 to sym_count-1 do
  925. begin
  926. maybe_write_align;
  927. asmlists[al_rtti].concat(Tai_const.create_32bit(syms[i].value));
  928. maybe_write_align;
  929. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  930. end;
  931. end;
  932. asmlists[al_rtti].concat(Tai_symbol_end.create(rttilab));
  933. end;
  934. end;
  935. { Writes a helper table for accelerated conversion of string to ordinal enum values.
  936. If you change something in this method, make sure to adapt the corresponding code
  937. in sstrings.inc. }
  938. procedure enumdef_rtti_string2ordindex(const sym_count:longint; const offsets:plongint; const syms:Penumsym; const st:longint);
  939. var rttilab:Tasmsymbol;
  940. i:longint;
  941. begin
  942. { write rtti data }
  943. with current_asmdata do
  944. begin
  945. rttilab:=defineasmsymbol(Tstoreddef(def).rtti_mangledname(rt)+'_s2o',AB_GLOBAL,AT_DATA);
  946. maybe_new_object_file(asmlists[al_rtti]);
  947. new_section(asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(pint)));
  948. asmlists[al_rtti].concat(Tai_symbol.create_global(rttilab,0));
  949. asmlists[al_rtti].concat(Tai_const.create_32bit(sym_count));
  950. { need to align the entry record according to the largest member }
  951. maybe_write_align;
  952. for i:=0 to sym_count-1 do
  953. begin
  954. if (tf_requires_proper_alignment in target_info.flags) then
  955. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(4)); // necessary?
  956. asmlists[al_rtti].concat(Tai_const.create_32bit(syms[i].value));
  957. maybe_write_align;
  958. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  959. end;
  960. asmlists[al_rtti].concat(Tai_symbol_end.create(rttilab));
  961. end;
  962. end;
  963. procedure enumdef_rtti_extrasyms(def:Tenumdef);
  964. var
  965. t:Tenumsym;
  966. syms:Penumsym;
  967. sym_count,sym_alloc:sizeuint;
  968. offsets:^longint;
  969. h,i,p,o,st:longint;
  970. begin
  971. {Random access needed, put in array.}
  972. getmem(syms,64*sizeof(Tenumsym));
  973. getmem(offsets,64*sizeof(longint));
  974. sym_count:=0;
  975. sym_alloc:=64;
  976. st:=0;
  977. for i := 0 to def.symtable.SymList.Count - 1 do
  978. begin
  979. t:=tenumsym(def.symtable.SymList[i]);
  980. if t.value<def.minval then
  981. continue
  982. else
  983. if t.value>def.maxval then
  984. break;
  985. if sym_count>=sym_alloc then
  986. begin
  987. reallocmem(syms,2*sym_alloc*sizeof(Tenumsym));
  988. reallocmem(offsets,2*sym_alloc*sizeof(longint));
  989. sym_alloc:=sym_alloc*2;
  990. end;
  991. syms[sym_count]:=t;
  992. offsets[sym_count]:=st;
  993. inc(sym_count);
  994. st:=st+length(t.realname)+1;
  995. end;
  996. {Sort the syms by enum name}
  997. if sym_count>=2 then
  998. begin
  999. p:=1;
  1000. while 2*p<sym_count do
  1001. p:=2*p;
  1002. while p<>0 do
  1003. begin
  1004. for h:=p to sym_count-1 do
  1005. begin
  1006. i:=h;
  1007. t:=syms[i];
  1008. o:=offsets[i];
  1009. repeat
  1010. if syms[i-p].name<=t.name then
  1011. break;
  1012. syms[i]:=syms[i-p];
  1013. offsets[i]:=offsets[i-p];
  1014. dec(i,p);
  1015. until i<p;
  1016. syms[i]:=t;
  1017. offsets[i]:=o;
  1018. end;
  1019. p:=p shr 1;
  1020. end;
  1021. end;
  1022. st:=enumdef_rtti_calcstringtablestart(def);
  1023. enumdef_rtti_string2ordindex(sym_count,offsets,syms,st);
  1024. { Sort the syms by enum value }
  1025. if sym_count>=2 then
  1026. begin
  1027. p:=1;
  1028. while 2*p<sym_count do
  1029. p:=2*p;
  1030. while p<>0 do
  1031. begin
  1032. for h:=p to sym_count-1 do
  1033. begin
  1034. i:=h;
  1035. t:=syms[i];
  1036. o:=offsets[i];
  1037. repeat
  1038. if syms[i-p].value<=t.value then
  1039. break;
  1040. syms[i]:=syms[i-p];
  1041. offsets[i]:=offsets[i-p];
  1042. dec(i,p);
  1043. until i<p;
  1044. syms[i]:=t;
  1045. offsets[i]:=o;
  1046. end;
  1047. p:=p shr 1;
  1048. end;
  1049. end;
  1050. enumdef_rtti_ord2stringindex(sym_count,offsets,syms,st);
  1051. freemem(syms);
  1052. freemem(offsets);
  1053. end;
  1054. begin
  1055. case def.typ of
  1056. enumdef:
  1057. if rt=fullrtti then
  1058. begin
  1059. enumdef_rtti_extrasyms(Tenumdef(def));
  1060. end;
  1061. end;
  1062. end;
  1063. procedure TRTTIWriter.write_child_rtti_data(def:tdef;rt:trttitype);
  1064. begin
  1065. case def.typ of
  1066. enumdef :
  1067. if assigned(tenumdef(def).basedef) then
  1068. write_rtti(tenumdef(def).basedef,rt);
  1069. setdef :
  1070. write_rtti(tsetdef(def).elementdef,rt);
  1071. arraydef :
  1072. write_rtti(tarraydef(def).elementdef,rt);
  1073. recorddef :
  1074. fields_write_rtti(trecorddef(def).symtable,rt);
  1075. objectdef :
  1076. begin
  1077. if assigned(tobjectdef(def).childof) then
  1078. write_rtti(tobjectdef(def).childof,rt);
  1079. if (rt=initrtti) or (tobjectdef(def).objecttype=odt_object) then
  1080. fields_write_rtti(tobjectdef(def).symtable,rt)
  1081. else
  1082. published_write_rtti(tobjectdef(def).symtable,rt);
  1083. end;
  1084. end;
  1085. end;
  1086. function TRTTIWriter.ref_rtti(def:tdef;rt:trttitype):tasmsymbol;
  1087. begin
  1088. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt));
  1089. end;
  1090. procedure TRTTIWriter.write_rtti(def:tdef;rt:trttitype);
  1091. var
  1092. rttilab : tasmsymbol;
  1093. begin
  1094. { only write rtti of definitions from the current module }
  1095. if not findunitsymtable(def.owner).iscurrentunit then
  1096. exit;
  1097. { prevent recursion }
  1098. if rttidefstate[rt] in def.defstates then
  1099. exit;
  1100. include(def.defstates,rttidefstate[rt]);
  1101. { write first all dependencies }
  1102. write_child_rtti_data(def,rt);
  1103. { write rtti data }
  1104. rttilab:=current_asmdata.DefineAsmSymbol(tstoreddef(def).rtti_mangledname(rt),AB_GLOBAL,AT_DATA);
  1105. maybe_new_object_file(current_asmdata.asmlists[al_rtti]);
  1106. new_section(current_asmdata.asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(pint)));
  1107. current_asmdata.asmlists[al_rtti].concat(Tai_symbol.Create_global(rttilab,0));
  1108. write_rtti_data(def,rt);
  1109. current_asmdata.asmlists[al_rtti].concat(Tai_symbol_end.Create(rttilab));
  1110. write_rtti_extrasyms(def,rt,rttilab);
  1111. end;
  1112. function TRTTIWriter.get_rtti_label(def:tdef;rt:trttitype):tasmsymbol;
  1113. begin
  1114. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt));
  1115. end;
  1116. function TRTTIWriter.get_rtti_label_ord2str(def:tdef;rt:trttitype):tasmsymbol;
  1117. begin
  1118. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt)+'_o2s');
  1119. end;
  1120. function TRTTIWriter.get_rtti_label_str2ord(def:tdef;rt:trttitype):tasmsymbol;
  1121. begin
  1122. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt)+'_s2o');
  1123. end;
  1124. end.