ncgrtti.pas 103 KB

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