ncgrtti.pas 50 KB

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