ncgrtti.pas 86 KB

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