ncgrtti.pas 81 KB

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