ncgrtti.pas 51 KB

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