ncgrtti.pas 102 KB

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