ncgrtti.pas 92 KB

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