ncgrtti.pas 81 KB

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