ncgrtti.pas 81 KB

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