ncgrtti.pas 62 KB

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