ncgrtti.pas 81 KB

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