ncgrtti.pas 72 KB

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