ncgrtti.pas 65 KB

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