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