ncgrtti.pas 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602
  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,globtype,
  22. aasmbase,aasmcnst,
  23. symbase,symconst,symtype,symdef,symsym,
  24. parabase;
  25. type
  26. { TRTTIWriter }
  27. TRTTIWriter=class
  28. private
  29. { required internal alignment of the rtti data }
  30. reqalign: shortint;
  31. { required packing of all structures except for ttypeinfo and tpropinfo,
  32. which always use packrecords 1 }
  33. defaultpacking: shortint;
  34. { write comments ? }
  35. addcomments : boolean;
  36. procedure fields_write_rtti(st:tsymtable;rt:trttitype);
  37. procedure params_write_rtti(def:tabstractprocdef;rt:trttitype;allow_hidden:boolean);
  38. procedure fields_write_rtti_data(tcb: ttai_typedconstbuilder; def: tabstractrecorddef; rt: trttitype);
  39. procedure methods_write_rtti(st:tsymtable;rt:trttitype;visibilities:tvisibilities;allow_hidden:boolean);
  40. procedure write_rtti_extrasyms(def:Tdef;rt:Trttitype;mainrtti:Tasmsymbol);
  41. procedure published_write_rtti(st:tsymtable;rt:trttitype);
  42. procedure properties_write_rtti_data(tcb:ttai_typedconstbuilder;propnamelist:TFPHashObjectList;st:tsymtable;extended_rtti:boolean;visibilities:tvisibilities);
  43. procedure collect_propnamelist(propnamelist:TFPHashObjectList;def:tabstractrecorddef;visibilities:tvisibilities);
  44. { only use a direct reference if the referenced type can *only* reside
  45. in the same unit as the current one }
  46. function ref_rtti(def:tdef;rt:trttitype;indirect:boolean;suffix:tsymstr):tasmsymbol;
  47. procedure write_rtti_name(tcb: ttai_typedconstbuilder; def: tdef);
  48. procedure write_common_rtti_data(tcb:ttai_typedconstbuilder;def:tdef;rt:trttitype);
  49. procedure write_rtti_data(tcb: ttai_typedconstbuilder; def:tdef; rt: trttitype);
  50. procedure write_attribute_data(tcb: ttai_typedconstbuilder;attr_list:trtti_attribute_list);
  51. procedure write_child_rtti_data(def:tdef;rt:trttitype);
  52. procedure write_rtti_reference(tcb: ttai_typedconstbuilder; def: tdef; rt: trttitype);
  53. procedure write_methods(tcb:ttai_typedconstbuilder;st:tsymtable;extended_rtti:boolean;visibilities:tvisibilities);
  54. procedure write_header(tcb: ttai_typedconstbuilder; def: tdef; typekind: byte);
  55. function write_methodkind(tcb:ttai_typedconstbuilder;def:tabstractprocdef):byte;
  56. procedure write_callconv(tcb:ttai_typedconstbuilder;def:tabstractprocdef);
  57. procedure write_paralocs(tcb:ttai_typedconstbuilder;para:pcgpara);
  58. procedure write_param_flag(tcb:ttai_typedconstbuilder;parasym:tparavarsym);
  59. procedure write_mop_offset_table(tcb:ttai_typedconstbuilder;def:tabstractrecorddef;mop:tmanagementoperator);
  60. procedure maybe_add_comment(tcb:ttai_typedconstbuilder;const comment : string); inline;
  61. public
  62. constructor create;
  63. procedure write_rtti(def:tdef;rt:trttitype);
  64. procedure write_extended_method_table(tcb:ttai_typedconstbuilder;def:tabstractrecorddef;packrecords:longint);
  65. procedure write_extended_field_table(tcb:ttai_typedconstbuilder;def:tabstractrecorddef;packrecords:longint);
  66. function get_rtti_label(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol; inline;
  67. function get_rtti_label_ord2str(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol; inline;
  68. function get_rtti_label_str2ord(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol; inline;
  69. end;
  70. { generate RTTI and init tables }
  71. procedure write_persistent_type_info(st:tsymtable;is_global:boolean);
  72. var
  73. RTTIWriter : TRTTIWriter;
  74. implementation
  75. uses
  76. cutils,
  77. globals,verbose,systems,
  78. node,ncal,ncon,
  79. fmodule, procinfo,
  80. symtable,symutil,
  81. aasmtai,aasmdata,
  82. defutil,
  83. paramgr
  84. ;
  85. const
  86. rttidefstate : array[trttitype] of tdefstate =
  87. (ds_rtti_table_written,ds_init_table_written,
  88. { Objective-C related, does not pass here }
  89. symconst.ds_none,symconst.ds_none,
  90. symconst.ds_none,symconst.ds_none);
  91. rttitypenames : array[trttitype] of string =
  92. ('full','init',
  93. 'objcmeta','objcmetaro',
  94. 'objcclass','objcclassro');
  95. type
  96. TPropNameListItem = class(TFPHashObject)
  97. propindex : longint;
  98. propowner : TSymtable;
  99. end;
  100. function visibility_to_rtti_flags(vis: tvisibility): byte;
  101. begin
  102. case vis of
  103. vis_private:
  104. result:=byte(rv_private);
  105. vis_strictprivate:
  106. begin
  107. result:=byte(rv_private);
  108. // add bit to indicate "strict"
  109. result:=result or (1 shl 2);
  110. end;
  111. vis_protected:
  112. result:=byte(rv_protected);
  113. vis_strictprotected:
  114. begin
  115. result:=byte(rv_protected);
  116. // add bit to indicate "strict"
  117. result:=result or (1 shl 2);
  118. end;
  119. vis_public:
  120. result:=byte(rv_public);
  121. vis_published:
  122. result:=byte(rv_published);
  123. otherwise
  124. internalerror(2021061301);
  125. end;
  126. end;
  127. procedure write_persistent_type_info(st: tsymtable; is_global: boolean);
  128. var
  129. i : longint;
  130. def : tdef;
  131. begin
  132. { no Delphi-style RTTI for managed platforms }
  133. if target_info.system in systems_managed_vm then
  134. exit;
  135. for i:=0 to st.DefList.Count-1 do
  136. begin
  137. def:=tdef(st.DefList[i]);
  138. { skip generics }
  139. if [df_generic,df_genconstraint]*def.defoptions<>[] then
  140. continue;
  141. case def.typ of
  142. recorddef:
  143. write_persistent_type_info(trecorddef(def).symtable,is_global);
  144. objectdef :
  145. begin
  146. { Skip forward defs }
  147. if (oo_is_forward in tobjectdef(def).objectoptions) then
  148. continue;
  149. { skip unique type aliases, they use the RTTI from the parent class }
  150. if tobjectdef(def).is_unique_objpasdef then
  151. continue;
  152. write_persistent_type_info(tobjectdef(def).symtable,is_global);
  153. end;
  154. procdef :
  155. begin
  156. if assigned(tprocdef(def).localst) and
  157. (tprocdef(def).localst.symtabletype=localsymtable) then
  158. write_persistent_type_info(tprocdef(def).localst,false);
  159. if assigned(tprocdef(def).parast) then
  160. write_persistent_type_info(tprocdef(def).parast,false);
  161. end;
  162. errordef:
  163. { we shouldn't have come this far if we have an errordef somewhere }
  164. internalerror(2017010701);
  165. undefineddef:
  166. { don't write any RTTI for these }
  167. continue;
  168. else
  169. ;
  170. end;
  171. { always generate persistent tables for types in the interface so
  172. they can be reused in other units and give always the same pointer
  173. location. }
  174. { Init }
  175. if (
  176. assigned(def.typesym) and
  177. is_global and
  178. not is_objc_class_or_protocol(def)
  179. ) or
  180. is_managed_type(def) or
  181. (ds_init_table_used in def.defstates) then
  182. RTTIWriter.write_rtti(def,initrtti);
  183. { RTTI }
  184. if not(df_internal in def.defoptions) and
  185. (
  186. (
  187. assigned(def.typesym) and
  188. is_global and
  189. not is_objc_class_or_protocol(def)
  190. ) or
  191. (ds_rtti_table_used in def.defstates)
  192. ) then
  193. RTTIWriter.write_rtti(def,fullrtti);
  194. end;
  195. end;
  196. {***************************************************************************
  197. TRTTIWriter
  198. ***************************************************************************}
  199. function TRTTIWriter.get_rtti_label(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  200. begin
  201. result:=ref_rtti(def,rt,indirect,'');
  202. end;
  203. function TRTTIWriter.get_rtti_label_ord2str(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  204. begin
  205. result:=ref_rtti(def,rt,indirect,'_o2s');
  206. end;
  207. function TRTTIWriter.get_rtti_label_str2ord(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  208. begin
  209. result:=ref_rtti(def,rt,indirect,'_s2o');
  210. end;
  211. procedure TRTTIWriter.write_methods(tcb:ttai_typedconstbuilder;st:tsymtable;extended_rtti:boolean;visibilities:tvisibilities);
  212. var
  213. rtticount,
  214. totalcount,
  215. i,j,k : longint;
  216. vmt_index : integer;
  217. sym : tprocsym;
  218. def : tprocdef;
  219. para : tparavarsym;
  220. begin
  221. maybe_add_comment(tcb,'RTTI: begin methods');
  222. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,SizeOf(PInt)),
  223. targetinfos[target_info.system]^.alignment.recordalignmin);
  224. totalcount:=0;
  225. rtticount:=0;
  226. for i:=0 to st.symlist.count-1 do
  227. if tsym(st.symlist[i]).typ=procsym then
  228. begin
  229. sym:=tprocsym(st.symlist[i]);
  230. inc(totalcount,sym.procdeflist.count);
  231. for j:=0 to sym.procdeflist.count-1 do
  232. if tprocdef(sym.procdeflist[j]).visibility in visibilities then
  233. inc(rtticount);
  234. end;
  235. { write the count section for non-extended methods }
  236. if not extended_rtti then
  237. begin
  238. maybe_add_comment(tcb,#9'count');
  239. tcb.emit_ord_const(totalcount,u16inttype);
  240. maybe_add_comment(tcb,#9'RTTI count');
  241. if rtticount=0 then
  242. tcb.emit_ord_const($FFFF,u16inttype)
  243. else
  244. tcb.emit_ord_const(rtticount,u16inttype);
  245. end;
  246. if rtticount>0 then
  247. for i:=0 to st.symlist.count-1 do
  248. if tsym(st.symlist[i]).typ=procsym then
  249. begin
  250. sym:=tprocsym(st.symlist[i]);
  251. for j:=0 to sym.procdeflist.count-1 do
  252. begin
  253. def:=tprocdef(sym.procdeflist[j]);
  254. if not (def.visibility in visibilities) then
  255. continue;
  256. def.init_paraloc_info(callerside);
  257. maybe_add_comment(tcb,'RTTI: begin method '+def.fullprocname(false));
  258. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,SizeOf(PInt)),
  259. targetinfos[target_info.system]^.alignment.recordalignmin);
  260. maybe_add_comment(tcb,#9'return type');
  261. write_rtti_reference(tcb,def.returndef,fullrtti);
  262. maybe_add_comment(tcb,#9'calling convention');
  263. write_callconv(tcb,def);
  264. maybe_add_comment(tcb,#9'method kind');
  265. write_methodkind(tcb,def);
  266. maybe_add_comment(tcb,#9'param count');
  267. tcb.emit_ord_const(def.paras.count,u16inttype);
  268. maybe_add_comment(tcb,#9'caller args size');
  269. tcb.emit_ord_const(def.callerargareasize,ptrsinttype);
  270. maybe_add_comment(tcb,#9'name');
  271. tcb.emit_pooled_shortstring_const_ref(sym.realname);
  272. if extended_rtti then
  273. begin
  274. { write visibility section for extended RTTI }
  275. maybe_add_comment(tcb,#9'visibility');
  276. tcb.emit_ord_const(visibility_to_rtti_flags(def.visibility),u8inttype);
  277. { for classes write a VMT index }
  278. if st.defowner.typ=objectdef then
  279. begin
  280. vmt_index:=-1;
  281. if po_virtualmethod in def.procoptions then
  282. for k:=0 to tobjectdef(st.defowner).vmtentries.count-1 do
  283. if pvmtentry(tobjectdef(st.defowner).vmtentries[k])^.procdef=def then
  284. begin
  285. vmt_index:=k;
  286. break;
  287. end;
  288. maybe_add_comment(tcb,#9'VMT index');
  289. tcb.emit_ord_const(vmt_index,s16inttype);
  290. end;
  291. end;
  292. for k:=0 to def.paras.count-1 do
  293. begin
  294. para:=tparavarsym(def.paras[k]);
  295. maybe_add_comment(tcb,'RTTI: begin param '+para.prettyname);
  296. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,SizeOf(PInt)),
  297. targetinfos[target_info.system]^.alignment.recordalignmin);
  298. maybe_add_comment(tcb,#9'type');
  299. if is_open_array(para.vardef) or is_array_of_const(para.vardef) then
  300. write_rtti_reference(tcb,tarraydef(para.vardef).elementdef,fullrtti)
  301. else if para.vardef=cformaltype then
  302. write_rtti_reference(tcb,nil,fullrtti)
  303. else
  304. write_rtti_reference(tcb,para.vardef,fullrtti);
  305. maybe_add_comment(tcb,#9'flags');
  306. write_param_flag(tcb,para);
  307. maybe_add_comment(tcb,#9'name');
  308. tcb.emit_pooled_shortstring_const_ref(para.realname);
  309. maybe_add_comment(tcb,#9'locs');
  310. write_paralocs(tcb,@para.paraloc[callerside]);
  311. tcb.end_anonymous_record;
  312. maybe_add_comment(tcb,'RTTI: end param '+para.prettyname);
  313. end;
  314. if not is_void(def.returndef) then
  315. begin
  316. if addcomments then
  317. tcb.emit_comment(#9'return loc');
  318. write_paralocs(tcb,@def.funcretloc[callerside]);
  319. end;
  320. tcb.end_anonymous_record;
  321. maybe_add_comment(tcb,'RTTI: end method '+def.fullprocname(false));
  322. end;
  323. end;
  324. tcb.end_anonymous_record;
  325. maybe_add_comment(tcb,'RTTI: end methods');
  326. end;
  327. procedure TRTTIWriter.write_header(tcb: ttai_typedconstbuilder; def: tdef; typekind: byte);
  328. var
  329. name: shortstring;
  330. begin
  331. if assigned(def.typesym) then
  332. name:=ttypesym(def.typesym).realname
  333. else
  334. name:='';
  335. { TTypeInfo, always packed and doesn't need alignment }
  336. tcb.begin_anonymous_record(
  337. internaltypeprefixName[itp_rtti_header]+tostr(length(name)),1,1,
  338. targetinfos[target_info.system]^.alignment.recordalignmin);
  339. if def.typ=arraydef then
  340. InternalError(201012211);
  341. tcb.emit_tai(Tai_const.Create_8bit(typekind),u8inttype);
  342. tcb.emit_shortstring_const(name);
  343. tcb.end_anonymous_record;
  344. end;
  345. function TRTTIWriter.write_methodkind(tcb:ttai_typedconstbuilder;def:tabstractprocdef):byte;
  346. begin
  347. case def.proctypeoption of
  348. potype_constructor: result:=mkConstructor;
  349. potype_destructor: result:=mkDestructor;
  350. potype_class_constructor: result:=mkClassConstructor;
  351. potype_class_destructor: result:=mkClassDestructor;
  352. potype_operator: result:=mkOperatorOverload;
  353. potype_procedure:
  354. if po_classmethod in def.procoptions then
  355. result:=mkClassProcedure
  356. else
  357. result:=mkProcedure;
  358. potype_function:
  359. if po_classmethod in def.procoptions then
  360. result:=mkClassFunction
  361. else
  362. result:=mkFunction;
  363. else
  364. begin
  365. if def.returndef = voidtype then
  366. result:=mkProcedure
  367. else
  368. result:=mkFunction;
  369. end;
  370. end;
  371. tcb.emit_ord_const(result,u8inttype);
  372. end;
  373. procedure TRTTIWriter.write_callconv(tcb:ttai_typedconstbuilder;def:tabstractprocdef);
  374. const
  375. ProcCallOptionToCallConv: array[tproccalloption] of byte = (
  376. { pocall_none } 0,
  377. { pocall_cdecl } 1,
  378. { pocall_cppdecl } 5,
  379. { pocall_far16 } 6,
  380. { pocall_oldfpccall } 7,
  381. { pocall_internproc } 8,
  382. { pocall_syscall } 9,
  383. { pocall_pascal } 2,
  384. { pocall_register } 0,
  385. { pocall_safecall } 4,
  386. { pocall_stdcall } 3,
  387. { pocall_softfloat } 10,
  388. { pocall_mwpascal } 11,
  389. { pocall_interrupt } 12,
  390. { pocall_hardfloat } 13,
  391. { pocall_sysv_abi_default } 14,
  392. { pocall_sysv_abi_cdecl } 15,
  393. { pocall_ms_abi_default } 16,
  394. { pocall_ms_abi_cdecl } 17,
  395. { pocall_vectorcall } 18
  396. );
  397. begin
  398. tcb.emit_ord_const(ProcCallOptionToCallConv[def.proccalloption],u8inttype);
  399. end;
  400. procedure TRTTIWriter.write_paralocs(tcb:ttai_typedconstbuilder;para:pcgpara);
  401. var
  402. locs : trttiparalocs;
  403. i : longint;
  404. pool : THashSet;
  405. entry : PHashSetItem;
  406. loclab : TAsmLabel;
  407. loctcb : ttai_typedconstbuilder;
  408. datadef : tdef;
  409. begin
  410. locs:=paramanager.cgparalocs_to_rttiparalocs(para^.location);
  411. if length(locs)>high(byte) then
  412. internalerror(2017010601);
  413. if length(locs)=0 then
  414. begin
  415. { *shrugs* }
  416. tcb.emit_tai(Tai_const.Create_nil_codeptr,voidpointertype);
  417. exit;
  418. end;
  419. { do we have such a paraloc already in the pool? }
  420. pool:=current_asmdata.ConstPools[sp_paraloc];
  421. entry:=pool.FindOrAdd(@locs[0],length(locs)*sizeof(trttiparaloc));
  422. if not assigned(entry^.Data) then
  423. begin
  424. current_asmdata.getglobaldatalabel(loclab);
  425. loctcb:=ctai_typedconstbuilder.create([tcalo_is_lab,tcalo_make_dead_strippable]);
  426. loctcb.begin_anonymous_record('',defaultpacking,min(reqalign,SizeOf(PInt)),
  427. targetinfos[target_info.system]^.alignment.recordalignmin);
  428. loctcb.emit_ord_const(length(locs),u8inttype);
  429. for i:=low(locs) to high(locs) do
  430. begin
  431. loctcb.begin_anonymous_record('',defaultpacking,min(reqalign,SizeOf(PInt)),
  432. targetinfos[target_info.system]^.alignment.recordalignmin);
  433. loctcb.emit_ord_const(locs[i].loctype,u8inttype);
  434. loctcb.emit_ord_const(locs[i].regsub,u8inttype);
  435. loctcb.emit_ord_const(locs[i].regindex,u16inttype);
  436. { the corresponding type for aint is alusinttype }
  437. loctcb.emit_ord_const(locs[i].offset,alusinttype);
  438. loctcb.end_anonymous_record;
  439. end;
  440. datadef:=loctcb.end_anonymous_record;
  441. current_asmdata.asmlists[al_typedconsts].concatList(
  442. loctcb.get_final_asmlist(loclab,datadef,sec_rodata_norel,loclab.name,const_align(sizeof(pint)))
  443. );
  444. loctcb.free;
  445. entry^.data:=loclab;
  446. end
  447. else
  448. loclab:=TAsmLabel(entry^.Data);
  449. tcb.emit_tai(Tai_const.Create_sym(loclab),voidpointertype);
  450. end;
  451. procedure TRTTIWriter.write_param_flag(tcb:ttai_typedconstbuilder;parasym:tparavarsym);
  452. var
  453. paraspec : word;
  454. begin
  455. case parasym.varspez of
  456. vs_value : paraspec := 0;
  457. vs_const : paraspec := pfConst;
  458. vs_var : paraspec := pfVar;
  459. vs_out : paraspec := pfOut;
  460. vs_constref: paraspec := pfConstRef;
  461. else
  462. internalerror(2013112904);
  463. end;
  464. { Kylix also seems to always add both pfArray and pfReference
  465. in this case
  466. }
  467. if is_open_array(parasym.vardef) or is_array_of_const(parasym.vardef) then
  468. paraspec:=paraspec or pfArray or pfReference;
  469. { and these for classes and interfaces (maybe because they
  470. are themselves addresses?)
  471. }
  472. if is_class_or_interface(parasym.vardef) then
  473. paraspec:=paraspec or pfAddress;
  474. { flags for the hidden parameters }
  475. if vo_is_hidden_para in parasym.varoptions then
  476. paraspec:=paraspec or pfHidden;
  477. if vo_is_high_para in parasym.varoptions then
  478. paraspec:=paraspec or pfHigh;
  479. if vo_is_self in parasym.varoptions then
  480. paraspec:=paraspec or pfSelf;
  481. if vo_is_vmt in parasym.varoptions then
  482. paraspec:=paraspec or pfVmt;
  483. if vo_is_funcret in parasym.varoptions then
  484. paraspec:=paraspec or pfResult;
  485. { set bits run from the highest to the lowest bit on
  486. big endian systems
  487. }
  488. if (target_info.endian = endian_big) then
  489. paraspec:=reverse_word(paraspec);
  490. { write flags for current parameter }
  491. tcb.emit_ord_const(paraspec,u16inttype);
  492. end;
  493. function compare_mop_offset_entry(item1,item2:pointer):longint;
  494. var
  495. entry1: pmanagementoperator_offset_entry absolute item1;
  496. entry2: pmanagementoperator_offset_entry absolute item2;
  497. begin
  498. if entry1^.offset<entry2^.offset then
  499. result:=-1
  500. else if entry1^.offset>entry2^.offset then
  501. result:=1
  502. else
  503. result:=0;
  504. end;
  505. procedure TRTTIWriter.write_mop_offset_table(tcb:ttai_typedconstbuilder;def:tabstractrecorddef;mop:tmanagementoperator);
  506. var
  507. list : tfplist;
  508. datatcb : ttai_typedconstbuilder;
  509. tbllbl : TAsmLabel;
  510. entry : pmanagementoperator_offset_entry;
  511. datadef,entrydef : tdef;
  512. i : longint;
  513. pdef : tobjectdef;
  514. begin
  515. list:=tfplist.create;
  516. tabstractrecordsymtable(def.symtable).get_managementoperator_offset_list(mop,list);
  517. if (def.typ=objectdef) then
  518. begin
  519. pdef:=tobjectdef(def).childof;
  520. while assigned(pdef) do
  521. begin
  522. tabstractrecordsymtable(pdef.symtable).get_managementoperator_offset_list(mop,list);
  523. pdef:=pdef.childof;
  524. end;
  525. list.sort(@compare_mop_offset_entry);
  526. end;
  527. if list.count=0 then
  528. tcb.emit_tai(tai_const.create_nil_dataptr,voidpointertype)
  529. else
  530. begin
  531. tcb.start_internal_data_builder(current_asmdata.AsmLists[al_rtti],sec_rodata,'',datatcb,tbllbl);
  532. datatcb.begin_anonymous_record('',defaultpacking,min(reqalign,SizeOf(PInt)),
  533. targetinfos[target_info.system]^.alignment.recordalignmin);
  534. datatcb.emit_ord_const(list.count,u32inttype);
  535. entrydef:=get_recorddef(itp_init_mop_offset_entry,[voidcodepointertype,sizeuinttype],defaultpacking);
  536. for i:=0 to list.count-1 do
  537. begin
  538. entry:=pmanagementoperator_offset_entry(list[i]);
  539. datatcb.maybe_begin_aggregate(entrydef);
  540. datatcb.queue_init(voidcodepointertype);
  541. datatcb.queue_emit_proc(entry^.pd);
  542. datatcb.queue_init(sizeuinttype);
  543. datatcb.queue_emit_ordconst(entry^.offset,sizeuinttype);
  544. datatcb.maybe_end_aggregate(entrydef);
  545. dispose(entry);
  546. end;
  547. datadef:=datatcb.end_anonymous_record;
  548. tcb.finish_internal_data_builder(datatcb,tbllbl,datadef,sizeof(pint));
  549. tcb.emit_tai(tai_const.Create_sym(tbllbl),voidpointertype);
  550. end;
  551. list.free;
  552. end;
  553. procedure TRTTIWriter.write_rtti_name(tcb: ttai_typedconstbuilder; def: tdef);
  554. begin
  555. if is_open_array(def) then
  556. { open arrays never have a typesym with a name, since you cannot
  557. define an "open array type". Kylix prints the type of the
  558. elements in the array in this case (so together with the pfArray
  559. flag, you can reconstruct the full typename, I assume (JM))
  560. }
  561. def:=tarraydef(def).elementdef;
  562. { name }
  563. if assigned(def.typesym) then
  564. tcb.emit_shortstring_const(ttypesym(def.typesym).realname)
  565. else
  566. tcb.emit_shortstring_const('');
  567. end;
  568. procedure TRTTIWriter.write_common_rtti_data(tcb:ttai_typedconstbuilder;def:tdef;rt:trttitype);
  569. begin
  570. if rt<>fullrtti then
  571. write_attribute_data(tcb,nil)
  572. else
  573. write_attribute_data(tcb,tstoreddef(def).rtti_attribute_list);
  574. end;
  575. { writes a 32-bit count followed by array of field infos for given symtable }
  576. procedure TRTTIWriter.fields_write_rtti_data(tcb: ttai_typedconstbuilder; def: tabstractrecorddef; rt: trttitype);
  577. var
  578. i : longint;
  579. sym : tsym;
  580. fieldcnt: longint;
  581. st: tsymtable;
  582. fields: tfplist;
  583. parentrtti: boolean;
  584. begin
  585. fieldcnt:=0;
  586. parentrtti:=false;
  587. st:=def.symtable;
  588. fields:=tfplist.create;
  589. fields.capacity:=st.symlist.count+1;
  590. { For objects, treat parent (if any) as a field with offset 0. This
  591. provides correct handling of entire instance with RTL rtti routines. }
  592. if (def.typ=objectdef) and (tobjectdef(def).objecttype=odt_object) and
  593. Assigned(tobjectdef(def).childof) and
  594. ((rt=fullrtti) or (tobjectdef(def).childof.needs_inittable)) then
  595. begin
  596. parentrtti:=true;
  597. inc(fieldcnt);
  598. end;
  599. for i:=0 to st.SymList.Count-1 do
  600. begin
  601. sym:=tsym(st.SymList[i]);
  602. if is_normal_fieldvarsym(sym) and
  603. (
  604. (rt=fullrtti) or
  605. tfieldvarsym(sym).vardef.needs_inittable
  606. ) and
  607. not is_objc_class_or_protocol(tfieldvarsym(sym).vardef) then
  608. begin
  609. fields.add(tfieldvarsym(sym));
  610. inc(fieldcnt);
  611. end;
  612. end;
  613. { insert field count before data }
  614. tcb.emit_ord_const(fieldcnt,u32inttype);
  615. { parent object? }
  616. if parentrtti then
  617. begin
  618. write_rtti_reference(tcb,tobjectdef(def).childof,rt);
  619. tcb.emit_ord_const(0,ptruinttype);
  620. end;
  621. { fields }
  622. for i:=0 to fields.count-1 do
  623. begin
  624. sym:=tsym(fields[i]);
  625. maybe_add_comment(tcb,'RTTI begin field '+tostr(i)+': '+sym.prettyname);
  626. write_rtti_reference(tcb,tfieldvarsym(sym).vardef,rt);
  627. tcb.emit_ord_const(tfieldvarsym(sym).fieldoffset,sizeuinttype);
  628. maybe_add_comment(tcb,'RTTI end field '+tostr(i)+': '+sym.prettyname);
  629. end;
  630. fields.free;
  631. end;
  632. procedure TRTTIWriter.fields_write_rtti(st:tsymtable;rt:trttitype);
  633. var
  634. i : longint;
  635. sym : tsym;
  636. begin
  637. for i:=0 to st.SymList.Count-1 do
  638. begin
  639. sym:=tsym(st.SymList[i]);
  640. if is_normal_fieldvarsym(sym) and
  641. (
  642. (rt=fullrtti) or
  643. tfieldvarsym(sym).vardef.needs_inittable
  644. ) then
  645. write_rtti(tfieldvarsym(sym).vardef,rt);
  646. end;
  647. end;
  648. procedure TRTTIWriter.params_write_rtti(def:tabstractprocdef;rt:trttitype;allow_hidden:boolean);
  649. var
  650. i : longint;
  651. sym : tparavarsym;
  652. begin
  653. for i:=0 to def.paras.count-1 do
  654. begin
  655. sym:=tparavarsym(def.paras[i]);
  656. if not (vo_is_hidden_para in sym.varoptions) or allow_hidden then
  657. begin
  658. if is_open_array(sym.vardef) or is_array_of_const(sym.vardef) then
  659. write_rtti(tarraydef(sym.vardef).elementdef,rt)
  660. else
  661. write_rtti(sym.vardef,rt);
  662. end;
  663. end;
  664. end;
  665. procedure TRTTIWriter.methods_write_rtti(st:tsymtable;rt:trttitype;visibilities:tvisibilities;allow_hidden:boolean);
  666. var
  667. i,j : longint;
  668. sym : tprocsym;
  669. def : tabstractprocdef;
  670. begin
  671. for i:=0 to st.symlist.count-1 do
  672. if tsym(st.symlist[i]).typ=procsym then
  673. begin
  674. sym:=tprocsym(st.symlist[i]);
  675. for j:=0 to sym.procdeflist.count-1 do
  676. begin
  677. def:=tabstractprocdef(sym.procdeflist[j]);
  678. write_rtti(def.returndef,rt);
  679. params_write_rtti(def,rt,allow_hidden);
  680. end;
  681. end;
  682. end;
  683. procedure TRTTIWriter.write_extended_method_table(tcb:ttai_typedconstbuilder;def:tabstractrecorddef;packrecords:longint);
  684. var
  685. methodcount,
  686. i, j: longint;
  687. sym: tprocsym;
  688. begin
  689. { count methods }
  690. methodcount:=0;
  691. for i:=0 to def.symtable.symlist.count-1 do
  692. if tsym(def.symtable.symlist[i]).typ=procsym then
  693. begin
  694. sym:=tprocsym(def.symtable.symlist[i]);
  695. for j:=0 to sym.procdeflist.count-1 do
  696. if def.is_visible_for_rtti(ro_methods,tprocdef(sym.procdeflist[j]).visibility) then
  697. inc(methodcount);
  698. end;
  699. tcb.begin_anonymous_record('',packrecords,min(reqalign,SizeOf(PInt)),
  700. targetinfos[target_info.system]^.alignment.recordalignmin);
  701. { emit method count }
  702. tcb.emit_ord_const(methodcount,u16inttype);
  703. { emit method entries (array) }
  704. if methodcount>0 then
  705. write_methods(tcb,def.symtable,true,def.rtti_visibilities_for_option(ro_methods));
  706. tcb.end_anonymous_record;
  707. end;
  708. procedure TRTTIWriter.write_extended_field_table(tcb:ttai_typedconstbuilder;def:tabstractrecorddef;packrecords:longint);
  709. var
  710. i: integer;
  711. sym: tsym;
  712. list: TFPList;
  713. begin
  714. list:=TFPList.Create;
  715. { build list of visible fields }
  716. for i:=0 to def.symtable.symlist.Count-1 do
  717. begin
  718. sym:=tsym(def.symtable.symlist[i]);
  719. if (sym.typ=fieldvarsym) and
  720. not(sp_static in sym.symoptions) and
  721. def.is_visible_for_rtti(ro_fields, sym.visibility) then
  722. list.add(sym);
  723. end;
  724. {
  725. TExtendedFieldTable = record
  726. FieldCount: Word;
  727. Fields: array[0..0] of TExtendedFieldInfo;
  728. end;
  729. }
  730. tcb.begin_anonymous_record(internaltypeprefixName[itp_extended_rtti_table]+tostr(list.count),packrecords,min(reqalign,SizeOf(PInt)),targetinfos[target_info.system]^.alignment.recordalignmin);
  731. tcb.emit_ord_const(list.count,u16inttype);
  732. for i := 0 to list.count-1 do
  733. begin
  734. sym:=tsym(list[i]);
  735. {
  736. TExtendedFieldInfo = record
  737. FieldOffset: SizeUInt;
  738. FieldType: Pointer;
  739. FieldVisibility: Byte;
  740. Name: PShortString;
  741. end;
  742. }
  743. tcb.begin_anonymous_record(internaltypeprefixName[itp_extended_rtti_field]+tostr(tfieldvarsym(sym).fieldoffset),packrecords,min(reqalign,SizeOf(PInt)),targetinfos[target_info.system]^.alignment.recordalignmin);
  744. { FieldOffset }
  745. tcb.emit_tai(Tai_const.Create_sizeint(tfieldvarsym(sym).fieldoffset),sizeuinttype);
  746. { FieldType: PPTypeInfo }
  747. tcb.emit_tai(Tai_const.Create_sym(RTTIWriter.get_rtti_label(tfieldvarsym(sym).vardef,fullrtti,true)),voidpointertype);
  748. { FieldVisibility }
  749. tcb.emit_ord_const(visibility_to_rtti_flags(tfieldvarsym(sym).visibility),u8inttype);
  750. { Name }
  751. tcb.emit_pooled_shortstring_const_ref(sym.realname);
  752. tcb.end_anonymous_record;
  753. end;
  754. tcb.end_anonymous_record;
  755. list.free;
  756. end;
  757. procedure TRTTIWriter.published_write_rtti(st:tsymtable;rt:trttitype);
  758. var
  759. i : longint;
  760. sym : tsym;
  761. begin
  762. for i:=0 to st.SymList.Count-1 do
  763. begin
  764. sym:=tsym(st.SymList[i]);
  765. if (sym.visibility=vis_published) then
  766. begin
  767. case tsym(sym).typ of
  768. propertysym:
  769. write_rtti(tpropertysym(sym).propdef,rt);
  770. fieldvarsym:
  771. write_rtti(tfieldvarsym(sym).vardef,rt);
  772. else
  773. ;
  774. end;
  775. end;
  776. end;
  777. end;
  778. procedure TRTTIWriter.collect_propnamelist(propnamelist:TFPHashObjectList;def:tabstractrecorddef;visibilities:tvisibilities);
  779. var
  780. i : longint;
  781. sym : tsym;
  782. pn : tpropnamelistitem;
  783. begin
  784. { search into parent for objects }
  785. if def.typ=objectdef then
  786. begin
  787. if assigned(tobjectdef(def).childof) then
  788. collect_propnamelist(propnamelist,tobjectdef(def).childof,visibilities);
  789. end;
  790. for i:=0 to def.symtable.SymList.Count-1 do
  791. begin
  792. sym:=tsym(def.symtable.SymList[i]);
  793. if (tsym(sym).typ=propertysym) and
  794. (sym.visibility in visibilities) then
  795. begin
  796. pn:=TPropNameListItem(propnamelist.Find(tsym(sym).name));
  797. if not assigned(pn) then
  798. begin
  799. pn:=tpropnamelistitem.create(propnamelist,tsym(sym).name);
  800. pn.propindex:=propnamelist.count-1;
  801. pn.propowner:=tsym(sym).owner;
  802. end;
  803. end;
  804. end;
  805. end;
  806. procedure TRTTIWriter.properties_write_rtti_data(tcb: ttai_typedconstbuilder; propnamelist:TFPHashObjectList; st:tsymtable; extended_rtti:boolean; visibilities:tvisibilities);
  807. var
  808. i : longint;
  809. sym : tsym;
  810. proctypesinfo : byte;
  811. propnameitem : tpropnamelistitem;
  812. propdefname : string;
  813. tbltcb : ttai_typedconstbuilder;
  814. tbllab : tasmlabel;
  815. tbldef : tdef;
  816. procedure writeaccessproc(tcb: ttai_typedconstbuilder; pap:tpropaccesslisttypes; shiftvalue : byte; unsetvalue: byte);
  817. var
  818. typvalue : byte;
  819. hp : ppropaccesslistitem;
  820. extnumber: longint;
  821. address,space : longint;
  822. def : tdef;
  823. hpropsym : tpropertysym;
  824. propaccesslist : tpropaccesslist;
  825. begin
  826. hpropsym:=tpropertysym(sym);
  827. repeat
  828. propaccesslist:=hpropsym.propaccesslist[pap];
  829. if not propaccesslist.empty then
  830. break;
  831. hpropsym:=hpropsym.overriddenpropsym;
  832. until not assigned(hpropsym);
  833. if not(assigned(propaccesslist) and assigned(propaccesslist.firstsym)) then
  834. begin
  835. tcb.emit_tai(Tai_const.Create_int_codeptr(unsetvalue),codeptruinttype);
  836. typvalue:=3;
  837. end
  838. { also for accessing class fields }
  839. else if propaccesslist.firstsym^.sym.typ=fieldvarsym then
  840. begin
  841. address:=0;
  842. hp:=propaccesslist.firstsym;
  843. def:=nil;
  844. while assigned(hp) do
  845. begin
  846. case hp^.sltype of
  847. sl_load :
  848. begin
  849. def:=tfieldvarsym(hp^.sym).vardef;
  850. inc(address,tfieldvarsym(hp^.sym).fieldoffset);
  851. end;
  852. sl_subscript :
  853. begin
  854. if not(assigned(def) and
  855. ((def.typ=recorddef) or
  856. is_object(def))) then
  857. internalerror(200402171);
  858. inc(address,tfieldvarsym(hp^.sym).fieldoffset);
  859. def:=tfieldvarsym(hp^.sym).vardef;
  860. end;
  861. sl_vec :
  862. begin
  863. if not(assigned(def) and (def.typ=arraydef)) then
  864. internalerror(200402172);
  865. def:=tarraydef(def).elementdef;
  866. {Hp.value is a Tconstexprint, which can be rather large,
  867. sanity check for longint overflow.}
  868. space:=(high(address)-address) div def.size;
  869. if int64(space)<hp^.value then
  870. internalerror(200706101);
  871. inc(address,int64(def.size*hp^.value));
  872. end;
  873. else
  874. internalerror(2019050523);
  875. end;
  876. hp:=hp^.next;
  877. end;
  878. tcb.emit_tai(Tai_const.Create_int_codeptr(address),codeptruinttype);
  879. typvalue:=0;
  880. end
  881. else
  882. begin
  883. { When there was an error then procdef is not assigned }
  884. if not assigned(propaccesslist.procdef) then
  885. exit;
  886. if not(po_virtualmethod in tprocdef(propaccesslist.procdef).procoptions) or
  887. is_objectpascal_helper(tprocdef(propaccesslist.procdef).struct) then
  888. begin
  889. tcb.queue_init(codeptruinttype);
  890. tcb.queue_emit_proc(tprocdef(propaccesslist.procdef));
  891. typvalue:=1;
  892. end
  893. else
  894. begin
  895. { virtual method, write vmt offset }
  896. extnumber:=tprocdef(propaccesslist.procdef).extnumber;
  897. tcb.emit_tai(Tai_const.Create_int_codeptr(
  898. tobjectdef(tprocdef(propaccesslist.procdef).struct).vmtmethodoffset(extnumber)),
  899. codeptruinttype);
  900. { register for wpo }
  901. tobjectdef(tprocdef(propaccesslist.procdef).struct).register_vmt_call(extnumber);
  902. {$ifdef vtentry}
  903. { not sure if we can insert those vtentry symbols safely here }
  904. {$error register methods used for published properties}
  905. {$endif vtentry}
  906. typvalue:=2;
  907. end;
  908. end;
  909. proctypesinfo:=proctypesinfo or (typvalue shl shiftvalue);
  910. end;
  911. function properties_count(st:tsymtable):longint;
  912. var
  913. i : longint;
  914. sym : tsym;
  915. begin
  916. result:=0;
  917. for i:=0 to st.SymList.Count-1 do
  918. begin
  919. sym:=tsym(st.SymList[i]);
  920. if (tsym(sym).typ=propertysym) and
  921. (sym.visibility in visibilities) then
  922. inc(result);
  923. end;
  924. end;
  925. function write_propinfo_data(tcb: ttai_typedconstbuilder; sym: tpropertysym): tdef;
  926. begin
  927. { we can only easily reuse defs if the property is not stored,
  928. because otherwise the rtti layout depends on how the "stored"
  929. is defined (field, indexed expression, virtual method, ...) }
  930. if not(ppo_stored in sym.propoptions) then
  931. propdefname:=internaltypeprefixName[itp_rtti_prop]+tostr(length(sym.realname))
  932. else
  933. propdefname:='';
  934. { TPropInfo is a packed record (even on targets that require
  935. alignment), but it starts aligned }
  936. if addcomments then
  937. tcb.emit_comment('RTTI: begin propinfo record '+sym.realname);
  938. tcb.begin_anonymous_record(
  939. propdefname,
  940. 1,min(reqalign,SizeOf(PInt)),
  941. targetinfos[target_info.system]^.alignment.recordalignmin);
  942. if ppo_indexed in sym.propoptions then
  943. proctypesinfo:=$40
  944. else
  945. proctypesinfo:=0;
  946. if addcomments then
  947. tcb.emit_comment(#9'type info');
  948. write_rtti_reference(tcb,sym.propdef,fullrtti);
  949. if addcomments then
  950. tcb.emit_comment(#9'read access');
  951. writeaccessproc(tcb,palt_read,0,0);
  952. if addcomments then
  953. tcb.emit_comment(#9'write access');
  954. writeaccessproc(tcb,palt_write,2,0);
  955. { is it stored ? }
  956. if addcomments then
  957. tcb.emit_comment(#9'stored ?');
  958. if not(ppo_stored in sym.propoptions) then
  959. begin
  960. { no, so put a constant zero }
  961. tcb.emit_tai(Tai_const.Create_nil_codeptr,codeptruinttype);
  962. proctypesinfo:=proctypesinfo or (3 shl 4);
  963. end
  964. else
  965. writeaccessproc(tcb,palt_stored,4,1); { maybe; if no procedure put a constant 1 (=true) }
  966. if addcomments then
  967. tcb.emit_comment(#9'Index');
  968. tcb.emit_ord_const(sym.index,u32inttype);
  969. if addcomments then
  970. tcb.emit_comment(#9'default');
  971. tcb.emit_ord_const(sym.default,u32inttype);
  972. propnameitem:=TPropNameListItem(propnamelist.Find(sym.name));
  973. if not assigned(propnameitem) then
  974. internalerror(200512201);
  975. if addcomments then
  976. tcb.emit_comment(#9'property index');
  977. tcb.emit_ord_const(propnameitem.propindex,u16inttype);
  978. if addcomments then
  979. tcb.emit_comment(#9'proc types');
  980. tcb.emit_ord_const(proctypesinfo,u8inttype);
  981. { write reference to attribute table }
  982. if addcomments then
  983. tcb.emit_comment(#9'attributes');
  984. write_attribute_data(tcb,sym.rtti_attribute_list);
  985. { write property name }
  986. if addcomments then
  987. tcb.emit_comment(#9'name');
  988. tcb.emit_shortstring_const(sym.realname);
  989. result:=tcb.end_anonymous_record;
  990. if addcomments then
  991. tcb.emit_comment('RTTI: End propinfo record '+sym.realname);
  992. end;
  993. begin
  994. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,SizeOf(PInt)),
  995. targetinfos[target_info.system]^.alignment.recordalignmin);
  996. tcb.emit_ord_const(properties_count(st),u16inttype);
  997. for i:=0 to st.SymList.Count-1 do
  998. begin
  999. sym:=tsym(st.SymList[i]);
  1000. if (sym.typ=propertysym) and
  1001. (sym.visibility in visibilities) then
  1002. begin
  1003. if extended_rtti then
  1004. begin
  1005. {
  1006. TPropInfoEx = record
  1007. Flags: Byte;
  1008. Info: PPropInfo;
  1009. // AttrData: TAttrData
  1010. end;
  1011. }
  1012. maybe_add_comment(tcb,'RTTI: begin property '+sym.prettyname);
  1013. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,SizeOf(PInt)),
  1014. targetinfos[target_info.system]^.alignment.recordalignmin);
  1015. { write visiblity flags for extended RTTI }
  1016. maybe_add_comment(tcb,#9'visibility flags');
  1017. tcb.emit_ord_const(byte(visibility_to_rtti_flags(sym.visibility)),u8inttype);
  1018. { create separate constant builder }
  1019. current_asmdata.getglobaldatalabel(tbllab);
  1020. tbltcb:=ctai_typedconstbuilder.create([tcalo_is_lab,tcalo_make_dead_strippable]);
  1021. { write TPropInfo record }
  1022. tbldef:=write_propinfo_data(tbltcb,tpropertysym(sym));
  1023. current_asmdata.asmlists[al_rtti].concatlist(
  1024. tbltcb.get_final_asmlist(tbllab,tbldef,sec_rodata,tbllab.name,const_align(sizeof(pint)))
  1025. );
  1026. tbltcb.free;
  1027. { write the pointer to the prop info }
  1028. maybe_add_comment(tcb,#9'property info reference');
  1029. tcb.emit_tai(Tai_const.Create_sym(tbllab),voidpointertype);
  1030. { end record }
  1031. tcb.end_anonymous_record;
  1032. maybe_add_comment(tcb,'RTTI: end property '+sym.prettyname);
  1033. end
  1034. else
  1035. write_propinfo_data(tcb,tpropertysym(sym));
  1036. end;
  1037. end;
  1038. tcb.end_anonymous_record;
  1039. end;
  1040. procedure TRTTIWriter.write_rtti_data(tcb: ttai_typedconstbuilder; def: tdef; rt: trttitype);
  1041. procedure unknown_rtti(def:tstoreddef);
  1042. begin
  1043. tcb.emit_ord_const(tkUnknown,u8inttype);
  1044. write_rtti_name(tcb,def);
  1045. write_common_rtti_data(tcb,def,rt);
  1046. end;
  1047. procedure variantdef_rtti(def:tvariantdef);
  1048. begin
  1049. write_header(tcb,def,tkVariant);
  1050. write_common_rtti_data(tcb,def,rt);
  1051. end;
  1052. procedure stringdef_rtti(def:tstringdef);
  1053. const
  1054. string_typekinds: array[tstringtype] of byte = (
  1055. tkSString,tkLString,tkAString,tkWString,tkUString
  1056. );
  1057. begin
  1058. write_header(tcb,def,string_typekinds[def.stringtype]);
  1059. tcb.begin_anonymous_record(
  1060. internaltypeprefixName[itp_rtti_outer]+tostr(string_typekinds[def.stringtype]),
  1061. defaultpacking,reqalign,
  1062. targetinfos[target_info.system]^.alignment.recordalignmin);
  1063. write_common_rtti_data(tcb,def,rt);
  1064. case def.stringtype of
  1065. st_ansistring:
  1066. begin
  1067. tcb.begin_anonymous_record(
  1068. internaltypeprefixName[itp_rtti_case]+tostr(string_typekinds[def.stringtype]),
  1069. defaultpacking,reqalign,
  1070. targetinfos[target_info.system]^.alignment.recordalignmin);
  1071. tcb.emit_ord_const(def.encoding,u16inttype);
  1072. tcb.end_anonymous_record;
  1073. end;
  1074. st_shortstring:
  1075. begin
  1076. tcb.begin_anonymous_record(
  1077. internaltypeprefixName[itp_rtti_case]+tostr(string_typekinds[def.stringtype]),
  1078. defaultpacking,reqalign,
  1079. targetinfos[target_info.system]^.alignment.recordalignmin);
  1080. tcb.emit_ord_const(def.len,u8inttype);
  1081. tcb.end_anonymous_record;
  1082. end;
  1083. st_widestring,
  1084. st_unicodestring,
  1085. st_longstring:
  1086. { nothing }
  1087. ;
  1088. end;
  1089. tcb.end_anonymous_record;
  1090. end;
  1091. procedure enumdef_rtti(def: tenumdef);
  1092. var
  1093. i : integer;
  1094. hp : tenumsym;
  1095. begin
  1096. write_header(tcb,def,tkEnumeration);
  1097. { align; the named fields are so that we can let the compiler
  1098. calculate the string offsets later on }
  1099. tcb.next_field_name:='size_start_rec';
  1100. { add a typename so that it can be reused when writing the the s2o
  1101. and o2s arrays for llvm (otherwise we have to write out the entire
  1102. type definition every time we access an element from this record) }
  1103. tcb.begin_anonymous_record(internaltypeprefixName[itp_rtti_enum_size_start_rec]+def.unique_id_str,defaultpacking,reqalign,
  1104. targetinfos[target_info.system]^.alignment.recordalignmin);
  1105. write_common_rtti_data(tcb,def,rt);
  1106. tcb.next_field_name:='typ_union_rec';
  1107. tcb.begin_anonymous_record(internaltypeprefixName[itp_rtti_enum_size_start_rec2]+def.unique_id_str,defaultpacking,reqalign,
  1108. targetinfos[target_info.system]^.alignment.recordalignmin);
  1109. case longint(def.size) of
  1110. 1 :
  1111. tcb.emit_ord_const(otUByte,u8inttype);
  1112. 2 :
  1113. tcb.emit_ord_const(otUWord,u8inttype);
  1114. 4 :
  1115. tcb.emit_ord_const(otULong,u8inttype);
  1116. end;
  1117. { we need to align by Tconstptruint here to satisfy the alignment
  1118. rules set by records: in the typinfo unit we overlay a TTypeData
  1119. record on this data, which at the innermost variant record needs an
  1120. alignment of TConstPtrUint due to e.g. the "CompType" member for
  1121. tkSet (also the "BaseType" member for tkEnumeration).
  1122. We need to adhere to this, otherwise things will break. }
  1123. tcb.next_field_name:='min_max_rec';
  1124. tcb.begin_anonymous_record(internaltypeprefixName[itp_rtti_enum_min_max_rec]+def.unique_id_str,defaultpacking,reqalign,
  1125. targetinfos[target_info.system]^.alignment.recordalignmin);
  1126. tcb.emit_ord_const(def.min,s32inttype);
  1127. tcb.emit_ord_const(def.max,s32inttype);
  1128. tcb.next_field_name:='basetype_array_rec';
  1129. { all strings must appear right after each other -> from now on
  1130. packrecords 1 (but the start must still be aligned) }
  1131. tcb.begin_anonymous_record(internaltypeprefixName[itp_rtti_enum_basetype_array_rec]+def.unique_id_str,1,reqalign,
  1132. targetinfos[target_info.system]^.alignment.recordalignmin);
  1133. { write base type }
  1134. write_rtti_reference(tcb,def.basedef,rt);
  1135. for i:=0 to def.symtable.SymList.Count-1 do
  1136. begin
  1137. hp:=tenumsym(def.symtable.SymList[i]);
  1138. if hp.value<def.minval then
  1139. continue
  1140. else
  1141. if hp.value>def.maxval then
  1142. break;
  1143. tcb.next_field_name:=hp.name;
  1144. tcb.emit_shortstring_const(hp.realname);
  1145. end;
  1146. { write unit name }
  1147. tcb.emit_shortstring_const(current_module.realmodulename^);
  1148. { write zero which is required by RTL }
  1149. tcb.emit_ord_const(0,u8inttype);
  1150. { terminate all records }
  1151. tcb.end_anonymous_record;
  1152. tcb.end_anonymous_record;
  1153. tcb.end_anonymous_record;
  1154. tcb.end_anonymous_record;
  1155. end;
  1156. procedure orddef_rtti(def:torddef);
  1157. procedure doint32_64(typekind: byte;min,max:int64);
  1158. const
  1159. trans : array[tordtype] of byte =
  1160. (otUByte{otNone},
  1161. otUByte,otUWord,otULong,otUQWord,otUByte{otNone},
  1162. otSByte,otSWord,otSLong,otSQWord,otUByte{otNone},
  1163. otUByte,otUByte,otUWord,otULong,otUQWord,
  1164. otSByte,otSWord,otSLong,otSQWord,
  1165. otUByte,otUWord,otUByte,255);
  1166. var
  1167. elesize: string[1];
  1168. deftrans: byte;
  1169. begin
  1170. write_header(tcb,def,typekind);
  1171. deftrans:=trans[def.ordtype];
  1172. case deftrans of
  1173. otUQWord,
  1174. otSQWord:
  1175. elesize:='8';
  1176. 255:
  1177. begin
  1178. if def.packedbitsize<=32 then
  1179. begin
  1180. elesize:='4';
  1181. if def.low<0 then
  1182. deftrans:=otSLong
  1183. else
  1184. deftrans:=otULong;
  1185. end
  1186. else
  1187. begin
  1188. elesize:='8';
  1189. if def.low<0 then
  1190. deftrans:=otSQWord
  1191. else
  1192. deftrans:=otUQWord;
  1193. end;
  1194. end
  1195. else
  1196. elesize:='4'
  1197. end;
  1198. tcb.begin_anonymous_record(
  1199. internaltypeprefixName[itp_rtti_ord_outer]+elesize,
  1200. defaultpacking,reqalign,
  1201. targetinfos[target_info.system]^.alignment.recordalignmin);
  1202. write_common_rtti_data(tcb,def,rt);
  1203. tcb.begin_anonymous_record(
  1204. internaltypeprefixName[itp_rtti_ord_middle]+elesize,
  1205. defaultpacking,reqalign,
  1206. targetinfos[target_info.system]^.alignment.recordalignmin);
  1207. tcb.emit_ord_const(byte(trans[def.ordtype]),u8inttype);
  1208. tcb.begin_anonymous_record(
  1209. internaltypeprefixName[itp_rtti_ord_inner]+elesize,
  1210. defaultpacking,reqalign,
  1211. targetinfos[target_info.system]^.alignment.recordalignmin);
  1212. {Convert to longint to smuggle values in high(longint)+1..high(cardinal) into asmlist.}
  1213. case deftrans of
  1214. otUQWord:
  1215. begin
  1216. tcb.emit_ord_const(min,u64inttype);
  1217. tcb.emit_ord_const(max,u64inttype);
  1218. end;
  1219. otSQWord:
  1220. begin
  1221. tcb.emit_ord_const(min,s64inttype);
  1222. tcb.emit_ord_const(max,s64inttype);
  1223. end;
  1224. else
  1225. begin
  1226. tcb.emit_ord_const(longint(min),s32inttype);
  1227. tcb.emit_ord_const(longint(max),s32inttype);
  1228. end;
  1229. end;
  1230. tcb.end_anonymous_record;
  1231. tcb.end_anonymous_record;
  1232. tcb.end_anonymous_record;
  1233. end;
  1234. procedure dointeger(typekind:byte);inline;
  1235. begin
  1236. doint32_64(typekind,int64(def.low.svalue),int64(def.high.svalue));
  1237. end;
  1238. begin
  1239. case def.ordtype of
  1240. s64bit :
  1241. dointeger(tkInt64);
  1242. u64bit :
  1243. dointeger(tkQWord);
  1244. pasbool1,
  1245. pasbool8,
  1246. pasbool16,
  1247. pasbool32,
  1248. pasbool64:
  1249. dointeger(tkBool);
  1250. { use different low/high values to be Delphi compatible }
  1251. bool8bit,
  1252. bool16bit,
  1253. bool32bit:
  1254. doint32_64(tkBool,longint(low(longint)),longint(high(longint)));
  1255. bool64bit:
  1256. doint32_64(tkBool,low(int64),high(int64));
  1257. uchar:
  1258. dointeger(tkChar);
  1259. uwidechar:
  1260. dointeger(tkWChar);
  1261. scurrency:
  1262. begin
  1263. write_header(tcb,def,tkFloat);
  1264. tcb.begin_anonymous_record(
  1265. internaltypeprefixName[itp_1byte],
  1266. defaultpacking,reqalign,
  1267. targetinfos[target_info.system]^.alignment.recordalignmin);
  1268. write_common_rtti_data(tcb,def,rt);
  1269. tcb.emit_ord_const(ftCurr,u8inttype);
  1270. tcb.end_anonymous_record;
  1271. end;
  1272. else
  1273. dointeger(tkInteger);
  1274. end;
  1275. end;
  1276. procedure floatdef_rtti(def:tfloatdef);
  1277. const
  1278. {tfloattype = (s32real,s64real,s80real,sc80real,s64bit,s128bit);}
  1279. translate : array[tfloattype] of byte =
  1280. (ftSingle,ftDouble,ftExtended,ftExtended,ftComp,ftCurr,ftFloat128);
  1281. begin
  1282. write_header(tcb,def,tkFloat);
  1283. tcb.begin_anonymous_record(
  1284. internaltypeprefixName[itp_1byte],
  1285. defaultpacking,reqalign,
  1286. targetinfos[target_info.system]^.alignment.recordalignmin);
  1287. write_common_rtti_data(tcb,def,rt);
  1288. tcb.begin_anonymous_record(
  1289. internaltypeprefixName[itp_rtti_float],
  1290. defaultpacking,reqalign,
  1291. targetinfos[target_info.system]^.alignment.recordalignmin);
  1292. tcb.emit_ord_const(translate[def.floattype],u8inttype);
  1293. tcb.end_anonymous_record;
  1294. tcb.end_anonymous_record;
  1295. end;
  1296. procedure setdef_rtti(def:tsetdef);
  1297. begin
  1298. write_header(tcb,def,tkSet);
  1299. tcb.begin_anonymous_record(
  1300. internaltypeprefixName[itp_rtti_set_outer],
  1301. defaultpacking,reqalign,
  1302. targetinfos[target_info.system]^.alignment.recordalignmin);
  1303. write_common_rtti_data(tcb,def,rt);
  1304. tcb.begin_anonymous_record(
  1305. internaltypeprefixName[itp_rtti_set_middle],
  1306. defaultpacking,reqalign,
  1307. targetinfos[target_info.system]^.alignment.recordalignmin);
  1308. case def.size of
  1309. 1:
  1310. tcb.emit_ord_const(otUByte,u8inttype);
  1311. 2:
  1312. tcb.emit_ord_const(otUWord,u8inttype);
  1313. 4:
  1314. tcb.emit_ord_const(otULong,u8inttype);
  1315. else
  1316. tcb.emit_ord_const(otUByte,u8inttype);
  1317. end;
  1318. tcb.begin_anonymous_record(
  1319. internaltypeprefixName[itp_rtti_set_inner],
  1320. defaultpacking,reqalign,
  1321. targetinfos[target_info.system]^.alignment.recordalignmin);
  1322. tcb.emit_ord_const(def.size,sizesinttype);
  1323. write_rtti_reference(tcb,def.elementdef,rt);
  1324. tcb.end_anonymous_record;
  1325. tcb.end_anonymous_record;
  1326. tcb.end_anonymous_record;
  1327. end;
  1328. procedure arraydef_rtti(def:tarraydef);
  1329. var
  1330. i,dimcount: byte;
  1331. totalcount: asizeuint;
  1332. finaldef: tdef;
  1333. curdef:tarraydef;
  1334. begin
  1335. if ado_IsDynamicArray in def.arrayoptions then
  1336. tcb.emit_ord_const(tkDynArray,u8inttype)
  1337. else
  1338. tcb.emit_ord_const(tkArray,u8inttype);
  1339. write_rtti_name(tcb,def);
  1340. if not(ado_IsDynamicArray in def.arrayoptions) then
  1341. begin
  1342. { remember tha last instruction. we will need to insert some
  1343. calculated values after it }
  1344. finaldef:=def;
  1345. totalcount:=1;
  1346. dimcount:=0;
  1347. repeat
  1348. curdef:=tarraydef(finaldef);
  1349. finaldef:=curdef.elementdef;
  1350. { Dims[i] PTypeInfo }
  1351. inc(dimcount);
  1352. totalcount:=totalcount*curdef.elecount;
  1353. until (finaldef.typ<>arraydef) or
  1354. (ado_IsDynamicArray in tarraydef(finaldef).arrayoptions);
  1355. tcb.begin_anonymous_record(
  1356. internaltypeprefixName[itp_rtti_normal_array]+tostr(dimcount),
  1357. defaultpacking,reqalign,
  1358. targetinfos[target_info.system]^.alignment.recordalignmin);
  1359. write_common_rtti_data(tcb,def,rt);
  1360. tcb.begin_anonymous_record(internaltypeprefixName[itp_rtti_normal_array_inner]+tostr(dimcount),
  1361. defaultpacking,reqalign,
  1362. targetinfos[target_info.system]^.alignment.recordalignmin);
  1363. { total size = elecount * elesize of the first arraydef }
  1364. tcb.emit_tai(Tai_const.Create_sizeint(def.elecount*def.elesize),sizeuinttype);
  1365. { total element count }
  1366. tcb.emit_tai(Tai_const.Create_sizeint(asizeint(totalcount)),sizeuinttype);
  1367. { last dimension element type }
  1368. tcb.emit_tai(Tai_const.Create_sym(get_rtti_label(curdef.elementdef,rt,true)),voidpointertype);
  1369. { dimension count }
  1370. tcb.emit_ord_const(dimcount,u8inttype);
  1371. finaldef:=def;
  1372. { ranges of the dimensions }
  1373. for i:=1 to dimcount do
  1374. begin
  1375. curdef:=tarraydef(finaldef);
  1376. finaldef:=curdef.elementdef;
  1377. { Dims[i] PPTypeInfo }
  1378. write_rtti_reference(tcb,curdef.rangedef,rt);
  1379. end;
  1380. end
  1381. else
  1382. { write a delphi almost compatible dyn. array entry:
  1383. there are two types, eltype and eltype2, the latter is nil if the element type needs
  1384. no finalization, the former is always valid, delphi has this swapped, but for
  1385. compatibility with older fpc versions we do it different, to be delphi compatible,
  1386. the names are swapped in typinfo.pp
  1387. }
  1388. begin
  1389. tcb.begin_anonymous_record(
  1390. internaltypeprefixName[itp_rtti_dyn_array],
  1391. defaultpacking,reqalign,
  1392. targetinfos[target_info.system]^.alignment.recordalignmin);
  1393. write_common_rtti_data(tcb,def,rt);
  1394. { record in TypInfo is aligned differently from init rtti }
  1395. tcb.begin_anonymous_record(
  1396. internaltypeprefixName[itp_rtti_dyn_array_inner],
  1397. defaultpacking,reqalign,
  1398. targetinfos[target_info.system]^.alignment.recordalignmin);
  1399. { size of elements }
  1400. tcb.emit_tai(Tai_const.Create_sizeint(def.elesize),sizeuinttype);
  1401. { element type }
  1402. write_rtti_reference(tcb,def.elementdef,rt);
  1403. { variant type }
  1404. tcb.emit_ord_const(tstoreddef(def.elementdef).getvardef,s32inttype);
  1405. { element type }
  1406. if def.elementdef.needs_inittable then
  1407. write_rtti_reference(tcb,def.elementdef,rt)
  1408. else
  1409. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1410. { write unit name }
  1411. tcb.emit_shortstring_const(current_module.realmodulename^);
  1412. end;
  1413. tcb.end_anonymous_record;
  1414. tcb.end_anonymous_record;
  1415. end;
  1416. procedure classrefdef_rtti(def:tclassrefdef);
  1417. begin
  1418. write_header(tcb,def,tkClassRef);
  1419. tcb.begin_anonymous_record(
  1420. internaltypeprefixName[itp_rtti_ref],
  1421. defaultpacking,reqalign,
  1422. targetinfos[target_info.system]^.alignment.recordalignmin);
  1423. write_common_rtti_data(tcb,def,rt);
  1424. tcb.begin_anonymous_record(
  1425. internaltypeprefixName[itp_rtti_classref],
  1426. defaultpacking,reqalign,
  1427. targetinfos[target_info.system]^.alignment.recordalignmin);
  1428. write_rtti_reference(tcb,def.pointeddef,rt);
  1429. tcb.end_anonymous_record;
  1430. tcb.end_anonymous_record;
  1431. end;
  1432. procedure pointerdef_rtti(def:tpointerdef);
  1433. begin
  1434. write_header(tcb,def,tkPointer);
  1435. tcb.begin_anonymous_record(
  1436. internaltypeprefixName[itp_rtti_ref],
  1437. defaultpacking,reqalign,
  1438. targetinfos[target_info.system]^.alignment.recordalignmin);
  1439. write_common_rtti_data(tcb,def,rt);
  1440. tcb.begin_anonymous_record(
  1441. internaltypeprefixName[itp_rtti_pointer],
  1442. defaultpacking,reqalign,
  1443. targetinfos[target_info.system]^.alignment.recordalignmin);
  1444. write_rtti_reference(tcb,def.pointeddef,rt);
  1445. tcb.end_anonymous_record;
  1446. tcb.end_anonymous_record;
  1447. end;
  1448. procedure recorddef_rtti(def:trecorddef);
  1449. procedure write_record_operators(rttilab:tasmlabel);
  1450. var
  1451. rttidef: tdef;
  1452. tcb: ttai_typedconstbuilder;
  1453. mop: tmanagementoperator;
  1454. procdef: tprocdef;
  1455. begin
  1456. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable]);
  1457. tcb.begin_anonymous_record(
  1458. '',
  1459. defaultpacking,min(reqalign,SizeOf(PInt)),
  1460. targetinfos[target_info.system]^.alignment.recordalignmin
  1461. );
  1462. { use "succ" to omit first enum item "mop_none" }
  1463. for mop := succ(low(tmanagementoperator)) to high(tmanagementoperator) do
  1464. begin
  1465. if not (mop in trecordsymtable(def.symtable).managementoperators) then
  1466. tcb.emit_tai(Tai_const.Create_nil_codeptr,voidcodepointertype)
  1467. else
  1468. begin
  1469. procdef := search_management_operator(mop, def);
  1470. if procdef = nil then
  1471. internalerror(201603021)
  1472. else
  1473. tcb.emit_tai(Tai_const.Createname(procdef.mangledname,AT_FUNCTION,0),
  1474. cprocvardef.getreusableprocaddr(procdef,pc_address_only));
  1475. end;
  1476. end;
  1477. rttidef := tcb.end_anonymous_record;
  1478. current_asmdata.AsmLists[al_rtti].concatList(
  1479. tcb.get_final_asmlist(rttilab,rttidef,sec_rodata,rttilab.name,
  1480. sizeof(PInt)));
  1481. tcb.free;
  1482. end;
  1483. procedure write_extended_property_table;
  1484. var
  1485. propnamelist: TFPHashObjectList;
  1486. visibilities: tvisibilities;
  1487. begin
  1488. propnamelist:=TFPHashObjectList.Create;
  1489. visibilities:=def.rtti_visibilities_for_option(ro_properties);
  1490. collect_propnamelist(propnamelist,def,visibilities);
  1491. properties_write_rtti_data(tcb,propnamelist,def.symtable,true,visibilities);
  1492. propnamelist.free;
  1493. end;
  1494. var
  1495. oplab : tasmlabel;
  1496. begin
  1497. write_header(tcb,def,tkRecord);
  1498. { need extra reqalign record, because otherwise the u32 int will
  1499. only be aligned to 4 even on 64 bit target (while the rtti code
  1500. in typinfo expects alignments to sizeof(pointer)) }
  1501. tcb.begin_anonymous_record('',
  1502. defaultpacking,reqalign,
  1503. targetinfos[target_info.system]^.alignment.recordalignmin);
  1504. write_common_rtti_data(tcb,def,rt);
  1505. tcb.begin_anonymous_record('',
  1506. defaultpacking,reqalign,
  1507. targetinfos[target_info.system]^.alignment.recordalignmin);
  1508. { store special terminator for init table for more optimal rtl operations
  1509. strictly related to RecordRTTI procedure in rtti.inc (directly
  1510. related to RTTIRecordRttiInfoToInitInfo function) }
  1511. if rt=initrtti then
  1512. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype)
  1513. else
  1514. { we use a direct reference as the init RTTI is always in the same
  1515. unit as the full RTTI }
  1516. tcb.emit_tai(Tai_const.Create_sym(get_rtti_label(def,initrtti,false)),voidpointertype);
  1517. tcb.emit_ord_const(def.size,u32inttype);
  1518. oplab:=nil;
  1519. { store rtti management operators only for init table }
  1520. if rt=initrtti then
  1521. begin
  1522. { for now records don't have the initializer table }
  1523. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1524. if (trecordsymtable(def.symtable).managementoperators=[]) then
  1525. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype)
  1526. else
  1527. begin
  1528. current_asmdata.getlocaldatalabel(oplab);
  1529. tcb.emit_tai(Tai_const.Createname(
  1530. oplab.name,
  1531. AT_DATA_FORCEINDIRECT,0),voidpointertype);
  1532. end;
  1533. end;
  1534. fields_write_rtti_data(tcb,def,rt);
  1535. { write extended rtti }
  1536. if rt=fullrtti then
  1537. begin
  1538. write_extended_field_table(tcb,def,defaultpacking);
  1539. write_extended_method_table(tcb,def,defaultpacking);
  1540. write_extended_property_table;
  1541. end;
  1542. tcb.end_anonymous_record;
  1543. tcb.end_anonymous_record;
  1544. { write pointers to operators if needed }
  1545. if (rt=initrtti) and (trecordsymtable(def.symtable).managementoperators<>[]) then
  1546. write_record_operators(oplab);
  1547. end;
  1548. procedure procvardef_rtti(def:tprocvardef);
  1549. procedure write_para(parasym:tparavarsym);
  1550. begin
  1551. { write flags for current parameter }
  1552. write_param_flag(tcb,parasym);
  1553. { write name of current parameter }
  1554. tcb.emit_shortstring_const(parasym.realname);
  1555. { write name of type of current parameter }
  1556. write_rtti_name(tcb,parasym.vardef);
  1557. end;
  1558. procedure write_procedure_param(parasym:tparavarsym);
  1559. begin
  1560. { every parameter is expected to start aligned }
  1561. tcb.begin_anonymous_record(
  1562. internaltypeprefixName[itp_rtti_proc_param]+tostr(length(parasym.realname)),
  1563. defaultpacking,min(reqalign,SizeOf(PInt)),
  1564. targetinfos[target_info.system]^.alignment.recordalignmin);
  1565. { write flags for current parameter }
  1566. write_param_flag(tcb,parasym);
  1567. { write param type }
  1568. if is_open_array(parasym.vardef) or is_array_of_const(parasym.vardef) then
  1569. write_rtti_reference(tcb,tarraydef(parasym.vardef).elementdef,fullrtti)
  1570. else if parasym.vardef=cformaltype then
  1571. write_rtti_reference(tcb,nil,fullrtti)
  1572. else
  1573. write_rtti_reference(tcb,parasym.vardef,fullrtti);
  1574. { write name of current parameter }
  1575. tcb.emit_shortstring_const(parasym.realname);
  1576. tcb.end_anonymous_record;
  1577. end;
  1578. var
  1579. methodkind : byte;
  1580. i : integer;
  1581. begin
  1582. if po_methodpointer in def.procoptions then
  1583. begin
  1584. { write method id and name }
  1585. write_header(tcb,def,tkMethod);
  1586. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1587. targetinfos[target_info.system]^.alignment.recordalignmin);
  1588. write_common_rtti_data(tcb,def,rt);
  1589. tcb.begin_anonymous_record('',
  1590. defaultpacking,reqalign,
  1591. targetinfos[target_info.system]^.alignment.recordalignmin);
  1592. { write kind of method }
  1593. methodkind:=write_methodkind(tcb,def);
  1594. { write parameter info. The parameters must be written in reverse order
  1595. if this method uses right to left parameter pushing! }
  1596. tcb.emit_ord_const(def.paras.count,u8inttype);
  1597. for i:=0 to def.paras.count-1 do
  1598. write_para(tparavarsym(def.paras[i]));
  1599. if (methodkind=mkFunction) or (methodkind=mkClassFunction) then
  1600. begin
  1601. { write name of result type }
  1602. write_rtti_name(tcb,def.returndef);
  1603. { enclosing record takes care of alignment }
  1604. { write result typeinfo }
  1605. write_rtti_reference(tcb,def.returndef,fullrtti);
  1606. end;
  1607. { write calling convention }
  1608. write_callconv(tcb,def);
  1609. { enclosing record takes care of alignment }
  1610. { write params typeinfo }
  1611. for i:=0 to def.paras.count-1 do
  1612. begin
  1613. if is_open_array(tparavarsym(def.paras[i]).vardef) or is_array_of_const(tparavarsym(def.paras[i]).vardef) then
  1614. write_rtti_reference(tcb,tarraydef(tparavarsym(def.paras[i]).vardef).elementdef,fullrtti)
  1615. else if tparavarsym(def.paras[i]).vardef=cformaltype then
  1616. write_rtti_reference(tcb,nil,fullrtti)
  1617. else
  1618. write_rtti_reference(tcb,tparavarsym(def.paras[i]).vardef,fullrtti);
  1619. end;
  1620. tcb.end_anonymous_record;
  1621. tcb.end_anonymous_record;
  1622. end
  1623. else
  1624. begin
  1625. write_header(tcb,def,tkProcvar);
  1626. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1627. targetinfos[target_info.system]^.alignment.recordalignmin);
  1628. write_common_rtti_data(tcb,def,rt);
  1629. tcb.begin_anonymous_record('',
  1630. defaultpacking,reqalign,
  1631. targetinfos[target_info.system]^.alignment.recordalignmin);
  1632. { flags }
  1633. tcb.emit_ord_const(0,u8inttype);
  1634. { write calling convention }
  1635. write_callconv(tcb,def);
  1636. { enclosing record takes care of alignment }
  1637. { write result typeinfo }
  1638. write_rtti_reference(tcb,def.returndef,fullrtti);
  1639. { write parameter count }
  1640. tcb.emit_ord_const(def.paras.count,u8inttype);
  1641. for i:=0 to def.paras.count-1 do
  1642. write_procedure_param(tparavarsym(def.paras[i]));
  1643. tcb.end_anonymous_record;
  1644. tcb.end_anonymous_record;
  1645. end;
  1646. end;
  1647. procedure objectdef_rtti(def: tobjectdef);
  1648. procedure objectdef_rtti_fields(def:tobjectdef);
  1649. begin
  1650. maybe_add_comment(tcb,'RTTI begin fields '+def.objname^);
  1651. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1652. targetinfos[target_info.system]^.alignment.recordalignmin);
  1653. { - for compatiblity with record RTTI we need to write a terminator-
  1654. Nil pointer for initrtti as well for objects
  1655. - for RTTI consistency for objects we need point from fullrtti
  1656. to initrtti
  1657. - classes are assumed to have the same INIT RTTI as records
  1658. (see TObject.CleanupInstance)
  1659. - neither helper nor class type have fullrtti for fields
  1660. }
  1661. if (rt=initrtti) then
  1662. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype)
  1663. else
  1664. if (def.objecttype=odt_object) then
  1665. tcb.emit_tai(Tai_const.Create_sym(get_rtti_label(def,initrtti,false)),voidpointertype)
  1666. else
  1667. internalerror(2017011801);
  1668. tcb.emit_ord_const(def.size, u32inttype);
  1669. { pointer to management operators available only for initrtti }
  1670. if (rt=initrtti) then
  1671. begin
  1672. { initializer table only available for classes currently }
  1673. if def.objecttype=odt_class then
  1674. write_mop_offset_table(tcb,def,mop_initialize)
  1675. else
  1676. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1677. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1678. end;
  1679. { enclosing record takes care of alignment }
  1680. fields_write_rtti_data(tcb,def,rt);
  1681. tcb.end_anonymous_record;
  1682. maybe_add_comment(tcb,'RTTI end fields '+def.objname^);
  1683. end;
  1684. procedure objectdef_rtti_interface_init(def:tobjectdef);
  1685. begin
  1686. tcb.emit_ord_const(def.size, u32inttype);
  1687. end;
  1688. procedure objectdef_extended_rtti_class(def:tobjectdef);
  1689. var
  1690. propnamelist : TFPHashObjectList;
  1691. visibilities : tvisibilities;
  1692. begin
  1693. propnamelist:=TFPHashObjectList.Create;
  1694. visibilities:=def.rtti_visibilities_for_option(ro_properties);
  1695. collect_propnamelist(propnamelist,def,visibilities);
  1696. properties_write_rtti_data(tcb,propnamelist,def.symtable,true,visibilities);
  1697. propnamelist.free;
  1698. end;
  1699. procedure objectdef_rtti_class_full(def:tobjectdef);
  1700. var
  1701. propnamelist : TFPHashObjectList;
  1702. begin
  1703. { Collect unique property names with nameindex }
  1704. propnamelist:=TFPHashObjectList.Create;
  1705. collect_propnamelist(propnamelist,def,[vis_published]);
  1706. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1707. targetinfos[target_info.system]^.alignment.recordalignmin);
  1708. if not is_objectpascal_helper(def) then
  1709. begin
  1710. maybe_add_comment(tcb,#9'Parent type info');
  1711. if (oo_has_vmt in def.objectoptions) then
  1712. tcb.emit_tai(
  1713. Tai_const.Createname(def.vmt_mangledname,AT_DATA_FORCEINDIRECT,0),
  1714. cpointerdef.getreusable(def.vmt_def))
  1715. else
  1716. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1717. end;
  1718. { write parent typeinfo }
  1719. maybe_add_comment(tcb,#9'Parent type info');
  1720. write_rtti_reference(tcb,def.childof,fullrtti);
  1721. { write typeinfo of extended type }
  1722. if is_objectpascal_helper(def) then
  1723. if assigned(def.extendeddef) then
  1724. begin
  1725. maybe_add_comment(tcb,#9'helper for type info');
  1726. write_rtti_reference(tcb,def.extendeddef,fullrtti)
  1727. end
  1728. else
  1729. InternalError(2011033001);
  1730. { total number of unique properties }
  1731. maybe_add_comment(tcb,#9'Number of properties');
  1732. tcb.emit_ord_const(propnamelist.count,u16inttype);
  1733. { write unit name }
  1734. maybe_add_comment(tcb, #9'Unit name');
  1735. tcb.emit_shortstring_const(current_module.realmodulename^);
  1736. { write published properties for this object }
  1737. properties_write_rtti_data(tcb,propnamelist,def.symtable,false,[vis_published]);
  1738. { write extended properties }
  1739. objectdef_extended_rtti_class(def);
  1740. tcb.end_anonymous_record;
  1741. propnamelist.free;
  1742. end;
  1743. procedure objectdef_rtti_interface_full(def:tobjectdef);
  1744. var
  1745. propnamelist : TFPHashObjectList;
  1746. { if changed to a set, make sure it's still a byte large, and
  1747. swap appropriately when cross-compiling
  1748. }
  1749. IntfFlags: byte;
  1750. begin
  1751. { Collect unique property names with nameindex }
  1752. propnamelist:=TFPHashObjectList.Create;
  1753. collect_propnamelist(propnamelist,def,[vis_published]);
  1754. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1755. targetinfos[target_info.system]^.alignment.recordalignmin);
  1756. { write parent typeinfo }
  1757. write_rtti_reference(tcb,def.childof,fullrtti);
  1758. { interface: write flags, iid and iidstr }
  1759. IntfFlags:=0;
  1760. if assigned(def.iidguid) then
  1761. IntfFlags:=IntfFlags or (1 shl ord(ifHasGuid));
  1762. if (def.objecttype=odt_interfacecorba) and (def.iidstr^<>'') then
  1763. IntfFlags:=IntfFlags or (1 shl ord(ifHasStrGUID));
  1764. if (def.objecttype=odt_dispinterface) then
  1765. IntfFlags:=IntfFlags or (1 shl ord(ifDispInterface));
  1766. if (target_info.endian=endian_big) then
  1767. IntfFlags:=reverse_byte(IntfFlags);
  1768. {
  1769. ifDispatch, }
  1770. tcb.emit_ord_const(IntfFlags,u8inttype);
  1771. { write GUID }
  1772. tcb.emit_guid_const(def.iidguid^);
  1773. { write unit name }
  1774. tcb.emit_shortstring_const(current_module.realmodulename^);
  1775. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1776. targetinfos[target_info.system]^.alignment.recordalignmin);
  1777. { write iidstr }
  1778. if def.objecttype=odt_interfacecorba then
  1779. begin
  1780. { prepareguid always allocates an empty string }
  1781. if not assigned(def.iidstr) then
  1782. internalerror(2016021901);
  1783. tcb.emit_shortstring_const(def.iidstr^)
  1784. end;
  1785. { write published properties for this object }
  1786. properties_write_rtti_data(tcb,propnamelist,def.symtable,false,[vis_published]);
  1787. { write published methods for this interface }
  1788. write_methods(tcb,def.symtable,false,[vis_published]);
  1789. tcb.end_anonymous_record;
  1790. tcb.end_anonymous_record;
  1791. propnamelist.free;
  1792. end;
  1793. begin
  1794. case def.objecttype of
  1795. odt_class:
  1796. tcb.emit_ord_const(tkclass,u8inttype);
  1797. odt_object:
  1798. tcb.emit_ord_const(tkobject,u8inttype);
  1799. odt_dispinterface,
  1800. odt_interfacecom:
  1801. tcb.emit_ord_const(tkInterface,u8inttype);
  1802. odt_interfacecorba:
  1803. tcb.emit_ord_const(tkinterfaceCorba,u8inttype);
  1804. odt_helper:
  1805. tcb.emit_ord_const(tkhelper,u8inttype);
  1806. else
  1807. internalerror(200611034);
  1808. end;
  1809. { generate the name }
  1810. tcb.emit_shortstring_const(def.objrealname^);
  1811. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1812. targetinfos[target_info.system]^.alignment.recordalignmin);
  1813. write_common_rtti_data(tcb,def,rt);
  1814. case rt of
  1815. initrtti :
  1816. begin
  1817. if def.objecttype in [odt_class,odt_object,odt_helper] then
  1818. objectdef_rtti_fields(def)
  1819. else
  1820. objectdef_rtti_interface_init(def);
  1821. end;
  1822. fullrtti :
  1823. begin
  1824. tcb.begin_anonymous_record('',
  1825. defaultpacking,reqalign,
  1826. targetinfos[target_info.system]^.alignment.recordalignmin);
  1827. case def.objecttype of
  1828. odt_helper,
  1829. odt_class:
  1830. objectdef_rtti_class_full(def);
  1831. odt_object:
  1832. objectdef_rtti_fields(def);
  1833. else
  1834. objectdef_rtti_interface_full(def);
  1835. end;
  1836. tcb.end_anonymous_record;
  1837. end;
  1838. else
  1839. ;
  1840. end;
  1841. tcb.end_anonymous_record;
  1842. end;
  1843. begin
  1844. case def.typ of
  1845. variantdef :
  1846. variantdef_rtti(tvariantdef(def));
  1847. stringdef :
  1848. stringdef_rtti(tstringdef(def));
  1849. enumdef :
  1850. enumdef_rtti(tenumdef(def));
  1851. orddef :
  1852. orddef_rtti(torddef(def));
  1853. floatdef :
  1854. floatdef_rtti(tfloatdef(def));
  1855. setdef :
  1856. setdef_rtti(tsetdef(def));
  1857. procvardef :
  1858. procvardef_rtti(tprocvardef(def));
  1859. arraydef :
  1860. begin
  1861. if ado_IsBitPacked in tarraydef(def).arrayoptions then
  1862. unknown_rtti(tstoreddef(def))
  1863. else
  1864. arraydef_rtti(tarraydef(def));
  1865. end;
  1866. recorddef :
  1867. begin
  1868. if trecorddef(def).is_packed then
  1869. unknown_rtti(tstoreddef(def))
  1870. else
  1871. recorddef_rtti(trecorddef(def));
  1872. end;
  1873. objectdef :
  1874. objectdef_rtti(tobjectdef(def));
  1875. classrefdef :
  1876. classrefdef_rtti(tclassrefdef(def));
  1877. pointerdef :
  1878. pointerdef_rtti(tpointerdef(def));
  1879. else
  1880. unknown_rtti(tstoreddef(def));
  1881. end;
  1882. end;
  1883. procedure TRTTIWriter.write_attribute_data(tcb:ttai_typedconstbuilder;attr_list:trtti_attribute_list);
  1884. procedure write_args(tbltcb:ttai_typedconstbuilder;attr:trtti_attribute);
  1885. var
  1886. argtcb : ttai_typedconstbuilder;
  1887. arglab : tasmlabel;
  1888. argdef : tdef;
  1889. i : sizeint;
  1890. begin
  1891. if length(attr.paras)=0 then
  1892. begin
  1893. tbltcb.emit_tai(tai_const.Create_16bit(0),u16inttype);
  1894. tbltcb.emit_tai(tai_const.Create_nil_dataptr,voidpointertype);
  1895. end
  1896. else
  1897. begin
  1898. current_asmdata.getglobaldatalabel(arglab);
  1899. argtcb:=ctai_typedconstbuilder.create([tcalo_is_lab,tcalo_make_dead_strippable]);
  1900. argtcb.begin_anonymous_record('',defaultpacking,min(reqalign,SizeOf(PInt)),
  1901. targetinfos[target_info.system]^.alignment.recordalignmin);
  1902. for i:=0 to High(attr.paras) do
  1903. begin
  1904. case attr.paras[i].nodetype of
  1905. niln,
  1906. ordconstn,
  1907. realconstn,
  1908. stringconstn,
  1909. pointerconstn,
  1910. guidconstn:
  1911. tconstnode(attr.paras[i]).emit_data(argtcb);
  1912. setconstn:
  1913. tsetconstnode(attr.paras[i]).emit_data(argtcb);
  1914. else
  1915. internalerror(2019070803);
  1916. end;
  1917. end;
  1918. argdef:=argtcb.end_anonymous_record;
  1919. current_asmdata.asmlists[al_rtti].concatlist(
  1920. argtcb.get_final_asmlist(arglab,argdef,sec_rodata,arglab.name,const_align(sizeof(pint)))
  1921. );
  1922. argtcb.free;
  1923. { write argument size and the reference to the argument entry }
  1924. tbltcb.emit_ord_const(argdef.size,u16inttype);
  1925. tbltcb.emit_tai(Tai_const.Create_sym(arglab),voidpointertype);
  1926. end;
  1927. end;
  1928. var
  1929. count,i: word;
  1930. attr : trtti_attribute;
  1931. tbltcb : ttai_typedconstbuilder;
  1932. tbllab : tasmlabel;
  1933. tbldef : tdef;
  1934. begin
  1935. if assigned(attr_list) then
  1936. count:=attr_list.get_attribute_count
  1937. else
  1938. count:=0;
  1939. if count=0 then
  1940. begin
  1941. { write a Nil reference }
  1942. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1943. exit;
  1944. end;
  1945. { first write the attribute list as a separate table }
  1946. current_asmdata.getglobaldatalabel(tbllab);
  1947. tbltcb:=ctai_typedconstbuilder.create([tcalo_is_lab,tcalo_make_dead_strippable]);
  1948. tbltcb.begin_anonymous_record(
  1949. internaltypeprefixName[itp_rtti_attr_list]+tostr(count),
  1950. defaultpacking,min(reqalign,SizeOf(PInt)),
  1951. targetinfos[target_info.system]^.alignment.recordalignmin);
  1952. tbltcb.emit_ord_const(count,u16inttype);
  1953. for i:=0 to count-1 do
  1954. begin
  1955. tbltcb.begin_anonymous_record(internaltypeprefixName[itp_rtti_attr_entry],defaultpacking,min(reqalign,SizeOf(PInt)),
  1956. targetinfos[target_info.system]^.alignment.recordalignmin);
  1957. attr:=trtti_attribute(attr_list.rtti_attributes[i]);
  1958. write_rtti_reference(tbltcb,ttypesym(attr.typesym).typedef,fullrtti);
  1959. tbltcb.emit_procdef_const(tprocdef(tcallnode(attr.constructorcall).procdefinition));
  1960. if not assigned(attr.constructorpd) then
  1961. internalerror(2019071004);
  1962. tbltcb.emit_procdef_const(tprocdef(attr.constructorpd));
  1963. write_args(tbltcb,attr);
  1964. tbltcb.end_anonymous_record;
  1965. end;
  1966. tbldef:=tbltcb.end_anonymous_record;
  1967. current_asmdata.asmlists[al_rtti].concatlist(
  1968. tbltcb.get_final_asmlist(tbllab,tbldef,sec_rodata,tbllab.name,const_align(sizeof(pint)))
  1969. );
  1970. tbltcb.free;
  1971. { write the reference to the attribute table }
  1972. tcb.emit_tai(Tai_const.Create_sym(tbllab),voidpointertype);
  1973. end;
  1974. function enumsym_compare_name(item1, item2: pointer): Integer;
  1975. var
  1976. enum1: tenumsym absolute item1;
  1977. enum2: tenumsym absolute item2;
  1978. begin
  1979. if enum1=enum2 then
  1980. result:=0
  1981. else if enum1.name>enum2.name then
  1982. result:=1
  1983. else
  1984. { there can't be equal names, identifiers are unique }
  1985. result:=-1;
  1986. end;
  1987. function enumsym_compare_value(item1, item2: pointer): Integer;
  1988. var
  1989. enum1: tenumsym absolute item1;
  1990. enum2: tenumsym absolute item2;
  1991. begin
  1992. if enum1.value>enum2.value then
  1993. result:=1
  1994. else if enum1.value<enum2.value then
  1995. result:=-1
  1996. else
  1997. result:=0;
  1998. end;
  1999. procedure TRTTIWriter.write_rtti_extrasyms(def:Tdef;rt:Trttitype;mainrtti:Tasmsymbol);
  2000. type Penumsym = ^Tenumsym;
  2001. { Writes a helper table for accelerated conversion of ordinal enum values to strings.
  2002. If you change something in this method, make sure to adapt the corresponding code
  2003. in sstrings.inc. }
  2004. procedure enumdef_rtti_ord2stringindex(rttidef: trecorddef; const syms: tfplist);
  2005. var rttilab:Tasmsymbol;
  2006. h,i,o,prev_value:longint;
  2007. mode:(lookup,search); {Modify with care, ordinal value of enum is written.}
  2008. r:single; {Must be real type because of integer overflow risk.}
  2009. tcb: ttai_typedconstbuilder;
  2010. sym_count: integer;
  2011. tabledef: tdef;
  2012. begin
  2013. {Decide wether a lookup array is size efficient.}
  2014. mode:=lookup;
  2015. sym_count:=syms.count;
  2016. if sym_count>0 then
  2017. begin
  2018. i:=1;
  2019. r:=0;
  2020. h:=tenumsym(syms[0]).value; {Next expected enum value is min.}
  2021. { set prev_value for the first iteration to a value that is
  2022. different from the first one without risking overflow (it's used
  2023. to detect whether two enum values are the same) }
  2024. if h=0 then
  2025. prev_value:=1
  2026. else
  2027. prev_value:=0;
  2028. while i<sym_count do
  2029. begin
  2030. { if two enum values are the same, we have to create a table }
  2031. if (prev_value=h) then
  2032. begin
  2033. mode:=search;
  2034. break;
  2035. end;
  2036. {Calculate size of hole between values. Avoid integer overflows.}
  2037. r:=r+(single(tenumsym(syms[i]).value)-single(h))-1;
  2038. prev_value:=h;
  2039. h:=tenumsym(syms[i]).value;
  2040. inc(i);
  2041. end;
  2042. if r>sym_count then
  2043. mode:=search; {Don't waste more than 50% space.}
  2044. end;
  2045. { write rtti data; make sure that the alignment matches the corresponding data structure
  2046. in the code that uses it (if alignment is required). }
  2047. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_data_force_indirect]);
  2048. { use TConstPtrUInt packrecords to ensure good alignment }
  2049. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  2050. targetinfos[target_info.system]^.alignment.recordalignmin);
  2051. { now emit the data: first the mode }
  2052. tcb.emit_tai(Tai_const.create_32bit(longint(mode)),u32inttype);
  2053. { align }
  2054. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,sizeof(PInt)),
  2055. targetinfos[target_info.system]^.alignment.recordalignmin);
  2056. if mode=lookup then
  2057. begin
  2058. o:=tenumsym(syms[0]).value; {Start with min value.}
  2059. for i:=0 to sym_count-1 do
  2060. begin
  2061. while o<tenumsym(syms[i]).value do
  2062. begin
  2063. tcb.emit_tai(Tai_const.create_nil_dataptr,ptruinttype);
  2064. inc(o);
  2065. end;
  2066. inc(o);
  2067. tcb.queue_init(voidpointertype);
  2068. tcb.queue_subscriptn_multiple_by_name(rttidef,
  2069. ['size_start_rec',
  2070. 'typ_union_rec',
  2071. 'min_max_rec',
  2072. 'basetype_array_rec',
  2073. tsym(syms[i]).Name]
  2074. );
  2075. tcb.queue_emit_asmsym(mainrtti,rttidef);
  2076. end;
  2077. end
  2078. else
  2079. begin
  2080. tcb.emit_ord_const(sym_count,u32inttype);
  2081. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,sizeof(PInt)),
  2082. targetinfos[target_info.system]^.alignment.recordalignmin);
  2083. for i:=0 to sym_count-1 do
  2084. begin
  2085. tcb.emit_ord_const(tenumsym(syms[i]).value,s32inttype);
  2086. tcb.queue_init(voidpointertype);
  2087. tcb.queue_subscriptn_multiple_by_name(rttidef,
  2088. ['size_start_rec',
  2089. 'typ_union_rec',
  2090. 'min_max_rec',
  2091. 'basetype_array_rec',
  2092. tsym(syms[i]).Name]
  2093. );
  2094. tcb.queue_emit_asmsym(mainrtti,rttidef);
  2095. end;
  2096. tcb.end_anonymous_record;
  2097. end;
  2098. tcb.end_anonymous_record;
  2099. tabledef:=tcb.end_anonymous_record;
  2100. rttilab:=current_asmdata.DefineAsmSymbol(Tstoreddef(def).rtti_mangledname(rt)+'_o2s',AB_GLOBAL,AT_DATA_NOINDIRECT,tabledef);
  2101. current_asmdata.asmlists[al_rtti].concatlist(tcb.get_final_asmlist(
  2102. rttilab,tabledef,sec_rodata,
  2103. rttilab.name,sizeof(PInt)));
  2104. tcb.free;
  2105. current_module.add_public_asmsym(rttilab);
  2106. end;
  2107. { Writes a helper table for accelerated conversion of string to ordinal enum values.
  2108. If you change something in this method, make sure to adapt the corresponding code
  2109. in sstrings.inc. }
  2110. procedure enumdef_rtti_string2ordindex(rttidef: trecorddef; const syms: tfplist);
  2111. var
  2112. tcb: ttai_typedconstbuilder;
  2113. rttilab: Tasmsymbol;
  2114. i:longint;
  2115. tabledef: tdef;
  2116. begin
  2117. { write rtti data }
  2118. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_data_force_indirect]);
  2119. { begin of Tstring_to_ord }
  2120. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,sizeof(PInt)),
  2121. targetinfos[target_info.system]^.alignment.recordalignmin);
  2122. tcb.emit_ord_const(syms.count,s32inttype);
  2123. { begin of "data" array in Tstring_to_ord }
  2124. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,sizeof(PInt)),
  2125. targetinfos[target_info.system]^.alignment.recordalignmin);
  2126. for i:=0 to syms.count-1 do
  2127. begin
  2128. tcb.emit_ord_const(tenumsym(syms[i]).value,s32inttype);
  2129. { alignment of pointer value handled by enclosing record already }
  2130. tcb.queue_init(voidpointertype);
  2131. tcb.queue_subscriptn_multiple_by_name(rttidef,
  2132. ['size_start_rec',
  2133. 'typ_union_rec',
  2134. 'min_max_rec',
  2135. 'basetype_array_rec',
  2136. tsym(syms[i]).Name]
  2137. );
  2138. tcb.queue_emit_asmsym(mainrtti,rttidef);
  2139. end;
  2140. tcb.end_anonymous_record;
  2141. tabledef:=tcb.end_anonymous_record;
  2142. rttilab:=current_asmdata.DefineAsmSymbol(Tstoreddef(def).rtti_mangledname(rt)+'_s2o',AB_GLOBAL,AT_DATA_NOINDIRECT,tabledef);
  2143. current_asmdata.asmlists[al_rtti].concatlist(tcb.get_final_asmlist(
  2144. rttilab,tabledef,sec_rodata,
  2145. rttilab.name,sizeof(PInt)));
  2146. tcb.free;
  2147. current_module.add_public_asmsym(rttilab);
  2148. end;
  2149. procedure enumdef_rtti_extrasyms(def:Tenumdef);
  2150. var
  2151. t:Tenumsym;
  2152. syms:tfplist;
  2153. i:longint;
  2154. rttitypesym: ttypesym;
  2155. rttidef: trecorddef;
  2156. s:TIDString;
  2157. begin
  2158. { collect enumsyms belonging to this enum type (could be a subsection
  2159. in case of a subrange type) }
  2160. syms:=tfplist.create;
  2161. for i := 0 to def.symtable.SymList.Count - 1 do
  2162. begin
  2163. t:=tenumsym(def.symtable.SymList[i]);
  2164. if t.value<def.minval then
  2165. continue
  2166. else
  2167. if t.value>def.maxval then
  2168. break;
  2169. syms.add(t);
  2170. end;
  2171. { sort the syms by enum name }
  2172. syms.sort(@enumsym_compare_name);
  2173. s:=internaltypeprefixName[itp_rttidef]+def.rtti_mangledname(fullrtti);
  2174. rttitypesym:=try_search_current_module_type(s);
  2175. if not assigned(rttitypesym) or
  2176. (ttypesym(rttitypesym).typedef.typ<>recorddef) then
  2177. internalerror(2015071402);
  2178. rttidef:=trecorddef(ttypesym(rttitypesym).typedef);
  2179. enumdef_rtti_string2ordindex(rttidef,syms);
  2180. { sort the syms by enum value }
  2181. syms.sort(@enumsym_compare_value);
  2182. enumdef_rtti_ord2stringindex(rttidef,syms);
  2183. syms.free;
  2184. end;
  2185. begin
  2186. case def.typ of
  2187. enumdef:
  2188. if rt=fullrtti then
  2189. begin
  2190. enumdef_rtti_extrasyms(Tenumdef(def));
  2191. end;
  2192. else
  2193. ;
  2194. end;
  2195. end;
  2196. procedure TRTTIWriter.write_child_rtti_data(def:tdef;rt:trttitype);
  2197. begin
  2198. case def.typ of
  2199. enumdef :
  2200. if assigned(tenumdef(def).basedef) then
  2201. write_rtti(tenumdef(def).basedef,rt);
  2202. setdef :
  2203. write_rtti(tsetdef(def).elementdef,rt);
  2204. arraydef :
  2205. begin
  2206. write_rtti(tarraydef(def).rangedef,rt);
  2207. write_rtti(tarraydef(def).elementdef,rt);
  2208. end;
  2209. recorddef :
  2210. begin
  2211. { guarantee initrtti for any record for RTTI purposes
  2212. also for fpc_initialize, fpc_finalize }
  2213. if (rt=fullrtti) then
  2214. begin
  2215. include(def.defstates,ds_init_table_used);
  2216. write_rtti(def, initrtti);
  2217. end;
  2218. fields_write_rtti(trecorddef(def).symtable,rt);
  2219. end;
  2220. objectdef :
  2221. begin
  2222. if assigned(tobjectdef(def).childof) then
  2223. write_rtti(tobjectdef(def).childof,rt);
  2224. if (rt=initrtti) or (tobjectdef(def).objecttype=odt_object) then
  2225. fields_write_rtti(tobjectdef(def).symtable,rt)
  2226. else
  2227. published_write_rtti(tobjectdef(def).symtable,rt);
  2228. if (rt=fullrtti) then
  2229. begin
  2230. { guarantee initrtti for any object for RTTI purposes
  2231. also for fpc_initialize, fpc_finalize }
  2232. if (tobjectdef(def).objecttype=odt_object) then
  2233. begin
  2234. include(def.defstates,ds_init_table_used);
  2235. write_rtti(def,initrtti);
  2236. end;
  2237. if (is_interface(def) or is_dispinterface(def))
  2238. and (oo_can_have_published in tobjectdef(def).objectoptions) then
  2239. methods_write_rtti(tobjectdef(def).symtable,rt,[vis_published],true);
  2240. end;
  2241. end;
  2242. classrefdef,
  2243. pointerdef:
  2244. if not is_objc_class_or_protocol(tabstractpointerdef(def).pointeddef) then
  2245. write_rtti(tabstractpointerdef(def).pointeddef,rt);
  2246. procvardef:
  2247. params_write_rtti(tabstractprocdef(def),rt,false);
  2248. else
  2249. ;
  2250. end;
  2251. end;
  2252. procedure TRTTIWriter.write_rtti_reference(tcb: ttai_typedconstbuilder; def: tdef; rt: trttitype);
  2253. begin
  2254. { we don't care about the real type here, because
  2255. a) we don't index into these elements
  2256. b) we may not have the rtti type available at the point that we
  2257. are emitting this data, because of forward definitions etc
  2258. c) if the rtti is emitted in another unit, we won't have the type
  2259. available at all
  2260. For the cases where the type is emitted in the current unit and hence
  2261. the underlying system will detect and complain about symbol def
  2262. mismatches, type conversions will have to be inserted afterwards (like
  2263. in llvm/llvmtype)
  2264. }
  2265. if not assigned(def) or is_void(def) or ((rt<>initrtti) and is_objc_class_or_protocol(def)) then
  2266. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype)
  2267. else
  2268. tcb.emit_tai(Tai_const.Create_sym(get_rtti_label(def,rt,true)),voidpointertype);
  2269. end;
  2270. function TRTTIWriter.ref_rtti(def:tdef;rt:trttitype;indirect:boolean;suffix:tsymstr):tasmsymbol;
  2271. var
  2272. s : tsymstr;
  2273. begin
  2274. s:=def.rtti_mangledname(rt)+suffix;
  2275. result:=current_asmdata.RefAsmSymbol(s,AT_DATA,indirect);
  2276. if def.owner.moduleid<>current_module.moduleid then
  2277. current_module.add_extern_asmsym(s,AB_EXTERNAL,AT_DATA);
  2278. end;
  2279. procedure TRTTIWriter.write_rtti(def:tdef;rt:trttitype);
  2280. var
  2281. tcb: ttai_typedconstbuilder;
  2282. opts : ttcasmlistoptions;
  2283. rttilab: tasmsymbol;
  2284. rttidef: tdef;
  2285. s: TIDString;
  2286. begin
  2287. { only write rtti of definitions from the current module }
  2288. if not findunitsymtable(def.owner).iscurrentunit then
  2289. exit;
  2290. { check if separate initrtti is actually needed }
  2291. if (rt=initrtti) and (not def.needs_separate_initrtti) then
  2292. rt:=fullrtti;
  2293. { prevent recursion }
  2294. if rttidefstate[rt] in def.defstates then
  2295. exit;
  2296. include(def.defstates,rttidefstate[rt]);
  2297. { write first all dependencies }
  2298. write_child_rtti_data(def,rt);
  2299. { write rtti data }
  2300. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_data_force_indirect]);
  2301. s:=internaltypeprefixName[itp_rttidef]+tstoreddef(def).rtti_mangledname(rt);
  2302. maybe_add_comment(tcb,'RTTI: begin '+def.GetTypeName+' ('+rttitypenames[rt]+')');
  2303. tcb.begin_anonymous_record(
  2304. s,
  2305. defaultpacking,reqalign,
  2306. targetinfos[target_info.system]^.alignment.recordalignmin
  2307. );
  2308. write_rtti_data(tcb,def,rt);
  2309. rttidef:=tcb.end_anonymous_record;
  2310. maybe_add_comment(tcb,'RTTI: end '+def.GetTypeName+' ('+rttitypenames[rt]+')');
  2311. rttilab:=current_asmdata.DefineAsmSymbol(tstoreddef(def).rtti_mangledname(rt),AB_GLOBAL,AT_DATA_NOINDIRECT,rttidef);
  2312. current_asmdata.AsmLists[al_rtti].concatList(
  2313. tcb.get_final_asmlist(rttilab,rttidef,sec_rodata,rttilab.name,min(target_info.alignment.maxCrecordalign,SizeOf(QWord))));
  2314. tcb.free;
  2315. current_module.add_public_asmsym(rttilab);
  2316. { write additional data }
  2317. write_rtti_extrasyms(def,rt,rttilab);
  2318. end;
  2319. procedure TRTTIWriter.maybe_add_comment(tcb:ttai_typedconstbuilder;const comment : string);
  2320. begin
  2321. if addcomments then
  2322. tcb.emit_comment(comment);
  2323. end;
  2324. constructor TRTTIWriter.create;
  2325. begin
  2326. addcomments:=cs_asm_rtti_source in current_settings.globalswitches;
  2327. if tf_requires_proper_alignment in target_info.flags then
  2328. begin
  2329. reqalign:=min(sizeof(QWord),target_info.alignment.maxCrecordalign);
  2330. defaultpacking:=C_alignment;
  2331. end
  2332. else
  2333. begin
  2334. reqalign:=1;
  2335. defaultpacking:=1;
  2336. end;
  2337. end;
  2338. end.