ncgrtti.pas 77 KB

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