ncgrtti.pas 63 KB

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