ncgrtti.pas 60 KB

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