ncgrtti.pas 79 KB

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