ncgrtti.pas 66 KB

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