ncgrtti.pas 76 KB

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