ncgrtti.pas 93 KB

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