ncgrtti.pas 62 KB

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