ncgrtti.pas 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678
  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 doint32_64(typekind: byte;min,max:int64);
  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,otUQWord,
  577. otSByte,otSWord,otSLong,otSQWord,
  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(min,u64inttype);
  606. tcb.emit_ord_const(max,u64inttype);
  607. end;
  608. otSQWord:
  609. begin
  610. tcb.emit_ord_const(min,s64inttype);
  611. tcb.emit_ord_const(max,s64inttype);
  612. end;
  613. else
  614. begin
  615. tcb.emit_ord_const(longint(min),s32inttype);
  616. tcb.emit_ord_const(longint(max),s32inttype);
  617. end;
  618. end;
  619. tcb.end_anonymous_record;
  620. tcb.end_anonymous_record;
  621. end;
  622. procedure dointeger(typekind:byte);inline;
  623. begin
  624. doint32_64(typekind,int64(def.low.svalue),int64(def.high.svalue));
  625. end;
  626. begin
  627. case def.ordtype of
  628. s64bit :
  629. dointeger(tkInt64);
  630. u64bit :
  631. dointeger(tkQWord);
  632. pasbool8:
  633. dointeger(tkBool);
  634. { use different low/high values to be Delphi compatible }
  635. bool8bit,
  636. bool16bit,
  637. bool32bit:
  638. doint32_64(tkInteger,longint(low(longint)),longint(high(longint)));
  639. bool64bit:
  640. doint32_64(tkInteger,low(int64),high(int64));
  641. uchar:
  642. dointeger(tkChar);
  643. uwidechar:
  644. dointeger(tkWChar);
  645. scurrency:
  646. begin
  647. write_header(tcb,def,tkFloat);
  648. tcb.begin_anonymous_record(
  649. internaltypeprefixName[itp_1byte],
  650. defaultpacking,reqalign,
  651. targetinfos[target_info.system]^.alignment.recordalignmin,
  652. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  653. tcb.emit_ord_const(ftCurr,u8inttype);
  654. tcb.end_anonymous_record;
  655. end;
  656. else
  657. dointeger(tkInteger);
  658. end;
  659. end;
  660. procedure floatdef_rtti(def:tfloatdef);
  661. const
  662. {tfloattype = (s32real,s64real,s80real,sc80real,s64bit,s128bit);}
  663. translate : array[tfloattype] of byte =
  664. (ftSingle,ftDouble,ftExtended,ftExtended,ftComp,ftCurr,ftFloat128);
  665. begin
  666. write_header(tcb,def,tkFloat);
  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. tcb.emit_ord_const(translate[def.floattype],u8inttype);
  673. tcb.end_anonymous_record;
  674. end;
  675. procedure setdef_rtti(def:tsetdef);
  676. begin
  677. write_header(tcb,def,tkSet);
  678. tcb.begin_anonymous_record(
  679. internaltypeprefixName[itp_1byte],
  680. defaultpacking,reqalign,
  681. targetinfos[target_info.system]^.alignment.recordalignmin,
  682. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  683. case def.size of
  684. 1:
  685. tcb.emit_ord_const(otUByte,u8inttype);
  686. 2:
  687. tcb.emit_ord_const(otUWord,u8inttype);
  688. 4:
  689. tcb.emit_ord_const(otULong,u8inttype);
  690. else
  691. tcb.emit_ord_const(otUByte,u8inttype);
  692. end;
  693. { since this record has an alignment of reqalign, its size will also
  694. be rounded up to a multiple of reqalign -> the following value will
  695. also be properly aligned without having to start an extra record }
  696. tcb.end_anonymous_record;
  697. write_rtti_reference(tcb,def.elementdef,rt);
  698. end;
  699. procedure arraydef_rtti(def:tarraydef);
  700. var
  701. i,dimcount: byte;
  702. totalcount: asizeuint;
  703. finaldef: tdef;
  704. curdef:tarraydef;
  705. begin
  706. if ado_IsDynamicArray in def.arrayoptions then
  707. tcb.emit_ord_const(tkDynArray,u8inttype)
  708. else
  709. tcb.emit_ord_const(tkArray,u8inttype);
  710. write_rtti_name(tcb,def);
  711. if not(ado_IsDynamicArray in def.arrayoptions) then
  712. begin
  713. { remember tha last instruction. we will need to insert some
  714. calculated values after it }
  715. finaldef:=def;
  716. totalcount:=1;
  717. dimcount:=0;
  718. repeat
  719. curdef:=tarraydef(finaldef);
  720. finaldef:=curdef.elementdef;
  721. { Dims[i] PTypeInfo }
  722. inc(dimcount);
  723. totalcount:=totalcount*curdef.elecount;
  724. until (finaldef.typ<>arraydef) or
  725. (ado_IsDynamicArray in tarraydef(finaldef).arrayoptions);
  726. tcb.begin_anonymous_record(
  727. internaltypeprefixName[itp_rtti_normal_array]+tostr(dimcount),
  728. defaultpacking,reqalign,
  729. targetinfos[target_info.system]^.alignment.recordalignmin,
  730. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  731. { total size = elecount * elesize of the first arraydef }
  732. tcb.emit_tai(Tai_const.Create_sizeint(def.elecount*def.elesize),sizeuinttype);
  733. { total element count }
  734. tcb.emit_tai(Tai_const.Create_sizeint(asizeint(totalcount)),sizeuinttype);
  735. { last dimension element type }
  736. tcb.emit_tai(Tai_const.Create_sym(ref_rtti(curdef.elementdef,rt,true)),voidpointertype);
  737. { dimension count }
  738. tcb.emit_ord_const(dimcount,u8inttype);
  739. finaldef:=def;
  740. { ranges of the dimensions }
  741. for i:=1 to dimcount do
  742. begin
  743. curdef:=tarraydef(finaldef);
  744. finaldef:=curdef.elementdef;
  745. { Dims[i] PPTypeInfo }
  746. write_rtti_reference(tcb,curdef.rangedef,rt);
  747. end;
  748. end
  749. else
  750. { write a delphi almost compatible dyn. array entry:
  751. there are two types, eltype and eltype2, the latter is nil if the element type needs
  752. no finalization, the former is always valid, delphi has this swapped, but for
  753. compatibility with older fpc versions we do it different, to be delphi compatible,
  754. the names are swapped in typinfo.pp
  755. }
  756. begin
  757. tcb.begin_anonymous_record(
  758. internaltypeprefixName[itp_rtti_dyn_array],
  759. defaultpacking,reqalign,
  760. targetinfos[target_info.system]^.alignment.recordalignmin,
  761. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  762. { size of elements }
  763. tcb.emit_tai(Tai_const.Create_sizeint(def.elesize),sizeuinttype);
  764. { element type }
  765. write_rtti_reference(tcb,def.elementdef,rt);
  766. { variant type }
  767. tcb.emit_ord_const(tstoreddef(def.elementdef).getvardef,s32inttype);
  768. { element type }
  769. if def.elementdef.needs_inittable then
  770. write_rtti_reference(tcb,def.elementdef,rt)
  771. else
  772. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  773. { write unit name }
  774. tcb.emit_shortstring_const(current_module.realmodulename^);
  775. end;
  776. tcb.end_anonymous_record;
  777. end;
  778. procedure classrefdef_rtti(def:tclassrefdef);
  779. begin
  780. write_header(tcb,def,tkClassRef);
  781. { will be aligned thanks to encompassing record }
  782. write_rtti_reference(tcb,def.pointeddef,rt);
  783. end;
  784. procedure pointerdef_rtti(def:tpointerdef);
  785. begin
  786. write_header(tcb,def,tkPointer);
  787. { will be aligned thanks to encompassing record }
  788. write_rtti_reference(tcb,def.pointeddef,rt);
  789. end;
  790. procedure recorddef_rtti(def:trecorddef);
  791. begin
  792. write_header(tcb,def,tkRecord);
  793. { need extra reqalign record, because otherwise the u32 int will
  794. only be aligned to 4 even on 64 bit target (while the rtti code
  795. in typinfo expects alignments to sizeof(pointer)) }
  796. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  797. targetinfos[target_info.system]^.alignment.recordalignmin,
  798. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  799. { store special terminator for init table for more optimal rtl operations
  800. strictly related to RecordRTTI procedure in rtti.inc (directly
  801. related to RTTIRecordRttiInfoToInitInfo function) }
  802. if (rt=initrtti) then
  803. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype)
  804. else
  805. begin
  806. { point to more optimal init table }
  807. include(def.defstates,ds_init_table_used);
  808. { we use a direct reference as the init RTTI is always in the same
  809. unit as the full RTTI }
  810. tcb.emit_tai(Tai_const.Create_sym(ref_rtti(def,initrtti,false)),voidpointertype);
  811. end;
  812. tcb.emit_ord_const(def.size,u32inttype);
  813. fields_write_rtti_data(tcb,def,rt);
  814. tcb.end_anonymous_record;
  815. { guarantee initrtti for any record for fpc_initialize, fpc_finalize }
  816. if (rt=fullrtti) and
  817. (ds_init_table_used in def.defstates) and
  818. not (ds_init_table_written in def.defstates)
  819. then
  820. write_rtti(def, initrtti);
  821. end;
  822. procedure procvardef_rtti(def:tprocvardef);
  823. const
  824. ProcCallOptionToCallConv: array[tproccalloption] of byte = (
  825. { pocall_none } 0,
  826. { pocall_cdecl } 1,
  827. { pocall_cppdecl } 5,
  828. { pocall_far16 } 6,
  829. { pocall_oldfpccall } 7,
  830. { pocall_internproc } 8,
  831. { pocall_syscall } 9,
  832. { pocall_pascal } 2,
  833. { pocall_register } 0,
  834. { pocall_safecall } 4,
  835. { pocall_stdcall } 3,
  836. { pocall_softfloat } 10,
  837. { pocall_mwpascal } 11,
  838. { pocall_interrupt } 12,
  839. { pocall_hardfloat } 13
  840. );
  841. procedure write_param_flag(parasym:tparavarsym);
  842. var
  843. paraspec : byte;
  844. begin
  845. case parasym.varspez of
  846. vs_value : paraspec := 0;
  847. vs_const : paraspec := pfConst;
  848. vs_var : paraspec := pfVar;
  849. vs_out : paraspec := pfOut;
  850. vs_constref: paraspec := pfConstRef;
  851. else
  852. internalerror(2013112904);
  853. end;
  854. { Kylix also seems to always add both pfArray and pfReference
  855. in this case
  856. }
  857. if is_open_array(parasym.vardef) then
  858. paraspec:=paraspec or pfArray or pfReference;
  859. { and these for classes and interfaces (maybe because they
  860. are themselves addresses?)
  861. }
  862. if is_class_or_interface(parasym.vardef) then
  863. paraspec:=paraspec or pfAddress;
  864. { set bits run from the highest to the lowest bit on
  865. big endian systems
  866. }
  867. if (target_info.endian = endian_big) then
  868. paraspec:=reverse_byte(paraspec);
  869. { write flags for current parameter }
  870. tcb.emit_ord_const(paraspec,u8inttype);
  871. end;
  872. procedure write_para(parasym:tparavarsym);
  873. begin
  874. { only store user visible parameters }
  875. if not(vo_is_hidden_para in parasym.varoptions) then
  876. begin
  877. { write flags for current parameter }
  878. write_param_flag(parasym);
  879. { write name of current parameter }
  880. tcb.emit_shortstring_const(parasym.realname);
  881. { write name of type of current parameter }
  882. write_rtti_name(tcb,parasym.vardef);
  883. end;
  884. end;
  885. procedure write_procedure_param(parasym:tparavarsym);
  886. begin
  887. { only store user visible parameters }
  888. if not(vo_is_hidden_para in parasym.varoptions) then
  889. begin
  890. { every parameter is expected to start aligned }
  891. tcb.begin_anonymous_record(
  892. internaltypeprefixName[itp_rtti_proc_param]+tostr(length(parasym.realname)),
  893. defaultpacking,reqalign,
  894. targetinfos[target_info.system]^.alignment.recordalignmin,
  895. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  896. { write flags for current parameter }
  897. write_param_flag(parasym);
  898. { write param type }
  899. write_rtti_reference(tcb,parasym.vardef,fullrtti);
  900. { write name of current parameter }
  901. tcb.emit_shortstring_const(parasym.realname);
  902. tcb.end_anonymous_record;
  903. end;
  904. end;
  905. var
  906. methodkind : byte;
  907. i : integer;
  908. begin
  909. if po_methodpointer in def.procoptions then
  910. begin
  911. { write method id and name }
  912. write_header(tcb,def,tkMethod);
  913. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  914. targetinfos[target_info.system]^.alignment.recordalignmin,
  915. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  916. { write kind of method }
  917. case def.proctypeoption of
  918. potype_constructor: methodkind:=mkConstructor;
  919. potype_destructor: methodkind:=mkDestructor;
  920. potype_class_constructor: methodkind:=mkClassConstructor;
  921. potype_class_destructor: methodkind:=mkClassDestructor;
  922. potype_operator: methodkind:=mkOperatorOverload;
  923. potype_procedure:
  924. if po_classmethod in def.procoptions then
  925. methodkind:=mkClassProcedure
  926. else
  927. methodkind:=mkProcedure;
  928. potype_function:
  929. if po_classmethod in def.procoptions then
  930. methodkind:=mkClassFunction
  931. else
  932. methodkind:=mkFunction;
  933. else
  934. begin
  935. if def.returndef = voidtype then
  936. methodkind:=mkProcedure
  937. else
  938. methodkind:=mkFunction;
  939. end;
  940. end;
  941. tcb.emit_ord_const(methodkind,u8inttype);
  942. { write parameter info. The parameters must be written in reverse order
  943. if this method uses right to left parameter pushing! }
  944. tcb.emit_ord_const(def.maxparacount,u8inttype);
  945. for i:=0 to def.paras.count-1 do
  946. write_para(tparavarsym(def.paras[i]));
  947. if (methodkind=mkFunction) or (methodkind=mkClassFunction) then
  948. begin
  949. { write name of result type }
  950. write_rtti_name(tcb,def.returndef);
  951. { enclosing record takes care of alignment }
  952. { write result typeinfo }
  953. write_rtti_reference(tcb,def.returndef,fullrtti);
  954. end;
  955. { write calling convention }
  956. tcb.emit_ord_const(ProcCallOptionToCallConv[def.proccalloption],u8inttype);
  957. { enclosing record takes care of alignment }
  958. { write params typeinfo }
  959. for i:=0 to def.paras.count-1 do
  960. if not(vo_is_hidden_para in tparavarsym(def.paras[i]).varoptions) then
  961. write_rtti_reference(tcb,tparavarsym(def.paras[i]).vardef,fullrtti);
  962. tcb.end_anonymous_record;
  963. end
  964. else
  965. begin
  966. write_header(tcb,def,tkProcvar);
  967. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  968. targetinfos[target_info.system]^.alignment.recordalignmin,
  969. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  970. { flags }
  971. tcb.emit_ord_const(0,u8inttype);
  972. { write calling convention }
  973. tcb.emit_ord_const(ProcCallOptionToCallConv[def.proccalloption],u8inttype);
  974. { enclosing record takes care of alignment }
  975. { write result typeinfo }
  976. write_rtti_reference(tcb,def.returndef,fullrtti);
  977. { write parameter count }
  978. tcb.emit_ord_const(def.maxparacount,u8inttype);
  979. for i:=0 to def.paras.count-1 do
  980. write_procedure_param(tparavarsym(def.paras[i]));
  981. tcb.end_anonymous_record;
  982. end;
  983. end;
  984. procedure objectdef_rtti(def: tobjectdef);
  985. procedure objectdef_rtti_fields(def:tobjectdef);
  986. begin
  987. { - for compatiblity with record RTTI we need to write a terminator-
  988. Nil pointer as well for objects
  989. - classes are assumed to have the same INIT RTTI as records
  990. (see TObject.CleanupInstance) }
  991. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  992. tcb.emit_ord_const(def.size, u32inttype);
  993. { enclosing record takes care of alignment }
  994. fields_write_rtti_data(tcb,def,rt);
  995. end;
  996. procedure objectdef_rtti_interface_init(def:tobjectdef);
  997. begin
  998. tcb.emit_ord_const(def.size, u32inttype);
  999. end;
  1000. procedure objectdef_rtti_class_full(def:tobjectdef);
  1001. var
  1002. propnamelist : TFPHashObjectList;
  1003. begin
  1004. { Collect unique property names with nameindex }
  1005. propnamelist:=TFPHashObjectList.Create;
  1006. collect_propnamelist(propnamelist,def);
  1007. if not is_objectpascal_helper(def) then
  1008. if (oo_has_vmt in def.objectoptions) then
  1009. tcb.emit_tai(
  1010. Tai_const.Createname(def.vmt_mangledname,AT_DATA_FORCEINDIRECT,0),
  1011. cpointerdef.getreusable(def.vmt_def))
  1012. else
  1013. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1014. { write parent typeinfo }
  1015. write_rtti_reference(tcb,def.childof,fullrtti);
  1016. { write typeinfo of extended type }
  1017. if is_objectpascal_helper(def) then
  1018. if assigned(def.extendeddef) then
  1019. write_rtti_reference(tcb,def.extendeddef,fullrtti)
  1020. else
  1021. InternalError(2011033001);
  1022. { total number of unique properties }
  1023. tcb.emit_ord_const(propnamelist.count,u16inttype);
  1024. { write unit name }
  1025. tcb.emit_shortstring_const(current_module.realmodulename^);
  1026. { write published properties for this object }
  1027. published_properties_write_rtti_data(tcb,propnamelist,def.symtable);
  1028. propnamelist.free;
  1029. end;
  1030. procedure objectdef_rtti_interface_full(def:tobjectdef);
  1031. var
  1032. propnamelist : TFPHashObjectList;
  1033. { if changed to a set, make sure it's still a byte large, and
  1034. swap appropriately when cross-compiling
  1035. }
  1036. IntfFlags: byte;
  1037. begin
  1038. { Collect unique property names with nameindex }
  1039. propnamelist:=TFPHashObjectList.Create;
  1040. collect_propnamelist(propnamelist,def);
  1041. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1042. targetinfos[target_info.system]^.alignment.recordalignmin,
  1043. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1044. { write parent typeinfo }
  1045. write_rtti_reference(tcb,def.childof,fullrtti);
  1046. { interface: write flags, iid and iidstr }
  1047. IntfFlags:=0;
  1048. if assigned(def.iidguid) then
  1049. IntfFlags:=IntfFlags or (1 shl ord(ifHasGuid));
  1050. if (def.objecttype=odt_interfacecorba) and (def.iidstr^<>'') then
  1051. IntfFlags:=IntfFlags or (1 shl ord(ifHasStrGUID));
  1052. if (def.objecttype=odt_dispinterface) then
  1053. IntfFlags:=IntfFlags or (1 shl ord(ifDispInterface));
  1054. if (target_info.endian=endian_big) then
  1055. IntfFlags:=reverse_byte(IntfFlags);
  1056. {
  1057. ifDispatch, }
  1058. tcb.emit_ord_const(IntfFlags,u8inttype);
  1059. { write GUID }
  1060. tcb.emit_guid_const(def.iidguid^);
  1061. { write unit name }
  1062. tcb.emit_shortstring_const(current_module.realmodulename^);
  1063. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1064. targetinfos[target_info.system]^.alignment.recordalignmin,
  1065. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1066. { write iidstr }
  1067. if def.objecttype=odt_interfacecorba then
  1068. begin
  1069. { prepareguid always allocates an empty string }
  1070. if not assigned(def.iidstr) then
  1071. internalerror(2016021901);
  1072. tcb.emit_shortstring_const(def.iidstr^)
  1073. end;
  1074. { write published properties for this object }
  1075. published_properties_write_rtti_data(tcb,propnamelist,def.symtable);
  1076. tcb.end_anonymous_record;
  1077. tcb.end_anonymous_record;
  1078. propnamelist.free;
  1079. end;
  1080. begin
  1081. case def.objecttype of
  1082. odt_class:
  1083. tcb.emit_ord_const(tkclass,u8inttype);
  1084. odt_object:
  1085. tcb.emit_ord_const(tkobject,u8inttype);
  1086. odt_dispinterface,
  1087. odt_interfacecom:
  1088. tcb.emit_ord_const(tkInterface,u8inttype);
  1089. odt_interfacecorba:
  1090. tcb.emit_ord_const(tkinterfaceCorba,u8inttype);
  1091. odt_helper:
  1092. tcb.emit_ord_const(tkhelper,u8inttype);
  1093. else
  1094. internalerror(200611034);
  1095. end;
  1096. { generate the name }
  1097. tcb.emit_shortstring_const(def.objrealname^);
  1098. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1099. targetinfos[target_info.system]^.alignment.recordalignmin,
  1100. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1101. case rt of
  1102. initrtti :
  1103. begin
  1104. if def.objecttype in [odt_class,odt_object,odt_helper] then
  1105. objectdef_rtti_fields(def)
  1106. else
  1107. objectdef_rtti_interface_init(def);
  1108. end;
  1109. fullrtti :
  1110. begin
  1111. case def.objecttype of
  1112. odt_helper,
  1113. odt_class:
  1114. objectdef_rtti_class_full(def);
  1115. odt_object:
  1116. objectdef_rtti_fields(def);
  1117. else
  1118. objectdef_rtti_interface_full(def);
  1119. end;
  1120. end;
  1121. end;
  1122. tcb.end_anonymous_record;
  1123. end;
  1124. begin
  1125. case def.typ of
  1126. variantdef :
  1127. variantdef_rtti(tvariantdef(def));
  1128. stringdef :
  1129. stringdef_rtti(tstringdef(def));
  1130. enumdef :
  1131. enumdef_rtti(tenumdef(def));
  1132. orddef :
  1133. orddef_rtti(torddef(def));
  1134. floatdef :
  1135. floatdef_rtti(tfloatdef(def));
  1136. setdef :
  1137. setdef_rtti(tsetdef(def));
  1138. procvardef :
  1139. procvardef_rtti(tprocvardef(def));
  1140. arraydef :
  1141. begin
  1142. if ado_IsBitPacked in tarraydef(def).arrayoptions then
  1143. unknown_rtti(tstoreddef(def))
  1144. else
  1145. arraydef_rtti(tarraydef(def));
  1146. end;
  1147. recorddef :
  1148. begin
  1149. if trecorddef(def).is_packed then
  1150. unknown_rtti(tstoreddef(def))
  1151. else
  1152. recorddef_rtti(trecorddef(def));
  1153. end;
  1154. objectdef :
  1155. objectdef_rtti(tobjectdef(def));
  1156. classrefdef :
  1157. classrefdef_rtti(tclassrefdef(def));
  1158. pointerdef :
  1159. pointerdef_rtti(tpointerdef(def));
  1160. else
  1161. unknown_rtti(tstoreddef(def));
  1162. end;
  1163. end;
  1164. function enumsym_compare_name(item1, item2: pointer): Integer;
  1165. var
  1166. enum1: tenumsym absolute item1;
  1167. enum2: tenumsym absolute item2;
  1168. begin
  1169. if enum1=enum2 then
  1170. result:=0
  1171. else if enum1.name>enum2.name then
  1172. result:=1
  1173. else
  1174. { there can't be equal names, identifiers are unique }
  1175. result:=-1;
  1176. end;
  1177. function enumsym_compare_value(item1, item2: pointer): Integer;
  1178. var
  1179. enum1: tenumsym absolute item1;
  1180. enum2: tenumsym absolute item2;
  1181. begin
  1182. if enum1.value>enum2.value then
  1183. result:=1
  1184. else if enum1.value<enum2.value then
  1185. result:=-1
  1186. else
  1187. result:=0;
  1188. end;
  1189. procedure TRTTIWriter.write_rtti_extrasyms(def:Tdef;rt:Trttitype;mainrtti:Tasmsymbol);
  1190. type Penumsym = ^Tenumsym;
  1191. { Writes a helper table for accelerated conversion of ordinal enum values to strings.
  1192. If you change something in this method, make sure to adapt the corresponding code
  1193. in sstrings.inc. }
  1194. procedure enumdef_rtti_ord2stringindex(rttidef: trecorddef; const syms: tfplist);
  1195. var rttilab:Tasmsymbol;
  1196. h,i,o,prev_value:longint;
  1197. mode:(lookup,search); {Modify with care, ordinal value of enum is written.}
  1198. r:single; {Must be real type because of integer overflow risk.}
  1199. tcb: ttai_typedconstbuilder;
  1200. sym_count: integer;
  1201. tabledef: tdef;
  1202. begin
  1203. {Decide wether a lookup array is size efficient.}
  1204. mode:=lookup;
  1205. sym_count:=syms.count;
  1206. if sym_count>0 then
  1207. begin
  1208. i:=1;
  1209. r:=0;
  1210. h:=tenumsym(syms[0]).value; {Next expected enum value is min.}
  1211. { set prev_value for the first iteration to a value that is
  1212. different from the first one without risking overflow (it's used
  1213. to detect whether two enum values are the same) }
  1214. if h=0 then
  1215. prev_value:=1
  1216. else
  1217. prev_value:=0;
  1218. while i<sym_count do
  1219. begin
  1220. { if two enum values are the same, we have to create a table }
  1221. if (prev_value=h) then
  1222. begin
  1223. mode:=search;
  1224. break;
  1225. end;
  1226. {Calculate size of hole between values. Avoid integer overflows.}
  1227. r:=r+(single(tenumsym(syms[i]).value)-single(h))-1;
  1228. prev_value:=h;
  1229. h:=tenumsym(syms[i]).value;
  1230. inc(i);
  1231. end;
  1232. if r>sym_count then
  1233. mode:=search; {Don't waste more than 50% space.}
  1234. end;
  1235. { write rtti data; make sure that the alignment matches the corresponding data structure
  1236. in the code that uses it (if alignment is required). }
  1237. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable]);
  1238. { use TConstPtrUInt packrecords to ensure good alignment }
  1239. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1240. targetinfos[target_info.system]^.alignment.recordalignmin,
  1241. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1242. { now emit the data: first the mode }
  1243. tcb.emit_tai(Tai_const.create_32bit(longint(mode)),u32inttype);
  1244. { align }
  1245. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1246. targetinfos[target_info.system]^.alignment.recordalignmin,
  1247. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1248. if mode=lookup then
  1249. begin
  1250. o:=tenumsym(syms[0]).value; {Start with min value.}
  1251. for i:=0 to sym_count-1 do
  1252. begin
  1253. while o<tenumsym(syms[i]).value do
  1254. begin
  1255. tcb.emit_tai(Tai_const.create_nil_dataptr,ptruinttype);
  1256. inc(o);
  1257. end;
  1258. inc(o);
  1259. tcb.queue_init(voidpointertype);
  1260. tcb.queue_subscriptn_multiple_by_name(rttidef,
  1261. ['size_start_rec',
  1262. 'min_max_rec',
  1263. 'basetype_array_rec',
  1264. tsym(syms[i]).Name]
  1265. );
  1266. tcb.queue_emit_asmsym(mainrtti,rttidef);
  1267. end;
  1268. end
  1269. else
  1270. begin
  1271. tcb.emit_ord_const(sym_count,u32inttype);
  1272. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1273. targetinfos[target_info.system]^.alignment.recordalignmin,
  1274. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1275. for i:=0 to sym_count-1 do
  1276. begin
  1277. tcb.emit_ord_const(tenumsym(syms[i]).value,s32inttype);
  1278. tcb.queue_init(voidpointertype);
  1279. tcb.queue_subscriptn_multiple_by_name(rttidef,
  1280. ['size_start_rec',
  1281. 'min_max_rec',
  1282. 'basetype_array_rec',
  1283. tsym(syms[i]).Name]
  1284. );
  1285. tcb.queue_emit_asmsym(mainrtti,rttidef);
  1286. end;
  1287. tcb.end_anonymous_record;
  1288. end;
  1289. tcb.end_anonymous_record;
  1290. tabledef:=tcb.end_anonymous_record;
  1291. rttilab:=current_asmdata.DefineAsmSymbol(Tstoreddef(def).rtti_mangledname(rt)+'_o2s',AB_GLOBAL,AT_DATA_FORCEINDIRECT,tabledef);
  1292. current_asmdata.asmlists[al_rtti].concatlist(tcb.get_final_asmlist(
  1293. rttilab,tabledef,sec_rodata,
  1294. rttilab.name,sizeof(pint)));
  1295. tcb.free;
  1296. current_module.add_public_asmsym(rttilab);
  1297. end;
  1298. { Writes a helper table for accelerated conversion of string to ordinal enum values.
  1299. If you change something in this method, make sure to adapt the corresponding code
  1300. in sstrings.inc. }
  1301. procedure enumdef_rtti_string2ordindex(rttidef: trecorddef; const syms: tfplist);
  1302. var
  1303. tcb: ttai_typedconstbuilder;
  1304. rttilab: Tasmsymbol;
  1305. i:longint;
  1306. tabledef: tdef;
  1307. begin
  1308. { write rtti data }
  1309. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable]);
  1310. { begin of Tstring_to_ord }
  1311. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1312. targetinfos[target_info.system]^.alignment.recordalignmin,
  1313. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1314. tcb.emit_ord_const(syms.count,s32inttype);
  1315. { begin of "data" array in Tstring_to_ord }
  1316. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1317. targetinfos[target_info.system]^.alignment.recordalignmin,
  1318. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1319. for i:=0 to syms.count-1 do
  1320. begin
  1321. tcb.emit_ord_const(tenumsym(syms[i]).value,s32inttype);
  1322. { alignment of pointer value handled by enclosing record already }
  1323. tcb.queue_init(voidpointertype);
  1324. tcb.queue_subscriptn_multiple_by_name(rttidef,
  1325. ['size_start_rec',
  1326. 'min_max_rec',
  1327. 'basetype_array_rec',
  1328. tsym(syms[i]).Name]
  1329. );
  1330. tcb.queue_emit_asmsym(mainrtti,rttidef);
  1331. end;
  1332. tcb.end_anonymous_record;
  1333. tabledef:=tcb.end_anonymous_record;
  1334. rttilab:=current_asmdata.DefineAsmSymbol(Tstoreddef(def).rtti_mangledname(rt)+'_s2o',AB_GLOBAL,AT_DATA_FORCEINDIRECT,tabledef);
  1335. current_asmdata.asmlists[al_rtti].concatlist(tcb.get_final_asmlist(
  1336. rttilab,tabledef,sec_rodata,
  1337. rttilab.name,sizeof(pint)));
  1338. tcb.free;
  1339. current_module.add_public_asmsym(rttilab);
  1340. end;
  1341. procedure enumdef_rtti_extrasyms(def:Tenumdef);
  1342. var
  1343. t:Tenumsym;
  1344. syms:tfplist;
  1345. i:longint;
  1346. rttitypesym: ttypesym;
  1347. rttidef: trecorddef;
  1348. begin
  1349. { collect enumsyms belonging to this enum type (could be a subsection
  1350. in case of a subrange type) }
  1351. syms:=tfplist.create;
  1352. for i := 0 to def.symtable.SymList.Count - 1 do
  1353. begin
  1354. t:=tenumsym(def.symtable.SymList[i]);
  1355. if t.value<def.minval then
  1356. continue
  1357. else
  1358. if t.value>def.maxval then
  1359. break;
  1360. syms.add(t);
  1361. end;
  1362. { sort the syms by enum name }
  1363. syms.sort(@enumsym_compare_name);
  1364. rttitypesym:=try_search_current_module_type(internaltypeprefixName[itp_rttidef]+def.rtti_mangledname(fullrtti));
  1365. if not assigned(rttitypesym) or
  1366. (ttypesym(rttitypesym).typedef.typ<>recorddef) then
  1367. internalerror(2015071402);
  1368. rttidef:=trecorddef(ttypesym(rttitypesym).typedef);
  1369. enumdef_rtti_string2ordindex(rttidef,syms);
  1370. { sort the syms by enum value }
  1371. syms.sort(@enumsym_compare_value);
  1372. enumdef_rtti_ord2stringindex(rttidef,syms);
  1373. syms.free;
  1374. end;
  1375. begin
  1376. case def.typ of
  1377. enumdef:
  1378. if rt=fullrtti then
  1379. begin
  1380. enumdef_rtti_extrasyms(Tenumdef(def));
  1381. end;
  1382. end;
  1383. end;
  1384. procedure TRTTIWriter.write_child_rtti_data(def:tdef;rt:trttitype);
  1385. begin
  1386. case def.typ of
  1387. enumdef :
  1388. if assigned(tenumdef(def).basedef) then
  1389. write_rtti(tenumdef(def).basedef,rt);
  1390. setdef :
  1391. write_rtti(tsetdef(def).elementdef,rt);
  1392. arraydef :
  1393. begin
  1394. write_rtti(tarraydef(def).rangedef,rt);
  1395. write_rtti(tarraydef(def).elementdef,rt);
  1396. end;
  1397. recorddef :
  1398. fields_write_rtti(trecorddef(def).symtable,rt);
  1399. objectdef :
  1400. begin
  1401. if assigned(tobjectdef(def).childof) then
  1402. write_rtti(tobjectdef(def).childof,rt);
  1403. if (rt=initrtti) or (tobjectdef(def).objecttype=odt_object) then
  1404. fields_write_rtti(tobjectdef(def).symtable,rt)
  1405. else
  1406. published_write_rtti(tobjectdef(def).symtable,rt);
  1407. end;
  1408. classrefdef,
  1409. pointerdef:
  1410. if not is_objc_class_or_protocol(tabstractpointerdef(def).pointeddef) then
  1411. write_rtti(tabstractpointerdef(def).pointeddef,rt);
  1412. procvardef:
  1413. params_write_rtti(tabstractprocdef(def),rt);
  1414. end;
  1415. end;
  1416. procedure TRTTIWriter.write_rtti_reference(tcb: ttai_typedconstbuilder; def: tdef; rt: trttitype);
  1417. begin
  1418. { we don't care about the real type here, because
  1419. a) we don't index into these elements
  1420. b) we may not have the rtti type available at the point that we
  1421. are emitting this data, because of forward definitions etc
  1422. c) if the rtti is emitted in another unit, we won't have the type
  1423. available at all
  1424. For the cases where the type is emitted in the current unit and hence
  1425. the underlying system will detect and complain about symbol def
  1426. mismatches, type conversions will have to be inserted afterwards (like
  1427. in llvm/llvmtype)
  1428. }
  1429. if not assigned(def) or is_void(def) or ((rt<>initrtti) and is_objc_class_or_protocol(def)) then
  1430. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype)
  1431. else
  1432. tcb.emit_tai(Tai_const.Create_sym(ref_rtti(def,rt,true)),voidpointertype);
  1433. end;
  1434. function TRTTIWriter.ref_rtti(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  1435. var
  1436. s : TSymStr;
  1437. begin
  1438. s:=def.rtti_mangledname(rt);
  1439. result:=current_asmdata.RefAsmSymbol(s,AT_DATA,indirect);
  1440. if (cs_create_pic in current_settings.moduleswitches) and
  1441. assigned(current_procinfo) then
  1442. include(current_procinfo.flags,pi_needs_got);
  1443. if def.owner.moduleid<>current_module.moduleid then
  1444. current_module.add_extern_asmsym(s,AB_EXTERNAL,AT_DATA);
  1445. end;
  1446. procedure TRTTIWriter.write_rtti(def:tdef;rt:trttitype);
  1447. var
  1448. tcb: ttai_typedconstbuilder;
  1449. rttilab: tasmsymbol;
  1450. rttidef: tdef;
  1451. begin
  1452. { only write rtti of definitions from the current module }
  1453. if not findunitsymtable(def.owner).iscurrentunit then
  1454. exit;
  1455. { check if separate initrtti is actually needed }
  1456. if (rt=initrtti) and (not def.needs_separate_initrtti) then
  1457. rt:=fullrtti;
  1458. { prevent recursion }
  1459. if rttidefstate[rt] in def.defstates then
  1460. exit;
  1461. include(def.defstates,rttidefstate[rt]);
  1462. { write first all dependencies }
  1463. write_child_rtti_data(def,rt);
  1464. { write rtti data }
  1465. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable]);
  1466. tcb.begin_anonymous_record(
  1467. internaltypeprefixName[itp_rttidef]+tstoreddef(def).rtti_mangledname(rt),
  1468. defaultpacking,reqalign,
  1469. targetinfos[target_info.system]^.alignment.recordalignmin,
  1470. targetinfos[target_info.system]^.alignment.maxCrecordalign
  1471. );
  1472. write_rtti_data(tcb,def,rt);
  1473. rttidef:=tcb.end_anonymous_record;
  1474. rttilab:=current_asmdata.DefineAsmSymbol(tstoreddef(def).rtti_mangledname(rt),AB_GLOBAL,AT_DATA_FORCEINDIRECT,rttidef);
  1475. current_asmdata.AsmLists[al_rtti].concatList(
  1476. tcb.get_final_asmlist(rttilab,rttidef,sec_rodata,rttilab.name,sizeof(pint)));
  1477. tcb.free;
  1478. current_module.add_public_asmsym(rttilab);
  1479. { write additional data }
  1480. write_rtti_extrasyms(def,rt,rttilab);
  1481. end;
  1482. constructor TRTTIWriter.create;
  1483. begin
  1484. if tf_requires_proper_alignment in target_info.flags then
  1485. begin
  1486. reqalign:=sizeof(TConstPtrUInt);
  1487. defaultpacking:=C_alignment;
  1488. end
  1489. else
  1490. begin
  1491. reqalign:=1;
  1492. defaultpacking:=1;
  1493. end;
  1494. end;
  1495. function TRTTIWriter.get_rtti_label(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  1496. var
  1497. name : tsymstr;
  1498. begin
  1499. name:=def.rtti_mangledname(rt);
  1500. result:=current_asmdata.RefAsmSymbol(name,AT_DATA,indirect);
  1501. if (cs_create_pic in current_settings.moduleswitches) and
  1502. assigned(current_procinfo) then
  1503. include(current_procinfo.flags,pi_needs_got);
  1504. if assigned(current_module) and (findunitsymtable(def.owner).moduleid<>current_module.moduleid) then
  1505. current_module.add_extern_asmsym(name,AB_EXTERNAL,AT_DATA);
  1506. end;
  1507. function TRTTIWriter.get_rtti_label_ord2str(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  1508. var
  1509. name : tsymstr;
  1510. begin
  1511. name:=def.rtti_mangledname(rt)+'_o2s';
  1512. result:=current_asmdata.RefAsmSymbol(name,AT_DATA,indirect);
  1513. if (cs_create_pic in current_settings.moduleswitches) and
  1514. assigned(current_procinfo) then
  1515. include(current_procinfo.flags,pi_needs_got);
  1516. if assigned(current_module) and (findunitsymtable(def.owner).moduleid<>current_module.moduleid) then
  1517. current_module.add_extern_asmsym(name,AB_EXTERNAL,AT_DATA);
  1518. end;
  1519. function TRTTIWriter.get_rtti_label_str2ord(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  1520. var
  1521. name : tsymstr;
  1522. begin
  1523. name:=def.rtti_mangledname(rt)+'_s2o';
  1524. result:=current_asmdata.RefAsmSymbol(name,AT_DATA,indirect);
  1525. if (cs_create_pic in current_settings.moduleswitches) and
  1526. assigned(current_procinfo) then
  1527. include(current_procinfo.flags,pi_needs_got);
  1528. if assigned(current_module) and (findunitsymtable(def.owner).moduleid<>current_module.moduleid) then
  1529. current_module.add_extern_asmsym(name,AB_EXTERNAL,AT_DATA);
  1530. end;
  1531. end.