2
0

ncgrtti.pas 81 KB

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