ncgrtti.pas 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  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;indirect:boolean):tasmsymbol;
  51. function get_rtti_label_ord2str(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  52. function get_rtti_label_str2ord(def:tdef;rt:trttitype;indirect:boolean):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] PPTypeInfo }
  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. { pocall_hardfloat } 13
  807. );
  808. procedure write_param_flag(parasym:tparavarsym);
  809. var
  810. paraspec : byte;
  811. begin
  812. case parasym.varspez of
  813. vs_value : paraspec := 0;
  814. vs_const : paraspec := pfConst;
  815. vs_var : paraspec := pfVar;
  816. vs_out : paraspec := pfOut;
  817. vs_constref: paraspec := pfConstRef;
  818. else
  819. internalerror(2013112904);
  820. end;
  821. { Kylix also seems to always add both pfArray and pfReference
  822. in this case
  823. }
  824. if is_open_array(parasym.vardef) then
  825. paraspec:=paraspec or pfArray or pfReference;
  826. { and these for classes and interfaces (maybe because they
  827. are themselves addresses?)
  828. }
  829. if is_class_or_interface(parasym.vardef) then
  830. paraspec:=paraspec or pfAddress;
  831. { set bits run from the highest to the lowest bit on
  832. big endian systems
  833. }
  834. if (target_info.endian = endian_big) then
  835. paraspec:=reverse_byte(paraspec);
  836. { write flags for current parameter }
  837. tcb.emit_ord_const(paraspec,u8inttype);
  838. end;
  839. procedure write_para(parasym:tparavarsym);
  840. begin
  841. { only store user visible parameters }
  842. if not(vo_is_hidden_para in parasym.varoptions) then
  843. begin
  844. { write flags for current parameter }
  845. write_param_flag(parasym);
  846. { write name of current parameter }
  847. tcb.emit_shortstring_const(parasym.realname);
  848. { write name of type of current parameter }
  849. write_rtti_name(tcb,parasym.vardef);
  850. end;
  851. end;
  852. procedure write_procedure_param(parasym:tparavarsym);
  853. begin
  854. { only store user visible parameters }
  855. if not(vo_is_hidden_para in parasym.varoptions) then
  856. begin
  857. { every parameter is expected to start aligned }
  858. tcb.begin_anonymous_record(
  859. internaltypeprefixName[itp_rtti_proc_param]+tostr(length(parasym.realname)),
  860. defaultpacking,reqalign,
  861. targetinfos[target_info.system]^.alignment.recordalignmin,
  862. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  863. { write flags for current parameter }
  864. write_param_flag(parasym);
  865. { write param type }
  866. write_rtti_reference(tcb,parasym.vardef,fullrtti);
  867. { write name of current parameter }
  868. tcb.emit_shortstring_const(parasym.realname);
  869. tcb.end_anonymous_record;
  870. end;
  871. end;
  872. var
  873. methodkind : byte;
  874. i : integer;
  875. begin
  876. if po_methodpointer in def.procoptions then
  877. begin
  878. { write method id and name }
  879. write_header(tcb,def,tkMethod);
  880. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  881. targetinfos[target_info.system]^.alignment.recordalignmin,
  882. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  883. { write kind of method }
  884. case def.proctypeoption of
  885. potype_constructor: methodkind:=mkConstructor;
  886. potype_destructor: methodkind:=mkDestructor;
  887. potype_class_constructor: methodkind:=mkClassConstructor;
  888. potype_class_destructor: methodkind:=mkClassDestructor;
  889. potype_operator: methodkind:=mkOperatorOverload;
  890. potype_procedure:
  891. if po_classmethod in def.procoptions then
  892. methodkind:=mkClassProcedure
  893. else
  894. methodkind:=mkProcedure;
  895. potype_function:
  896. if po_classmethod in def.procoptions then
  897. methodkind:=mkClassFunction
  898. else
  899. methodkind:=mkFunction;
  900. else
  901. begin
  902. if def.returndef = voidtype then
  903. methodkind:=mkProcedure
  904. else
  905. methodkind:=mkFunction;
  906. end;
  907. end;
  908. tcb.emit_ord_const(methodkind,u8inttype);
  909. { write parameter info. The parameters must be written in reverse order
  910. if this method uses right to left parameter pushing! }
  911. tcb.emit_ord_const(def.maxparacount,u8inttype);
  912. for i:=0 to def.paras.count-1 do
  913. write_para(tparavarsym(def.paras[i]));
  914. if (methodkind=mkFunction) or (methodkind=mkClassFunction) then
  915. begin
  916. { write name of result type }
  917. write_rtti_name(tcb,def.returndef);
  918. { enclosing record takes care of alignment }
  919. { write result typeinfo }
  920. write_rtti_reference(tcb,def.returndef,fullrtti);
  921. end;
  922. { write calling convention }
  923. tcb.emit_ord_const(ProcCallOptionToCallConv[def.proccalloption],u8inttype);
  924. { enclosing record takes care of alignment }
  925. { write params typeinfo }
  926. for i:=0 to def.paras.count-1 do
  927. if not(vo_is_hidden_para in tparavarsym(def.paras[i]).varoptions) then
  928. write_rtti_reference(tcb,tparavarsym(def.paras[i]).vardef,fullrtti);
  929. tcb.end_anonymous_record;
  930. end
  931. else
  932. begin
  933. write_header(tcb,def,tkProcvar);
  934. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  935. targetinfos[target_info.system]^.alignment.recordalignmin,
  936. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  937. { flags }
  938. tcb.emit_ord_const(0,u8inttype);
  939. { write calling convention }
  940. tcb.emit_ord_const(ProcCallOptionToCallConv[def.proccalloption],u8inttype);
  941. { enclosing record takes care of alignment }
  942. { write result typeinfo }
  943. write_rtti_reference(tcb,def.returndef,fullrtti);
  944. { write parameter count }
  945. tcb.emit_ord_const(def.maxparacount,u8inttype);
  946. for i:=0 to def.paras.count-1 do
  947. write_procedure_param(tparavarsym(def.paras[i]));
  948. tcb.end_anonymous_record;
  949. end;
  950. end;
  951. procedure objectdef_rtti(def: tobjectdef);
  952. procedure objectdef_rtti_fields(def:tobjectdef);
  953. begin
  954. tcb.emit_ord_const(def.size, u32inttype);
  955. { enclosing record takes care of alignment }
  956. fields_write_rtti_data(tcb,def,rt);
  957. end;
  958. procedure objectdef_rtti_interface_init(def:tobjectdef);
  959. begin
  960. tcb.emit_ord_const(def.size, u32inttype);
  961. end;
  962. procedure objectdef_rtti_class_full(def:tobjectdef);
  963. var
  964. propnamelist : TFPHashObjectList;
  965. begin
  966. { Collect unique property names with nameindex }
  967. propnamelist:=TFPHashObjectList.Create;
  968. collect_propnamelist(propnamelist,def);
  969. if not is_objectpascal_helper(def) then
  970. if (oo_has_vmt in def.objectoptions) then
  971. tcb.emit_tai(
  972. Tai_const.Createname(def.vmt_mangledname,AT_DATA_FORCEINDIRECT,0),
  973. cpointerdef.getreusable(def.vmt_def))
  974. else
  975. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  976. { write parent typeinfo }
  977. write_rtti_reference(tcb,def.childof,fullrtti);
  978. { write typeinfo of extended type }
  979. if is_objectpascal_helper(def) then
  980. if assigned(def.extendeddef) then
  981. write_rtti_reference(tcb,def.extendeddef,fullrtti)
  982. else
  983. InternalError(2011033001);
  984. { total number of unique properties }
  985. tcb.emit_ord_const(propnamelist.count,u16inttype);
  986. { write unit name }
  987. tcb.emit_shortstring_const(current_module.realmodulename^);
  988. { write published properties for this object }
  989. published_properties_write_rtti_data(tcb,propnamelist,def.symtable);
  990. propnamelist.free;
  991. end;
  992. procedure objectdef_rtti_interface_full(def:tobjectdef);
  993. var
  994. propnamelist : TFPHashObjectList;
  995. { if changed to a set, make sure it's still a byte large, and
  996. swap appropriately when cross-compiling
  997. }
  998. IntfFlags: byte;
  999. begin
  1000. { Collect unique property names with nameindex }
  1001. propnamelist:=TFPHashObjectList.Create;
  1002. collect_propnamelist(propnamelist,def);
  1003. { write parent typeinfo }
  1004. write_rtti_reference(tcb,def.childof,fullrtti);
  1005. { interface: write flags, iid and iidstr }
  1006. IntfFlags:=0;
  1007. if assigned(def.iidguid) then
  1008. IntfFlags:=IntfFlags or (1 shl ord(ifHasGuid));
  1009. if (def.objecttype=odt_interfacecorba) and (def.iidstr^<>'') then
  1010. IntfFlags:=IntfFlags or (1 shl ord(ifHasStrGUID));
  1011. if (def.objecttype=odt_dispinterface) then
  1012. IntfFlags:=IntfFlags or (1 shl ord(ifDispInterface));
  1013. if (target_info.endian=endian_big) then
  1014. IntfFlags:=reverse_byte(IntfFlags);
  1015. {
  1016. ifDispatch, }
  1017. tcb.emit_ord_const(IntfFlags,u8inttype);
  1018. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1019. targetinfos[target_info.system]^.alignment.recordalignmin,
  1020. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1021. tcb.emit_guid_const(def.iidguid^);
  1022. { write unit name }
  1023. tcb.emit_shortstring_const(current_module.realmodulename^);
  1024. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1025. targetinfos[target_info.system]^.alignment.recordalignmin,
  1026. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1027. { write iidstr }
  1028. if def.objecttype=odt_interfacecorba then
  1029. begin
  1030. { prepareguid always allocates an empty string }
  1031. if not assigned(def.iidstr) then
  1032. internalerror(2016021901);
  1033. tcb.emit_shortstring_const(def.iidstr^)
  1034. end;
  1035. { write published properties for this object }
  1036. published_properties_write_rtti_data(tcb,propnamelist,def.symtable);
  1037. tcb.end_anonymous_record;
  1038. tcb.end_anonymous_record;
  1039. propnamelist.free;
  1040. end;
  1041. begin
  1042. case def.objecttype of
  1043. odt_class:
  1044. tcb.emit_ord_const(tkclass,u8inttype);
  1045. odt_object:
  1046. tcb.emit_ord_const(tkobject,u8inttype);
  1047. odt_dispinterface,
  1048. odt_interfacecom:
  1049. tcb.emit_ord_const(tkInterface,u8inttype);
  1050. odt_interfacecorba:
  1051. tcb.emit_ord_const(tkinterfaceCorba,u8inttype);
  1052. odt_helper:
  1053. tcb.emit_ord_const(tkhelper,u8inttype);
  1054. else
  1055. internalerror(200611034);
  1056. end;
  1057. { generate the name }
  1058. tcb.emit_shortstring_const(def.objrealname^);
  1059. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1060. targetinfos[target_info.system]^.alignment.recordalignmin,
  1061. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1062. case rt of
  1063. initrtti :
  1064. begin
  1065. if def.objecttype in [odt_class,odt_object,odt_helper] then
  1066. objectdef_rtti_fields(def)
  1067. else
  1068. objectdef_rtti_interface_init(def);
  1069. end;
  1070. fullrtti :
  1071. begin
  1072. case def.objecttype of
  1073. odt_helper,
  1074. odt_class:
  1075. objectdef_rtti_class_full(def);
  1076. odt_object:
  1077. objectdef_rtti_fields(def);
  1078. else
  1079. objectdef_rtti_interface_full(def);
  1080. end;
  1081. end;
  1082. end;
  1083. tcb.end_anonymous_record;
  1084. end;
  1085. begin
  1086. case def.typ of
  1087. variantdef :
  1088. variantdef_rtti(tvariantdef(def));
  1089. stringdef :
  1090. stringdef_rtti(tstringdef(def));
  1091. enumdef :
  1092. enumdef_rtti(tenumdef(def));
  1093. orddef :
  1094. orddef_rtti(torddef(def));
  1095. floatdef :
  1096. floatdef_rtti(tfloatdef(def));
  1097. setdef :
  1098. setdef_rtti(tsetdef(def));
  1099. procvardef :
  1100. procvardef_rtti(tprocvardef(def));
  1101. arraydef :
  1102. begin
  1103. if ado_IsBitPacked in tarraydef(def).arrayoptions then
  1104. unknown_rtti(tstoreddef(def))
  1105. else
  1106. arraydef_rtti(tarraydef(def));
  1107. end;
  1108. recorddef :
  1109. begin
  1110. if trecorddef(def).is_packed then
  1111. unknown_rtti(tstoreddef(def))
  1112. else
  1113. recorddef_rtti(trecorddef(def));
  1114. end;
  1115. objectdef :
  1116. objectdef_rtti(tobjectdef(def));
  1117. classrefdef :
  1118. classrefdef_rtti(tclassrefdef(def));
  1119. pointerdef :
  1120. pointerdef_rtti(tpointerdef(def));
  1121. else
  1122. unknown_rtti(tstoreddef(def));
  1123. end;
  1124. end;
  1125. function enumsym_compare_name(item1, item2: pointer): Integer;
  1126. var
  1127. enum1: tenumsym absolute item1;
  1128. enum2: tenumsym absolute item2;
  1129. begin
  1130. if enum1=enum2 then
  1131. result:=0
  1132. else if enum1.name>enum2.name then
  1133. result:=1
  1134. else
  1135. { there can't be equal names, identifiers are unique }
  1136. result:=-1;
  1137. end;
  1138. function enumsym_compare_value(item1, item2: pointer): Integer;
  1139. var
  1140. enum1: tenumsym absolute item1;
  1141. enum2: tenumsym absolute item2;
  1142. begin
  1143. if enum1.value>enum2.value then
  1144. result:=1
  1145. else if enum1.value<enum2.value then
  1146. result:=-1
  1147. else
  1148. result:=0;
  1149. end;
  1150. procedure TRTTIWriter.write_rtti_extrasyms(def:Tdef;rt:Trttitype;mainrtti:Tasmsymbol);
  1151. type Penumsym = ^Tenumsym;
  1152. { Writes a helper table for accelerated conversion of ordinal enum values to strings.
  1153. If you change something in this method, make sure to adapt the corresponding code
  1154. in sstrings.inc. }
  1155. procedure enumdef_rtti_ord2stringindex(rttidef: trecorddef; const syms: tfplist);
  1156. var rttilab:Tasmsymbol;
  1157. h,i,o,prev_value:longint;
  1158. mode:(lookup,search); {Modify with care, ordinal value of enum is written.}
  1159. r:single; {Must be real type because of integer overflow risk.}
  1160. tcb: ttai_typedconstbuilder;
  1161. sym_count: integer;
  1162. tabledef: tdef;
  1163. begin
  1164. {Decide wether a lookup array is size efficient.}
  1165. mode:=lookup;
  1166. sym_count:=syms.count;
  1167. if sym_count>0 then
  1168. begin
  1169. i:=1;
  1170. r:=0;
  1171. h:=tenumsym(syms[0]).value; {Next expected enum value is min.}
  1172. { set prev_value for the first iteration to a value that is
  1173. different from the first one without risking overflow (it's used
  1174. to detect whether two enum values are the same) }
  1175. if h=0 then
  1176. prev_value:=1
  1177. else
  1178. prev_value:=0;
  1179. while i<sym_count do
  1180. begin
  1181. { if two enum values are the same, we have to create a table }
  1182. if (prev_value=h) then
  1183. begin
  1184. mode:=search;
  1185. break;
  1186. end;
  1187. {Calculate size of hole between values. Avoid integer overflows.}
  1188. r:=r+(single(tenumsym(syms[i]).value)-single(h))-1;
  1189. prev_value:=h;
  1190. h:=tenumsym(syms[i]).value;
  1191. inc(i);
  1192. end;
  1193. if r>sym_count then
  1194. mode:=search; {Don't waste more than 50% space.}
  1195. end;
  1196. { write rtti data; make sure that the alignment matches the corresponding data structure
  1197. in the code that uses it (if alignment is required). }
  1198. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable]);
  1199. { use TConstPtrUInt packrecords to ensure good alignment }
  1200. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1201. targetinfos[target_info.system]^.alignment.recordalignmin,
  1202. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1203. { now emit the data: first the mode }
  1204. tcb.emit_tai(Tai_const.create_32bit(longint(mode)),u32inttype);
  1205. { align }
  1206. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1207. targetinfos[target_info.system]^.alignment.recordalignmin,
  1208. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1209. if mode=lookup then
  1210. begin
  1211. o:=tenumsym(syms[0]).value; {Start with min value.}
  1212. for i:=0 to sym_count-1 do
  1213. begin
  1214. while o<tenumsym(syms[i]).value do
  1215. begin
  1216. tcb.emit_tai(Tai_const.create_pint(0),ptruinttype);
  1217. inc(o);
  1218. end;
  1219. inc(o);
  1220. tcb.queue_init(voidpointertype);
  1221. tcb.queue_subscriptn_multiple_by_name(rttidef,
  1222. ['size_start_rec',
  1223. 'min_max_rec',
  1224. 'basetype_array_rec',
  1225. tsym(syms[i]).Name]
  1226. );
  1227. tcb.queue_emit_asmsym(mainrtti,rttidef);
  1228. end;
  1229. end
  1230. else
  1231. begin
  1232. tcb.emit_ord_const(sym_count,u32inttype);
  1233. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1234. targetinfos[target_info.system]^.alignment.recordalignmin,
  1235. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1236. for i:=0 to sym_count-1 do
  1237. begin
  1238. tcb.emit_ord_const(tenumsym(syms[i]).value,s32inttype);
  1239. tcb.queue_init(voidpointertype);
  1240. tcb.queue_subscriptn_multiple_by_name(rttidef,
  1241. ['size_start_rec',
  1242. 'min_max_rec',
  1243. 'basetype_array_rec',
  1244. tsym(syms[i]).Name]
  1245. );
  1246. tcb.queue_emit_asmsym(mainrtti,rttidef);
  1247. end;
  1248. tcb.end_anonymous_record;
  1249. end;
  1250. tcb.end_anonymous_record;
  1251. tabledef:=tcb.end_anonymous_record;
  1252. rttilab:=current_asmdata.DefineAsmSymbol(Tstoreddef(def).rtti_mangledname(rt)+'_o2s',AB_GLOBAL,AT_DATA_FORCEINDIRECT,tabledef);
  1253. current_asmdata.asmlists[al_rtti].concatlist(tcb.get_final_asmlist(
  1254. rttilab,tabledef,sec_rodata,
  1255. rttilab.name,const_align(sizeof(pint))));
  1256. tcb.free;
  1257. current_module.add_public_asmsym(rttilab);
  1258. end;
  1259. { Writes a helper table for accelerated conversion of string to ordinal enum values.
  1260. If you change something in this method, make sure to adapt the corresponding code
  1261. in sstrings.inc. }
  1262. procedure enumdef_rtti_string2ordindex(rttidef: trecorddef; const syms: tfplist);
  1263. var
  1264. tcb: ttai_typedconstbuilder;
  1265. rttilab: Tasmsymbol;
  1266. i:longint;
  1267. tabledef: tdef;
  1268. begin
  1269. { write rtti data }
  1270. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable]);
  1271. { begin of Tstring_to_ord }
  1272. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1273. targetinfos[target_info.system]^.alignment.recordalignmin,
  1274. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1275. tcb.emit_ord_const(syms.count,s32inttype);
  1276. { begin of "data" array in Tstring_to_ord }
  1277. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1278. targetinfos[target_info.system]^.alignment.recordalignmin,
  1279. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1280. for i:=0 to syms.count-1 do
  1281. begin
  1282. tcb.emit_ord_const(tenumsym(syms[i]).value,s32inttype);
  1283. { alignment of pointer value handled by enclosing record already }
  1284. tcb.queue_init(voidpointertype);
  1285. tcb.queue_subscriptn_multiple_by_name(rttidef,
  1286. ['size_start_rec',
  1287. 'min_max_rec',
  1288. 'basetype_array_rec',
  1289. tsym(syms[i]).Name]
  1290. );
  1291. tcb.queue_emit_asmsym(mainrtti,rttidef);
  1292. end;
  1293. tcb.end_anonymous_record;
  1294. tabledef:=tcb.end_anonymous_record;
  1295. rttilab:=current_asmdata.DefineAsmSymbol(Tstoreddef(def).rtti_mangledname(rt)+'_s2o',AB_GLOBAL,AT_DATA_FORCEINDIRECT,tabledef);
  1296. current_asmdata.asmlists[al_rtti].concatlist(tcb.get_final_asmlist(
  1297. rttilab,tabledef,sec_rodata,
  1298. rttilab.name,const_align(sizeof(pint))));
  1299. tcb.free;
  1300. current_module.add_public_asmsym(rttilab);
  1301. end;
  1302. procedure enumdef_rtti_extrasyms(def:Tenumdef);
  1303. var
  1304. t:Tenumsym;
  1305. syms:tfplist;
  1306. i:longint;
  1307. rttitypesym: ttypesym;
  1308. rttidef: trecorddef;
  1309. begin
  1310. { collect enumsyms belonging to this enum type (could be a subsection
  1311. in case of a subrange type) }
  1312. syms:=tfplist.create;
  1313. for i := 0 to def.symtable.SymList.Count - 1 do
  1314. begin
  1315. t:=tenumsym(def.symtable.SymList[i]);
  1316. if t.value<def.minval then
  1317. continue
  1318. else
  1319. if t.value>def.maxval then
  1320. break;
  1321. syms.add(t);
  1322. end;
  1323. { sort the syms by enum name }
  1324. syms.sort(@enumsym_compare_name);
  1325. rttitypesym:=try_search_current_module_type(internaltypeprefixName[itp_rttidef]+def.rtti_mangledname(fullrtti));
  1326. if not assigned(rttitypesym) or
  1327. (ttypesym(rttitypesym).typedef.typ<>recorddef) then
  1328. internalerror(2015071402);
  1329. rttidef:=trecorddef(ttypesym(rttitypesym).typedef);
  1330. enumdef_rtti_string2ordindex(rttidef,syms);
  1331. { sort the syms by enum value }
  1332. syms.sort(@enumsym_compare_value);
  1333. enumdef_rtti_ord2stringindex(rttidef,syms);
  1334. syms.free;
  1335. end;
  1336. begin
  1337. case def.typ of
  1338. enumdef:
  1339. if rt=fullrtti then
  1340. begin
  1341. enumdef_rtti_extrasyms(Tenumdef(def));
  1342. end;
  1343. end;
  1344. end;
  1345. procedure TRTTIWriter.write_child_rtti_data(def:tdef;rt:trttitype);
  1346. begin
  1347. case def.typ of
  1348. enumdef :
  1349. if assigned(tenumdef(def).basedef) then
  1350. write_rtti(tenumdef(def).basedef,rt);
  1351. setdef :
  1352. write_rtti(tsetdef(def).elementdef,rt);
  1353. arraydef :
  1354. begin
  1355. write_rtti(tarraydef(def).rangedef,rt);
  1356. write_rtti(tarraydef(def).elementdef,rt);
  1357. end;
  1358. recorddef :
  1359. fields_write_rtti(trecorddef(def).symtable,rt);
  1360. objectdef :
  1361. begin
  1362. if assigned(tobjectdef(def).childof) then
  1363. write_rtti(tobjectdef(def).childof,rt);
  1364. if (rt=initrtti) or (tobjectdef(def).objecttype=odt_object) then
  1365. fields_write_rtti(tobjectdef(def).symtable,rt)
  1366. else
  1367. published_write_rtti(tobjectdef(def).symtable,rt);
  1368. end;
  1369. classrefdef,
  1370. pointerdef:
  1371. if not is_objc_class_or_protocol(tabstractpointerdef(def).pointeddef) then
  1372. write_rtti(tabstractpointerdef(def).pointeddef,rt);
  1373. procvardef:
  1374. params_write_rtti(tabstractprocdef(def),rt);
  1375. end;
  1376. end;
  1377. procedure TRTTIWriter.write_rtti_reference(tcb: ttai_typedconstbuilder; def: tdef; rt: trttitype);
  1378. begin
  1379. { we don't care about the real type here, because
  1380. a) we don't index into these elements
  1381. b) we may not have the rtti type available at the point that we
  1382. are emitting this data, because of forward definitions etc
  1383. c) if the rtti is emitted in another unit, we won't have the type
  1384. available at all
  1385. For the cases where the type is emitted in the current unit and hence
  1386. the underlying system will detect and complain about symbol def
  1387. mismatches, type conversions will have to be inserted afterwards (like
  1388. in llvm/llvmtype)
  1389. }
  1390. if not assigned(def) or is_void(def) or ((rt<>initrtti) and is_objc_class_or_protocol(def)) then
  1391. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype)
  1392. else
  1393. tcb.emit_tai(Tai_const.Create_sym(ref_rtti(def,rt)),voidpointertype);
  1394. end;
  1395. function TRTTIWriter.ref_rtti(def:tdef;rt:trttitype):tasmsymbol;
  1396. begin
  1397. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt),AT_DATA,true);
  1398. if (cs_create_pic in current_settings.moduleswitches) and
  1399. assigned(current_procinfo) then
  1400. include(current_procinfo.flags,pi_needs_got);
  1401. if def.owner.moduleid<>current_module.moduleid then
  1402. current_module.add_extern_asmsym(result);
  1403. end;
  1404. procedure TRTTIWriter.write_rtti(def:tdef;rt:trttitype);
  1405. var
  1406. tcb: ttai_typedconstbuilder;
  1407. rttilab: tasmsymbol;
  1408. rttidef: tdef;
  1409. begin
  1410. { only write rtti of definitions from the current module }
  1411. if not findunitsymtable(def.owner).iscurrentunit then
  1412. exit;
  1413. { check if separate initrtti is actually needed }
  1414. if (rt=initrtti) and (not def.needs_separate_initrtti) then
  1415. rt:=fullrtti;
  1416. { prevent recursion }
  1417. if rttidefstate[rt] in def.defstates then
  1418. exit;
  1419. include(def.defstates,rttidefstate[rt]);
  1420. { write first all dependencies }
  1421. write_child_rtti_data(def,rt);
  1422. { write rtti data }
  1423. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable]);
  1424. tcb.begin_anonymous_record(
  1425. internaltypeprefixName[itp_rttidef]+tstoreddef(def).rtti_mangledname(rt),
  1426. defaultpacking,reqalign,
  1427. targetinfos[target_info.system]^.alignment.recordalignmin,
  1428. targetinfos[target_info.system]^.alignment.maxCrecordalign
  1429. );
  1430. write_rtti_data(tcb,def,rt);
  1431. rttidef:=tcb.end_anonymous_record;
  1432. rttilab:=current_asmdata.DefineAsmSymbol(tstoreddef(def).rtti_mangledname(rt),AB_GLOBAL,AT_DATA_FORCEINDIRECT,rttidef);
  1433. current_asmdata.AsmLists[al_rtti].concatList(
  1434. tcb.get_final_asmlist(rttilab,rttidef,sec_rodata,rttilab.name,const_align(sizeof(pint))));
  1435. tcb.free;
  1436. current_module.add_public_asmsym(rttilab);
  1437. { write additional data }
  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;indirect:boolean):tasmsymbol;
  1454. begin
  1455. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt),AT_DATA,indirect);
  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;indirect:boolean):tasmsymbol;
  1461. begin
  1462. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt)+'_o2s',AT_DATA,indirect);
  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;indirect:boolean):tasmsymbol;
  1468. begin
  1469. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt)+'_s2o',AT_DATA,indirect);
  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.