ncgrtti.pas 76 KB

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