ncgrtti.pas 53 KB

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