ncgrtti.pas 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648
  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. procedure writeaccessproc(tcb: ttai_typedconstbuilder; pap:tpropaccesslisttypes; shiftvalue : byte; unsetvalue: byte);
  846. var
  847. typvalue : byte;
  848. hp : ppropaccesslistitem;
  849. extnumber: longint;
  850. address,space : longint;
  851. def : tdef;
  852. hpropsym : tpropertysym;
  853. propaccesslist : tpropaccesslist;
  854. begin
  855. hpropsym:=tpropertysym(sym);
  856. repeat
  857. propaccesslist:=hpropsym.propaccesslist[pap];
  858. if not propaccesslist.empty then
  859. break;
  860. hpropsym:=hpropsym.overriddenpropsym;
  861. until not assigned(hpropsym);
  862. if not(assigned(propaccesslist) and assigned(propaccesslist.firstsym)) then
  863. begin
  864. tcb.emit_tai(Tai_const.Create_int_codeptr(unsetvalue),codeptruinttype);
  865. typvalue:=3;
  866. end
  867. { also for accessing class fields }
  868. else if propaccesslist.firstsym^.sym.typ=fieldvarsym then
  869. begin
  870. address:=0;
  871. hp:=propaccesslist.firstsym;
  872. def:=nil;
  873. while assigned(hp) do
  874. begin
  875. case hp^.sltype of
  876. sl_load :
  877. begin
  878. def:=tfieldvarsym(hp^.sym).vardef;
  879. inc(address,tfieldvarsym(hp^.sym).fieldoffset);
  880. end;
  881. sl_subscript :
  882. begin
  883. if not(assigned(def) and
  884. ((def.typ=recorddef) or
  885. is_object(def))) then
  886. internalerror(200402171);
  887. inc(address,tfieldvarsym(hp^.sym).fieldoffset);
  888. def:=tfieldvarsym(hp^.sym).vardef;
  889. end;
  890. sl_vec :
  891. begin
  892. if not(assigned(def) and (def.typ=arraydef)) then
  893. internalerror(200402172);
  894. def:=tarraydef(def).elementdef;
  895. {Hp.value is a Tconstexprint, which can be rather large,
  896. sanity check for longint overflow.}
  897. space:=(high(address)-address) div def.size;
  898. if int64(space)<hp^.value then
  899. internalerror(200706101);
  900. inc(address,int64(def.size*hp^.value));
  901. end;
  902. else
  903. internalerror(2019050523);
  904. end;
  905. hp:=hp^.next;
  906. end;
  907. tcb.emit_tai(Tai_const.Create_int_codeptr(address),codeptruinttype);
  908. typvalue:=0;
  909. end
  910. else
  911. begin
  912. { When there was an error then procdef is not assigned }
  913. if not assigned(propaccesslist.procdef) then
  914. exit;
  915. if not(po_virtualmethod in tprocdef(propaccesslist.procdef).procoptions) or
  916. is_objectpascal_helper(tprocdef(propaccesslist.procdef).struct) then
  917. begin
  918. tcb.queue_init(codeptruinttype);
  919. tcb.queue_emit_proc(tprocdef(propaccesslist.procdef));
  920. typvalue:=1;
  921. end
  922. else
  923. begin
  924. { virtual method, write vmt offset }
  925. extnumber:=tprocdef(propaccesslist.procdef).extnumber;
  926. tcb.emit_tai(Tai_const.Create_int_codeptr(
  927. tobjectdef(tprocdef(propaccesslist.procdef).struct).vmtmethodoffset(extnumber)),
  928. codeptruinttype);
  929. { register for wpo }
  930. tobjectdef(tprocdef(propaccesslist.procdef).struct).register_vmt_call(extnumber);
  931. {$ifdef vtentry}
  932. { not sure if we can insert those vtentry symbols safely here }
  933. {$error register methods used for published properties}
  934. {$endif vtentry}
  935. typvalue:=2;
  936. end;
  937. end;
  938. proctypesinfo:=proctypesinfo or (typvalue shl shiftvalue);
  939. end;
  940. function properties_count(st:tsymtable):longint;
  941. var
  942. i : longint;
  943. sym : tsym;
  944. begin
  945. result:=0;
  946. for i:=0 to st.SymList.Count-1 do
  947. begin
  948. sym:=tsym(st.SymList[i]);
  949. if (tsym(sym).typ=propertysym) and
  950. (sym.visibility in visibilities) then
  951. inc(result);
  952. end;
  953. end;
  954. function write_propinfo_data(tcb: ttai_typedconstbuilder; sym: tpropertysym): tdef;
  955. begin
  956. { we can only easily reuse defs if the property is not stored,
  957. because otherwise the rtti layout depends on how the "stored"
  958. is defined (field, indexed expression, virtual method, ...) }
  959. if not(ppo_stored in sym.propoptions) then
  960. propdefname:=internaltypeprefixName[itp_rtti_prop]+tostr(length(sym.realname))
  961. else
  962. propdefname:='';
  963. { TPropInfo is a packed record (even on targets that require
  964. alignment), but it starts aligned }
  965. if addcomments then
  966. if assigned(st.name) then
  967. tcb.emit_comment('RTTI: begin propinfo record '+sym.realname+' (class/rec: '+st.name^+')')
  968. else
  969. tcb.emit_comment('RTTI: begin propinfo record '+sym.realname+'(anon)');
  970. tcb.begin_anonymous_record(
  971. propdefname,
  972. 1,min(reqalign,SizeOf(PInt)),
  973. targetinfos[target_info.system]^.alignment.recordalignmin);
  974. if ppo_indexed in sym.propoptions then
  975. proctypesinfo:=$40
  976. else
  977. proctypesinfo:=0;
  978. if addcomments then
  979. tcb.emit_comment(#9'type info '+sym.Name+' (Type: '+sym.propdef.GetTypeName+')');
  980. write_rtti_reference(tcb,sym.propdef,fullrtti);
  981. if addcomments then
  982. tcb.emit_comment(#9'read access');
  983. writeaccessproc(tcb,palt_read,0,0);
  984. if addcomments then
  985. tcb.emit_comment(#9'write access');
  986. writeaccessproc(tcb,palt_write,2,0);
  987. { is it stored ? }
  988. if addcomments then
  989. tcb.emit_comment(#9'stored ?');
  990. if not(ppo_stored in sym.propoptions) then
  991. begin
  992. { no, so put a constant zero }
  993. tcb.emit_tai(Tai_const.Create_nil_codeptr,codeptruinttype);
  994. proctypesinfo:=proctypesinfo or (3 shl 4);
  995. end
  996. else
  997. writeaccessproc(tcb,palt_stored,4,1); { maybe; if no procedure put a constant 1 (=true) }
  998. if addcomments then
  999. tcb.emit_comment(#9'Index');
  1000. tcb.emit_ord_const(sym.index,u32inttype);
  1001. if addcomments then
  1002. tcb.emit_comment(#9'default');
  1003. tcb.emit_ord_const(sym.default,u32inttype);
  1004. propnameitem:=TPropNameListItem(propnamelist.Find(sym.name));
  1005. if not assigned(propnameitem) then
  1006. internalerror(200512201);
  1007. if addcomments then
  1008. tcb.emit_comment(#9'property index');
  1009. tcb.emit_ord_const(propnameitem.propindex,u16inttype);
  1010. if addcomments then
  1011. tcb.emit_comment(#9'proc types');
  1012. tcb.emit_ord_const(proctypesinfo,u8inttype);
  1013. { write reference to attribute table }
  1014. if addcomments then
  1015. tcb.emit_comment(#9'attributes');
  1016. write_attribute_data(tcb,sym.rtti_attribute_list);
  1017. { write property name }
  1018. if addcomments then
  1019. tcb.emit_comment(#9'name');
  1020. tcb.emit_shortstring_const(sym.realname);
  1021. result:=tcb.end_anonymous_record;
  1022. if addcomments then
  1023. tcb.emit_comment('RTTI: End propinfo record '+sym.realname);
  1024. end;
  1025. begin
  1026. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,SizeOf(PInt)),
  1027. targetinfos[target_info.system]^.alignment.recordalignmin);
  1028. if extended_rtti then
  1029. maybe_add_comment(tcb,'RTTI: Extended property data: Property count')
  1030. else
  1031. maybe_add_comment(tcb,'RTTI: Legacy property data: Property count');
  1032. tcb.emit_ord_const(properties_count(st),u16inttype);
  1033. for i:=0 to st.SymList.Count-1 do
  1034. begin
  1035. sym:=tsym(st.SymList[i]);
  1036. if (sym.typ=propertysym) and
  1037. (sym.visibility in visibilities) then
  1038. begin
  1039. if extended_rtti then
  1040. begin
  1041. {
  1042. TPropInfoEx = record
  1043. Flags: Byte;
  1044. Info: PPropInfo;
  1045. // AttrData: TAttrData
  1046. end;
  1047. }
  1048. maybe_add_comment(tcb,'RTTI: begin property '+sym.prettyname);
  1049. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,SizeOf(PInt)),
  1050. targetinfos[target_info.system]^.alignment.recordalignmin);
  1051. { write visiblity flags for extended RTTI }
  1052. maybe_add_comment(tcb,#9'visibility flags');
  1053. tcb.emit_ord_const(byte(visibility_to_rtti_flags(sym.visibility)),u8inttype);
  1054. { create separate constant builder }
  1055. current_asmdata.getglobaldatalabel(tbllab);
  1056. tbltcb:=ctai_typedconstbuilder.create([tcalo_is_lab,tcalo_make_dead_strippable]);
  1057. { write TPropInfo record }
  1058. tbldef:=write_propinfo_data(tbltcb,tpropertysym(sym));
  1059. current_asmdata.asmlists[al_rtti].concatlist(
  1060. tbltcb.get_final_asmlist(tbllab,tbldef,sec_rodata,tbllab.name,const_align(sizeof(pint)))
  1061. );
  1062. tbltcb.free;
  1063. { write the pointer to the prop info }
  1064. maybe_add_comment(tcb,#9'property info reference');
  1065. tcb.emit_tai(Tai_const.Create_sym(tbllab),voidpointertype);
  1066. { end record }
  1067. tcb.end_anonymous_record;
  1068. maybe_add_comment(tcb,'RTTI: end property '+sym.prettyname);
  1069. end
  1070. else
  1071. write_propinfo_data(tcb,tpropertysym(sym));
  1072. end;
  1073. end;
  1074. tcb.end_anonymous_record;
  1075. end;
  1076. procedure TRTTIWriter.write_rtti_data(tcb: ttai_typedconstbuilder; def: tdef; rt: trttitype);
  1077. procedure unknown_rtti(def:tstoreddef);
  1078. begin
  1079. tcb.emit_ord_const(tkUnknown,u8inttype);
  1080. write_rtti_name(tcb,def);
  1081. write_common_rtti_data(tcb,def,rt);
  1082. end;
  1083. procedure variantdef_rtti(def:tvariantdef);
  1084. begin
  1085. write_header(tcb,def,tkVariant);
  1086. write_common_rtti_data(tcb,def,rt);
  1087. end;
  1088. procedure stringdef_rtti(def:tstringdef);
  1089. const
  1090. string_typekinds: array[tstringtype] of byte = (
  1091. tkSString,tkLString,tkAString,tkWString,tkUString
  1092. );
  1093. begin
  1094. write_header(tcb,def,string_typekinds[def.stringtype]);
  1095. tcb.begin_anonymous_record(
  1096. internaltypeprefixName[itp_rtti_outer]+tostr(string_typekinds[def.stringtype]),
  1097. defaultpacking,reqalign,
  1098. targetinfos[target_info.system]^.alignment.recordalignmin);
  1099. write_common_rtti_data(tcb,def,rt);
  1100. case def.stringtype of
  1101. st_ansistring:
  1102. begin
  1103. tcb.begin_anonymous_record(
  1104. internaltypeprefixName[itp_rtti_case]+tostr(string_typekinds[def.stringtype]),
  1105. defaultpacking,reqalign,
  1106. targetinfos[target_info.system]^.alignment.recordalignmin);
  1107. tcb.emit_ord_const(def.encoding,u16inttype);
  1108. tcb.end_anonymous_record;
  1109. end;
  1110. st_shortstring:
  1111. begin
  1112. tcb.begin_anonymous_record(
  1113. internaltypeprefixName[itp_rtti_case]+tostr(string_typekinds[def.stringtype]),
  1114. defaultpacking,reqalign,
  1115. targetinfos[target_info.system]^.alignment.recordalignmin);
  1116. tcb.emit_ord_const(def.len,u8inttype);
  1117. tcb.end_anonymous_record;
  1118. end;
  1119. st_widestring,
  1120. st_unicodestring,
  1121. st_longstring:
  1122. { nothing }
  1123. ;
  1124. end;
  1125. tcb.end_anonymous_record;
  1126. end;
  1127. procedure enumdef_rtti(def: tenumdef);
  1128. var
  1129. i : integer;
  1130. hp : tenumsym;
  1131. begin
  1132. write_header(tcb,def,tkEnumeration);
  1133. { align; the named fields are so that we can let the compiler
  1134. calculate the string offsets later on }
  1135. tcb.next_field_name:='size_start_rec';
  1136. { add a typename so that it can be reused when writing the the s2o
  1137. and o2s arrays for llvm (otherwise we have to write out the entire
  1138. type definition every time we access an element from this record) }
  1139. tcb.begin_anonymous_record(internaltypeprefixName[itp_rtti_enum_size_start_rec]+def.unique_id_str,defaultpacking,reqalign,
  1140. targetinfos[target_info.system]^.alignment.recordalignmin);
  1141. write_common_rtti_data(tcb,def,rt);
  1142. tcb.next_field_name:='typ_union_rec';
  1143. tcb.begin_anonymous_record(internaltypeprefixName[itp_rtti_enum_size_start_rec2]+def.unique_id_str,defaultpacking,reqalign,
  1144. targetinfos[target_info.system]^.alignment.recordalignmin);
  1145. case longint(def.size) of
  1146. 1 :
  1147. tcb.emit_ord_const(otUByte,u8inttype);
  1148. 2 :
  1149. tcb.emit_ord_const(otUWord,u8inttype);
  1150. 4 :
  1151. tcb.emit_ord_const(otULong,u8inttype);
  1152. end;
  1153. { we need to align by Tconstptruint here to satisfy the alignment
  1154. rules set by records: in the typinfo unit we overlay a TTypeData
  1155. record on this data, which at the innermost variant record needs an
  1156. alignment of TConstPtrUint due to e.g. the "CompType" member for
  1157. tkSet (also the "BaseType" member for tkEnumeration).
  1158. We need to adhere to this, otherwise things will break. }
  1159. tcb.next_field_name:='min_max_rec';
  1160. tcb.begin_anonymous_record(internaltypeprefixName[itp_rtti_enum_min_max_rec]+def.unique_id_str,defaultpacking,reqalign,
  1161. targetinfos[target_info.system]^.alignment.recordalignmin);
  1162. tcb.emit_ord_const(def.min,s32inttype);
  1163. tcb.emit_ord_const(def.max,s32inttype);
  1164. tcb.next_field_name:='basetype_array_rec';
  1165. { all strings must appear right after each other -> from now on
  1166. packrecords 1 (but the start must still be aligned) }
  1167. tcb.begin_anonymous_record(internaltypeprefixName[itp_rtti_enum_basetype_array_rec]+def.unique_id_str,1,reqalign,
  1168. targetinfos[target_info.system]^.alignment.recordalignmin);
  1169. { write base type }
  1170. write_rtti_reference(tcb,def.basedef,rt);
  1171. for i:=0 to def.symtable.SymList.Count-1 do
  1172. begin
  1173. hp:=tenumsym(def.symtable.SymList[i]);
  1174. if hp.value<def.minval then
  1175. continue
  1176. else
  1177. if hp.value>def.maxval then
  1178. break;
  1179. tcb.next_field_name:=hp.name;
  1180. tcb.emit_shortstring_const(hp.realname);
  1181. end;
  1182. { write unit name }
  1183. tcb.emit_shortstring_const(current_module.realmodulename^);
  1184. { write zero which is required by RTL }
  1185. tcb.emit_ord_const(0,u8inttype);
  1186. { terminate all records }
  1187. tcb.end_anonymous_record;
  1188. tcb.end_anonymous_record;
  1189. tcb.end_anonymous_record;
  1190. tcb.end_anonymous_record;
  1191. end;
  1192. procedure orddef_rtti(def:torddef);
  1193. procedure doint32_64(typekind: byte;min,max:int64);
  1194. const
  1195. trans : array[tordtype] of byte =
  1196. (otUByte{otNone},
  1197. otUByte,otUWord,otULong,otUQWord,otUByte{otNone},
  1198. otSByte,otSWord,otSLong,otSQWord,otUByte{otNone},
  1199. otUByte,otUByte,otUWord,otULong,otUQWord,
  1200. otSByte,otSWord,otSLong,otSQWord,
  1201. otUByte,otUWord,otUByte,255);
  1202. var
  1203. elesize: string[1];
  1204. deftrans: byte;
  1205. begin
  1206. write_header(tcb,def,typekind);
  1207. deftrans:=trans[def.ordtype];
  1208. case deftrans of
  1209. otUQWord,
  1210. otSQWord:
  1211. elesize:='8';
  1212. 255:
  1213. begin
  1214. if def.packedbitsize<=32 then
  1215. begin
  1216. elesize:='4';
  1217. if def.low<0 then
  1218. deftrans:=otSLong
  1219. else
  1220. deftrans:=otULong;
  1221. end
  1222. else
  1223. begin
  1224. elesize:='8';
  1225. if def.low<0 then
  1226. deftrans:=otSQWord
  1227. else
  1228. deftrans:=otUQWord;
  1229. end;
  1230. end
  1231. else
  1232. elesize:='4'
  1233. end;
  1234. tcb.begin_anonymous_record(
  1235. internaltypeprefixName[itp_rtti_ord_outer]+elesize,
  1236. defaultpacking,reqalign,
  1237. targetinfos[target_info.system]^.alignment.recordalignmin);
  1238. write_common_rtti_data(tcb,def,rt);
  1239. tcb.begin_anonymous_record(
  1240. internaltypeprefixName[itp_rtti_ord_middle]+elesize,
  1241. defaultpacking,reqalign,
  1242. targetinfos[target_info.system]^.alignment.recordalignmin);
  1243. tcb.emit_ord_const(byte(trans[def.ordtype]),u8inttype);
  1244. tcb.begin_anonymous_record(
  1245. internaltypeprefixName[itp_rtti_ord_inner]+elesize,
  1246. defaultpacking,reqalign,
  1247. targetinfos[target_info.system]^.alignment.recordalignmin);
  1248. {Convert to longint to smuggle values in high(longint)+1..high(cardinal) into asmlist.}
  1249. case deftrans of
  1250. otUQWord:
  1251. begin
  1252. tcb.emit_ord_const(min,u64inttype);
  1253. tcb.emit_ord_const(max,u64inttype);
  1254. end;
  1255. otSQWord:
  1256. begin
  1257. tcb.emit_ord_const(min,s64inttype);
  1258. tcb.emit_ord_const(max,s64inttype);
  1259. end;
  1260. else
  1261. begin
  1262. tcb.emit_ord_const(longint(min),s32inttype);
  1263. tcb.emit_ord_const(longint(max),s32inttype);
  1264. end;
  1265. end;
  1266. tcb.end_anonymous_record;
  1267. tcb.end_anonymous_record;
  1268. tcb.end_anonymous_record;
  1269. end;
  1270. procedure dointeger(typekind:byte);inline;
  1271. begin
  1272. doint32_64(typekind,int64(def.low.svalue),int64(def.high.svalue));
  1273. end;
  1274. begin
  1275. case def.ordtype of
  1276. s64bit :
  1277. dointeger(tkInt64);
  1278. u64bit :
  1279. dointeger(tkQWord);
  1280. pasbool1,
  1281. pasbool8,
  1282. pasbool16,
  1283. pasbool32,
  1284. pasbool64:
  1285. dointeger(tkBool);
  1286. { use different low/high values to be Delphi compatible }
  1287. bool8bit,
  1288. bool16bit,
  1289. bool32bit:
  1290. doint32_64(tkBool,longint(low(longint)),longint(high(longint)));
  1291. bool64bit:
  1292. doint32_64(tkBool,low(int64),high(int64));
  1293. uchar:
  1294. dointeger(tkChar);
  1295. uwidechar:
  1296. dointeger(tkWChar);
  1297. scurrency:
  1298. begin
  1299. write_header(tcb,def,tkFloat);
  1300. tcb.begin_anonymous_record(
  1301. internaltypeprefixName[itp_1byte],
  1302. defaultpacking,reqalign,
  1303. targetinfos[target_info.system]^.alignment.recordalignmin);
  1304. write_common_rtti_data(tcb,def,rt);
  1305. tcb.emit_ord_const(ftCurr,u8inttype);
  1306. tcb.end_anonymous_record;
  1307. end;
  1308. else
  1309. dointeger(tkInteger);
  1310. end;
  1311. end;
  1312. procedure floatdef_rtti(def:tfloatdef);
  1313. const
  1314. {tfloattype = (s32real,s64real,s80real,sc80real,s64bit,s128bit);}
  1315. translate : array[tfloattype] of byte =
  1316. (ftSingle,ftDouble,ftExtended,ftExtended,ftComp,ftCurr,ftFloat128);
  1317. begin
  1318. write_header(tcb,def,tkFloat);
  1319. tcb.begin_anonymous_record(
  1320. internaltypeprefixName[itp_1byte],
  1321. defaultpacking,reqalign,
  1322. targetinfos[target_info.system]^.alignment.recordalignmin);
  1323. write_common_rtti_data(tcb,def,rt);
  1324. tcb.begin_anonymous_record(
  1325. internaltypeprefixName[itp_rtti_float],
  1326. defaultpacking,reqalign,
  1327. targetinfos[target_info.system]^.alignment.recordalignmin);
  1328. tcb.emit_ord_const(translate[def.floattype],u8inttype);
  1329. tcb.end_anonymous_record;
  1330. tcb.end_anonymous_record;
  1331. end;
  1332. procedure setdef_rtti(def:tsetdef);
  1333. begin
  1334. write_header(tcb,def,tkSet);
  1335. tcb.begin_anonymous_record(
  1336. internaltypeprefixName[itp_rtti_set_outer],
  1337. defaultpacking,reqalign,
  1338. targetinfos[target_info.system]^.alignment.recordalignmin);
  1339. write_common_rtti_data(tcb,def,rt);
  1340. tcb.begin_anonymous_record(
  1341. internaltypeprefixName[itp_rtti_set_middle],
  1342. defaultpacking,reqalign,
  1343. targetinfos[target_info.system]^.alignment.recordalignmin);
  1344. case def.size of
  1345. 1:
  1346. tcb.emit_ord_const(otUByte,u8inttype);
  1347. 2:
  1348. tcb.emit_ord_const(otUWord,u8inttype);
  1349. 4:
  1350. tcb.emit_ord_const(otULong,u8inttype);
  1351. else
  1352. tcb.emit_ord_const(otUByte,u8inttype);
  1353. end;
  1354. tcb.begin_anonymous_record(
  1355. internaltypeprefixName[itp_rtti_set_inner],
  1356. defaultpacking,reqalign,
  1357. targetinfos[target_info.system]^.alignment.recordalignmin);
  1358. tcb.emit_ord_const(def.size,sizesinttype);
  1359. write_rtti_reference(tcb,def.elementdef,rt);
  1360. tcb.end_anonymous_record;
  1361. tcb.end_anonymous_record;
  1362. tcb.end_anonymous_record;
  1363. end;
  1364. procedure arraydef_rtti(def:tarraydef);
  1365. var
  1366. i,dimcount: byte;
  1367. totalcount: asizeuint;
  1368. finaldef: tdef;
  1369. curdef:tarraydef;
  1370. begin
  1371. if ado_IsDynamicArray in def.arrayoptions then
  1372. tcb.emit_ord_const(tkDynArray,u8inttype)
  1373. else
  1374. tcb.emit_ord_const(tkArray,u8inttype);
  1375. write_rtti_name(tcb,def);
  1376. if not(ado_IsDynamicArray in def.arrayoptions) then
  1377. begin
  1378. { remember tha last instruction. we will need to insert some
  1379. calculated values after it }
  1380. finaldef:=def;
  1381. totalcount:=1;
  1382. dimcount:=0;
  1383. repeat
  1384. curdef:=tarraydef(finaldef);
  1385. finaldef:=curdef.elementdef;
  1386. { Dims[i] PTypeInfo }
  1387. inc(dimcount);
  1388. totalcount:=totalcount*curdef.elecount;
  1389. until (finaldef.typ<>arraydef) or
  1390. (ado_IsDynamicArray in tarraydef(finaldef).arrayoptions);
  1391. tcb.begin_anonymous_record(
  1392. internaltypeprefixName[itp_rtti_normal_array]+tostr(dimcount),
  1393. defaultpacking,reqalign,
  1394. targetinfos[target_info.system]^.alignment.recordalignmin);
  1395. write_common_rtti_data(tcb,def,rt);
  1396. tcb.begin_anonymous_record(internaltypeprefixName[itp_rtti_normal_array_inner]+tostr(dimcount),
  1397. defaultpacking,reqalign,
  1398. targetinfos[target_info.system]^.alignment.recordalignmin);
  1399. { total size = elecount * elesize of the first arraydef }
  1400. tcb.emit_tai(Tai_const.Create_sizeint(def.elecount*def.elesize),sizeuinttype);
  1401. { total element count }
  1402. tcb.emit_tai(Tai_const.Create_sizeint(asizeint(totalcount)),sizeuinttype);
  1403. { last dimension element type }
  1404. tcb.emit_tai(Tai_const.Create_sym(get_rtti_label(curdef.elementdef,rt,true)),voidpointertype);
  1405. { dimension count }
  1406. tcb.emit_ord_const(dimcount,u8inttype);
  1407. finaldef:=def;
  1408. { ranges of the dimensions }
  1409. for i:=1 to dimcount do
  1410. begin
  1411. curdef:=tarraydef(finaldef);
  1412. finaldef:=curdef.elementdef;
  1413. { Dims[i] PPTypeInfo }
  1414. write_rtti_reference(tcb,curdef.rangedef,rt);
  1415. end;
  1416. end
  1417. else
  1418. { write a delphi almost compatible dyn. array entry:
  1419. there are two types, eltype and eltype2, the latter is nil if the element type needs
  1420. no finalization, the former is always valid, delphi has this swapped, but for
  1421. compatibility with older fpc versions we do it different, to be delphi compatible,
  1422. the names are swapped in typinfo.pp
  1423. }
  1424. begin
  1425. tcb.begin_anonymous_record(
  1426. internaltypeprefixName[itp_rtti_dyn_array],
  1427. defaultpacking,reqalign,
  1428. targetinfos[target_info.system]^.alignment.recordalignmin);
  1429. write_common_rtti_data(tcb,def,rt);
  1430. { record in TypInfo is aligned differently from init rtti }
  1431. tcb.begin_anonymous_record(
  1432. internaltypeprefixName[itp_rtti_dyn_array_inner],
  1433. defaultpacking,reqalign,
  1434. targetinfos[target_info.system]^.alignment.recordalignmin);
  1435. { size of elements }
  1436. tcb.emit_tai(Tai_const.Create_sizeint(def.elesize),sizeuinttype);
  1437. { element type }
  1438. write_rtti_reference(tcb,def.elementdef,rt);
  1439. { variant type }
  1440. tcb.emit_ord_const(tstoreddef(def.elementdef).getvardef,s32inttype);
  1441. { element type }
  1442. if def.elementdef.needs_inittable then
  1443. write_rtti_reference(tcb,def.elementdef,rt)
  1444. else
  1445. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1446. { write unit name }
  1447. tcb.emit_shortstring_const(current_module.realmodulename^);
  1448. end;
  1449. tcb.end_anonymous_record;
  1450. tcb.end_anonymous_record;
  1451. end;
  1452. procedure classrefdef_rtti(def:tclassrefdef);
  1453. begin
  1454. write_header(tcb,def,tkClassRef);
  1455. tcb.begin_anonymous_record(
  1456. internaltypeprefixName[itp_rtti_ref],
  1457. defaultpacking,reqalign,
  1458. targetinfos[target_info.system]^.alignment.recordalignmin);
  1459. write_common_rtti_data(tcb,def,rt);
  1460. tcb.begin_anonymous_record(
  1461. internaltypeprefixName[itp_rtti_classref],
  1462. defaultpacking,reqalign,
  1463. targetinfos[target_info.system]^.alignment.recordalignmin);
  1464. write_rtti_reference(tcb,def.pointeddef,rt);
  1465. tcb.end_anonymous_record;
  1466. tcb.end_anonymous_record;
  1467. end;
  1468. procedure pointerdef_rtti(def:tpointerdef);
  1469. begin
  1470. write_header(tcb,def,tkPointer);
  1471. tcb.begin_anonymous_record(
  1472. internaltypeprefixName[itp_rtti_ref],
  1473. defaultpacking,reqalign,
  1474. targetinfos[target_info.system]^.alignment.recordalignmin);
  1475. write_common_rtti_data(tcb,def,rt);
  1476. tcb.begin_anonymous_record(
  1477. internaltypeprefixName[itp_rtti_pointer],
  1478. defaultpacking,reqalign,
  1479. targetinfos[target_info.system]^.alignment.recordalignmin);
  1480. write_rtti_reference(tcb,def.pointeddef,rt);
  1481. tcb.end_anonymous_record;
  1482. tcb.end_anonymous_record;
  1483. end;
  1484. procedure recorddef_rtti(def:trecorddef);
  1485. procedure write_record_operators(rttilab:tasmlabel);
  1486. var
  1487. rttidef: tdef;
  1488. tcb: ttai_typedconstbuilder;
  1489. mop: tmanagementoperator;
  1490. procdef: tprocdef;
  1491. begin
  1492. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable]);
  1493. tcb.begin_anonymous_record(
  1494. '',
  1495. defaultpacking,min(reqalign,SizeOf(PInt)),
  1496. targetinfos[target_info.system]^.alignment.recordalignmin
  1497. );
  1498. { use "succ" to omit first enum item "mop_none" }
  1499. for mop := succ(low(tmanagementoperator)) to high(tmanagementoperator) do
  1500. begin
  1501. if not (mop in trecordsymtable(def.symtable).managementoperators) then
  1502. tcb.emit_tai(Tai_const.Create_nil_codeptr,voidcodepointertype)
  1503. else
  1504. begin
  1505. procdef := search_management_operator(mop, def);
  1506. if procdef = nil then
  1507. internalerror(201603021)
  1508. else
  1509. tcb.emit_tai(Tai_const.Createname(procdef.mangledname,AT_FUNCTION,0),
  1510. cprocvardef.getreusableprocaddr(procdef,pc_address_only));
  1511. end;
  1512. end;
  1513. rttidef := tcb.end_anonymous_record;
  1514. current_asmdata.AsmLists[al_rtti].concatList(
  1515. tcb.get_final_asmlist(rttilab,rttidef,sec_rodata,rttilab.name,
  1516. sizeof(PInt)));
  1517. tcb.free;
  1518. end;
  1519. procedure write_extended_property_table;
  1520. var
  1521. propnamelist: TFPHashObjectList;
  1522. visibilities: tvisibilities;
  1523. begin
  1524. propnamelist:=TFPHashObjectList.Create;
  1525. visibilities:=def.rtti_visibilities_for_option(ro_properties);
  1526. collect_propnamelist(propnamelist,def,visibilities);
  1527. properties_write_rtti_data(tcb,propnamelist,def.symtable,true,visibilities);
  1528. propnamelist.free;
  1529. end;
  1530. var
  1531. oplab : tasmlabel;
  1532. begin
  1533. write_header(tcb,def,tkRecord);
  1534. { need extra reqalign record, because otherwise the u32 int will
  1535. only be aligned to 4 even on 64 bit target (while the rtti code
  1536. in typinfo expects alignments to sizeof(pointer)) }
  1537. tcb.begin_anonymous_record('',
  1538. defaultpacking,reqalign,
  1539. targetinfos[target_info.system]^.alignment.recordalignmin);
  1540. write_common_rtti_data(tcb,def,rt);
  1541. tcb.begin_anonymous_record('',
  1542. defaultpacking,reqalign,
  1543. targetinfos[target_info.system]^.alignment.recordalignmin);
  1544. { store special terminator for init table for more optimal rtl operations
  1545. strictly related to RecordRTTI procedure in rtti.inc (directly
  1546. related to RTTIRecordRttiInfoToInitInfo function) }
  1547. if rt=initrtti then
  1548. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype)
  1549. else
  1550. { we use a direct reference as the init RTTI is always in the same
  1551. unit as the full RTTI }
  1552. tcb.emit_tai(Tai_const.Create_sym(get_rtti_label(def,initrtti,false)),voidpointertype);
  1553. tcb.emit_ord_const(def.size,u32inttype);
  1554. oplab:=nil;
  1555. { store rtti management operators only for init table }
  1556. if rt=initrtti then
  1557. begin
  1558. { for now records don't have the initializer table }
  1559. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1560. if (trecordsymtable(def.symtable).managementoperators=[]) then
  1561. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype)
  1562. else
  1563. begin
  1564. current_asmdata.getlocaldatalabel(oplab);
  1565. tcb.emit_tai(Tai_const.Createname(
  1566. oplab.name,
  1567. AT_DATA_FORCEINDIRECT,0),voidpointertype);
  1568. end;
  1569. end;
  1570. fields_write_rtti_data(tcb,def,rt);
  1571. { write extended rtti }
  1572. if rt=fullrtti then
  1573. begin
  1574. write_extended_field_table(tcb,def,defaultpacking);
  1575. write_extended_method_table(tcb,def,defaultpacking);
  1576. write_extended_property_table;
  1577. end;
  1578. tcb.end_anonymous_record;
  1579. tcb.end_anonymous_record;
  1580. { write pointers to operators if needed }
  1581. if (rt=initrtti) and (trecordsymtable(def.symtable).managementoperators<>[]) then
  1582. write_record_operators(oplab);
  1583. end;
  1584. procedure procvardef_rtti(def:tprocvardef);
  1585. procedure write_para(parasym:tparavarsym);
  1586. begin
  1587. { write flags for current parameter }
  1588. write_param_flag(tcb,parasym);
  1589. { write name of current parameter }
  1590. tcb.emit_shortstring_const(parasym.realname);
  1591. { write name of type of current parameter }
  1592. write_rtti_name(tcb,parasym.vardef);
  1593. end;
  1594. procedure write_procedure_param(parasym:tparavarsym);
  1595. begin
  1596. { every parameter is expected to start aligned }
  1597. tcb.begin_anonymous_record(
  1598. internaltypeprefixName[itp_rtti_proc_param]+tostr(length(parasym.realname)),
  1599. defaultpacking,min(reqalign,SizeOf(PInt)),
  1600. targetinfos[target_info.system]^.alignment.recordalignmin);
  1601. { write flags for current parameter }
  1602. write_param_flag(tcb,parasym);
  1603. { write param type }
  1604. if is_open_array(parasym.vardef) or is_array_of_const(parasym.vardef) then
  1605. write_rtti_reference(tcb,tarraydef(parasym.vardef).elementdef,fullrtti)
  1606. else if parasym.vardef=cformaltype then
  1607. write_rtti_reference(tcb,nil,fullrtti)
  1608. else
  1609. write_rtti_reference(tcb,parasym.vardef,fullrtti);
  1610. { write name of current parameter }
  1611. tcb.emit_shortstring_const(parasym.realname);
  1612. tcb.end_anonymous_record;
  1613. end;
  1614. var
  1615. methodkind : byte;
  1616. i : integer;
  1617. begin
  1618. if po_methodpointer in def.procoptions then
  1619. begin
  1620. { write method id and name }
  1621. write_header(tcb,def,tkMethod);
  1622. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1623. targetinfos[target_info.system]^.alignment.recordalignmin);
  1624. write_common_rtti_data(tcb,def,rt);
  1625. tcb.begin_anonymous_record('',
  1626. defaultpacking,reqalign,
  1627. targetinfos[target_info.system]^.alignment.recordalignmin);
  1628. { write kind of method }
  1629. methodkind:=write_methodkind(tcb,def);
  1630. { write parameter info. The parameters must be written in reverse order
  1631. if this method uses right to left parameter pushing! }
  1632. tcb.emit_ord_const(def.paras.count,u8inttype);
  1633. for i:=0 to def.paras.count-1 do
  1634. write_para(tparavarsym(def.paras[i]));
  1635. if (methodkind=mkFunction) or (methodkind=mkClassFunction) then
  1636. begin
  1637. { write name of result type }
  1638. write_rtti_name(tcb,def.returndef);
  1639. { enclosing record takes care of alignment }
  1640. { write result typeinfo }
  1641. write_rtti_reference(tcb,def.returndef,fullrtti);
  1642. end;
  1643. { write calling convention }
  1644. write_callconv(tcb,def);
  1645. { enclosing record takes care of alignment }
  1646. { write params typeinfo }
  1647. for i:=0 to def.paras.count-1 do
  1648. begin
  1649. if is_open_array(tparavarsym(def.paras[i]).vardef) or is_array_of_const(tparavarsym(def.paras[i]).vardef) then
  1650. write_rtti_reference(tcb,tarraydef(tparavarsym(def.paras[i]).vardef).elementdef,fullrtti)
  1651. else if tparavarsym(def.paras[i]).vardef=cformaltype then
  1652. write_rtti_reference(tcb,nil,fullrtti)
  1653. else
  1654. write_rtti_reference(tcb,tparavarsym(def.paras[i]).vardef,fullrtti);
  1655. end;
  1656. tcb.end_anonymous_record;
  1657. tcb.end_anonymous_record;
  1658. end
  1659. else
  1660. begin
  1661. write_header(tcb,def,tkProcvar);
  1662. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1663. targetinfos[target_info.system]^.alignment.recordalignmin);
  1664. write_common_rtti_data(tcb,def,rt);
  1665. tcb.begin_anonymous_record('',
  1666. defaultpacking,reqalign,
  1667. targetinfos[target_info.system]^.alignment.recordalignmin);
  1668. { flags }
  1669. tcb.emit_ord_const(0,u8inttype);
  1670. { write calling convention }
  1671. write_callconv(tcb,def);
  1672. { enclosing record takes care of alignment }
  1673. { write result typeinfo }
  1674. write_rtti_reference(tcb,def.returndef,fullrtti);
  1675. { write parameter count }
  1676. tcb.emit_ord_const(def.paras.count,u8inttype);
  1677. for i:=0 to def.paras.count-1 do
  1678. write_procedure_param(tparavarsym(def.paras[i]));
  1679. tcb.end_anonymous_record;
  1680. tcb.end_anonymous_record;
  1681. end;
  1682. end;
  1683. procedure objectdef_rtti(def: tobjectdef);
  1684. procedure objectdef_rtti_fields(def:tobjectdef);
  1685. begin
  1686. maybe_add_comment(tcb,'RTTI begin fields '+def.objname^);
  1687. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1688. targetinfos[target_info.system]^.alignment.recordalignmin);
  1689. { - for compatiblity with record RTTI we need to write a terminator-
  1690. Nil pointer for initrtti as well for objects
  1691. - for RTTI consistency for objects we need point from fullrtti
  1692. to initrtti
  1693. - classes are assumed to have the same INIT RTTI as records
  1694. (see TObject.CleanupInstance)
  1695. - neither helper nor class type have fullrtti for fields
  1696. }
  1697. if (rt=initrtti) then
  1698. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype)
  1699. else
  1700. if (def.objecttype=odt_object) then
  1701. tcb.emit_tai(Tai_const.Create_sym(get_rtti_label(def,initrtti,false)),voidpointertype)
  1702. else
  1703. internalerror(2017011801);
  1704. tcb.emit_ord_const(def.size, u32inttype);
  1705. { pointer to management operators available only for initrtti }
  1706. if (rt=initrtti) then
  1707. begin
  1708. { initializer table only available for classes currently }
  1709. if def.objecttype=odt_class then
  1710. write_mop_offset_table(tcb,def,mop_initialize)
  1711. else
  1712. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1713. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1714. end;
  1715. { enclosing record takes care of alignment }
  1716. fields_write_rtti_data(tcb,def,rt);
  1717. tcb.end_anonymous_record;
  1718. maybe_add_comment(tcb,'RTTI end fields '+def.objname^);
  1719. end;
  1720. procedure objectdef_rtti_interface_init(def:tobjectdef);
  1721. begin
  1722. tcb.emit_ord_const(def.size, u32inttype);
  1723. end;
  1724. procedure objectdef_extended_rtti_class(def:tobjectdef);
  1725. var
  1726. propnamelist : TFPHashObjectList;
  1727. visibilities : tvisibilities;
  1728. begin
  1729. propnamelist:=TFPHashObjectList.Create;
  1730. visibilities:=def.rtti_visibilities_for_option(ro_properties);
  1731. collect_propnamelist(propnamelist,def,visibilities);
  1732. properties_write_rtti_data(tcb,propnamelist,def.symtable,true,visibilities);
  1733. propnamelist.free;
  1734. end;
  1735. procedure objectdef_rtti_class_full(def:tobjectdef);
  1736. var
  1737. propnamelist : TFPHashObjectList;
  1738. begin
  1739. { Collect unique property names with nameindex }
  1740. propnamelist:=TFPHashObjectList.Create;
  1741. collect_propnamelist(propnamelist,def,[vis_published]);
  1742. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1743. targetinfos[target_info.system]^.alignment.recordalignmin);
  1744. if not is_objectpascal_helper(def) then
  1745. begin
  1746. maybe_add_comment(tcb,#9'Parent type info');
  1747. if (oo_has_vmt in def.objectoptions) then
  1748. tcb.emit_tai(
  1749. Tai_const.Createname(def.vmt_mangledname,AT_DATA_FORCEINDIRECT,0),
  1750. cpointerdef.getreusable(def.vmt_def))
  1751. else
  1752. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1753. end;
  1754. { write parent typeinfo }
  1755. maybe_add_comment(tcb,#9'Parent type info');
  1756. write_rtti_reference(tcb,def.childof,fullrtti);
  1757. { write typeinfo of extended type }
  1758. if is_objectpascal_helper(def) then
  1759. if assigned(def.extendeddef) then
  1760. begin
  1761. maybe_add_comment(tcb,#9'helper for type info');
  1762. write_rtti_reference(tcb,def.extendeddef,fullrtti)
  1763. end
  1764. else
  1765. InternalError(2011033001);
  1766. { total number of unique properties }
  1767. maybe_add_comment(tcb,#9'Number of properties');
  1768. tcb.emit_ord_const(propnamelist.count,u16inttype);
  1769. { write unit name }
  1770. maybe_add_comment(tcb, #9'Unit name');
  1771. tcb.emit_shortstring_const(current_module.realmodulename^);
  1772. { write published properties for this object }
  1773. properties_write_rtti_data(tcb,propnamelist,def.symtable,false,[vis_published]);
  1774. { write extended properties }
  1775. objectdef_extended_rtti_class(def);
  1776. tcb.end_anonymous_record;
  1777. propnamelist.free;
  1778. end;
  1779. procedure objectdef_rtti_interface_full(def:tobjectdef);
  1780. var
  1781. propnamelist : TFPHashObjectList;
  1782. { if changed to a set, make sure it's still a byte large, and
  1783. swap appropriately when cross-compiling
  1784. }
  1785. IntfFlags: byte;
  1786. begin
  1787. { Collect unique property names with nameindex }
  1788. propnamelist:=TFPHashObjectList.Create;
  1789. collect_propnamelist(propnamelist,def,[vis_published]);
  1790. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1791. targetinfos[target_info.system]^.alignment.recordalignmin);
  1792. { write parent typeinfo }
  1793. write_rtti_reference(tcb,def.childof,fullrtti);
  1794. { interface: write flags, iid and iidstr }
  1795. IntfFlags:=0;
  1796. if assigned(def.iidguid) then
  1797. IntfFlags:=IntfFlags or (1 shl ord(ifHasGuid));
  1798. if (def.objecttype=odt_interfacecorba) and (def.iidstr^<>'') then
  1799. IntfFlags:=IntfFlags or (1 shl ord(ifHasStrGUID));
  1800. if (def.objecttype=odt_dispinterface) then
  1801. IntfFlags:=IntfFlags or (1 shl ord(ifDispInterface));
  1802. if (target_info.endian=endian_big) then
  1803. IntfFlags:=reverse_byte(IntfFlags);
  1804. {
  1805. ifDispatch, }
  1806. tcb.emit_ord_const(IntfFlags,u8inttype);
  1807. { write GUID }
  1808. tcb.emit_guid_const(def.iidguid^);
  1809. { write hidden class reference - if it is nil, write_rtti_reference writes nil }
  1810. write_rtti_reference(tcb,def.hiddenclassdef,fullrtti);
  1811. { write unit name }
  1812. tcb.emit_shortstring_const(current_module.realmodulename^);
  1813. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1814. targetinfos[target_info.system]^.alignment.recordalignmin);
  1815. { write iidstr }
  1816. if def.objecttype=odt_interfacecorba then
  1817. begin
  1818. { prepareguid always allocates an empty string }
  1819. if not assigned(def.iidstr) then
  1820. internalerror(2016021901);
  1821. tcb.emit_shortstring_const(def.iidstr^)
  1822. end;
  1823. { write published properties for this object }
  1824. properties_write_rtti_data(tcb,propnamelist,def.symtable,false,[vis_published]);
  1825. { write published methods for this interface }
  1826. write_methods(tcb,def.symtable,false,[vis_published]);
  1827. tcb.end_anonymous_record;
  1828. tcb.end_anonymous_record;
  1829. propnamelist.free;
  1830. end;
  1831. begin
  1832. case def.objecttype of
  1833. odt_class:
  1834. tcb.emit_ord_const(tkclass,u8inttype);
  1835. odt_object:
  1836. tcb.emit_ord_const(tkobject,u8inttype);
  1837. odt_dispinterface,
  1838. odt_interfacecom:
  1839. tcb.emit_ord_const(tkInterface,u8inttype);
  1840. odt_interfacecorba:
  1841. tcb.emit_ord_const(tkinterfaceCorba,u8inttype);
  1842. odt_helper:
  1843. tcb.emit_ord_const(tkhelper,u8inttype);
  1844. else
  1845. internalerror(200611034);
  1846. end;
  1847. { generate the name }
  1848. tcb.emit_shortstring_const(def.objrealname^);
  1849. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1850. targetinfos[target_info.system]^.alignment.recordalignmin);
  1851. write_common_rtti_data(tcb,def,rt);
  1852. case rt of
  1853. initrtti :
  1854. begin
  1855. if def.objecttype in [odt_class,odt_object,odt_helper] then
  1856. objectdef_rtti_fields(def)
  1857. else
  1858. objectdef_rtti_interface_init(def);
  1859. end;
  1860. fullrtti :
  1861. begin
  1862. tcb.begin_anonymous_record('',
  1863. defaultpacking,reqalign,
  1864. targetinfos[target_info.system]^.alignment.recordalignmin);
  1865. case def.objecttype of
  1866. odt_helper,
  1867. odt_class:
  1868. objectdef_rtti_class_full(def);
  1869. odt_object:
  1870. objectdef_rtti_fields(def);
  1871. else
  1872. objectdef_rtti_interface_full(def);
  1873. end;
  1874. tcb.end_anonymous_record;
  1875. end;
  1876. else
  1877. ;
  1878. end;
  1879. tcb.end_anonymous_record;
  1880. end;
  1881. begin
  1882. case def.typ of
  1883. variantdef :
  1884. variantdef_rtti(tvariantdef(def));
  1885. stringdef :
  1886. stringdef_rtti(tstringdef(def));
  1887. enumdef :
  1888. enumdef_rtti(tenumdef(def));
  1889. orddef :
  1890. orddef_rtti(torddef(def));
  1891. floatdef :
  1892. floatdef_rtti(tfloatdef(def));
  1893. setdef :
  1894. setdef_rtti(tsetdef(def));
  1895. procvardef :
  1896. procvardef_rtti(tprocvardef(def));
  1897. arraydef :
  1898. begin
  1899. if ado_IsBitPacked in tarraydef(def).arrayoptions then
  1900. unknown_rtti(tstoreddef(def))
  1901. else
  1902. arraydef_rtti(tarraydef(def));
  1903. end;
  1904. recorddef :
  1905. begin
  1906. if trecorddef(def).is_packed then
  1907. unknown_rtti(tstoreddef(def))
  1908. else
  1909. recorddef_rtti(trecorddef(def));
  1910. end;
  1911. objectdef :
  1912. objectdef_rtti(tobjectdef(def));
  1913. classrefdef :
  1914. classrefdef_rtti(tclassrefdef(def));
  1915. pointerdef :
  1916. pointerdef_rtti(tpointerdef(def));
  1917. else
  1918. unknown_rtti(tstoreddef(def));
  1919. end;
  1920. end;
  1921. procedure TRTTIWriter.write_attribute_data(tcb:ttai_typedconstbuilder;attr_list:trtti_attribute_list);
  1922. procedure write_args(tbltcb:ttai_typedconstbuilder;attr:trtti_attribute);
  1923. var
  1924. argtcb : ttai_typedconstbuilder;
  1925. arglab : tasmlabel;
  1926. argdef : tdef;
  1927. i : sizeint;
  1928. begin
  1929. if length(attr.paras)=0 then
  1930. begin
  1931. tbltcb.emit_tai(tai_const.Create_16bit(0),u16inttype);
  1932. tbltcb.emit_tai(tai_const.Create_nil_dataptr,voidpointertype);
  1933. end
  1934. else
  1935. begin
  1936. current_asmdata.getglobaldatalabel(arglab);
  1937. argtcb:=ctai_typedconstbuilder.create([tcalo_is_lab,tcalo_make_dead_strippable]);
  1938. argtcb.begin_anonymous_record('',defaultpacking,min(reqalign,SizeOf(PInt)),
  1939. targetinfos[target_info.system]^.alignment.recordalignmin);
  1940. for i:=0 to High(attr.paras) do
  1941. begin
  1942. case attr.paras[i].nodetype of
  1943. niln,
  1944. ordconstn,
  1945. realconstn,
  1946. stringconstn,
  1947. pointerconstn,
  1948. guidconstn:
  1949. tconstnode(attr.paras[i]).emit_data(argtcb);
  1950. setconstn:
  1951. tsetconstnode(attr.paras[i]).emit_data(argtcb);
  1952. else
  1953. internalerror(2019070803);
  1954. end;
  1955. end;
  1956. argdef:=argtcb.end_anonymous_record;
  1957. current_asmdata.asmlists[al_rtti].concatlist(
  1958. argtcb.get_final_asmlist(arglab,argdef,sec_rodata,arglab.name,const_align(sizeof(pint)))
  1959. );
  1960. argtcb.free;
  1961. { write argument size and the reference to the argument entry }
  1962. tbltcb.emit_ord_const(argdef.size,u16inttype);
  1963. tbltcb.emit_tai(Tai_const.Create_sym(arglab),voidpointertype);
  1964. end;
  1965. end;
  1966. var
  1967. count,i: word;
  1968. attr : trtti_attribute;
  1969. tbltcb : ttai_typedconstbuilder;
  1970. tbllab : tasmlabel;
  1971. tbldef : tdef;
  1972. begin
  1973. if assigned(attr_list) then
  1974. count:=attr_list.get_attribute_count
  1975. else
  1976. count:=0;
  1977. if count=0 then
  1978. begin
  1979. { write a Nil reference }
  1980. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1981. exit;
  1982. end;
  1983. { first write the attribute list as a separate table }
  1984. current_asmdata.getglobaldatalabel(tbllab);
  1985. tbltcb:=ctai_typedconstbuilder.create([tcalo_is_lab,tcalo_make_dead_strippable]);
  1986. tbltcb.begin_anonymous_record(
  1987. internaltypeprefixName[itp_rtti_attr_list]+tostr(count),
  1988. defaultpacking,min(reqalign,SizeOf(PInt)),
  1989. targetinfos[target_info.system]^.alignment.recordalignmin);
  1990. tbltcb.emit_ord_const(count,u16inttype);
  1991. for i:=0 to count-1 do
  1992. begin
  1993. tbltcb.begin_anonymous_record(internaltypeprefixName[itp_rtti_attr_entry],defaultpacking,min(reqalign,SizeOf(PInt)),
  1994. targetinfos[target_info.system]^.alignment.recordalignmin);
  1995. attr:=trtti_attribute(attr_list.rtti_attributes[i]);
  1996. write_rtti_reference(tbltcb,ttypesym(attr.typesym).typedef,fullrtti);
  1997. tbltcb.emit_procdef_const(tprocdef(tcallnode(attr.constructorcall).procdefinition));
  1998. if not assigned(attr.constructorpd) then
  1999. internalerror(2019071004);
  2000. tbltcb.emit_procdef_const(tprocdef(attr.constructorpd));
  2001. write_args(tbltcb,attr);
  2002. tbltcb.end_anonymous_record;
  2003. end;
  2004. tbldef:=tbltcb.end_anonymous_record;
  2005. current_asmdata.asmlists[al_rtti].concatlist(
  2006. tbltcb.get_final_asmlist(tbllab,tbldef,sec_rodata,tbllab.name,const_align(sizeof(pint)))
  2007. );
  2008. tbltcb.free;
  2009. { write the reference to the attribute table }
  2010. tcb.emit_tai(Tai_const.Create_sym(tbllab),voidpointertype);
  2011. end;
  2012. function enumsym_compare_name(item1, item2: pointer): Integer;
  2013. var
  2014. enum1: tenumsym absolute item1;
  2015. enum2: tenumsym absolute item2;
  2016. begin
  2017. if enum1=enum2 then
  2018. result:=0
  2019. else if enum1.name>enum2.name then
  2020. result:=1
  2021. else
  2022. { there can't be equal names, identifiers are unique }
  2023. result:=-1;
  2024. end;
  2025. function enumsym_compare_value(item1, item2: pointer): Integer;
  2026. var
  2027. enum1: tenumsym absolute item1;
  2028. enum2: tenumsym absolute item2;
  2029. begin
  2030. if enum1.value>enum2.value then
  2031. result:=1
  2032. else if enum1.value<enum2.value then
  2033. result:=-1
  2034. else
  2035. result:=0;
  2036. end;
  2037. procedure TRTTIWriter.write_rtti_extrasyms(def:Tdef;rt:Trttitype;mainrtti:Tasmsymbol);
  2038. type Penumsym = ^Tenumsym;
  2039. { Writes a helper table for accelerated conversion of ordinal enum values to strings.
  2040. If you change something in this method, make sure to adapt the corresponding code
  2041. in sstrings.inc. }
  2042. procedure enumdef_rtti_ord2stringindex(rttidef: trecorddef; const syms: tfplist);
  2043. var rttilab:Tasmsymbol;
  2044. h,i,o,prev_value:longint;
  2045. mode:(lookup,search); {Modify with care, ordinal value of enum is written.}
  2046. r:single; {Must be real type because of integer overflow risk.}
  2047. tcb: ttai_typedconstbuilder;
  2048. sym_count: integer;
  2049. tabledef: tdef;
  2050. begin
  2051. {Decide wether a lookup array is size efficient.}
  2052. mode:=lookup;
  2053. sym_count:=syms.count;
  2054. if sym_count>0 then
  2055. begin
  2056. i:=1;
  2057. r:=0;
  2058. h:=tenumsym(syms[0]).value; {Next expected enum value is min.}
  2059. { set prev_value for the first iteration to a value that is
  2060. different from the first one without risking overflow (it's used
  2061. to detect whether two enum values are the same) }
  2062. if h=0 then
  2063. prev_value:=1
  2064. else
  2065. prev_value:=0;
  2066. while i<sym_count do
  2067. begin
  2068. { if two enum values are the same, we have to create a table }
  2069. if (prev_value=h) then
  2070. begin
  2071. mode:=search;
  2072. break;
  2073. end;
  2074. {Calculate size of hole between values. Avoid integer overflows.}
  2075. r:=r+(single(tenumsym(syms[i]).value)-single(h))-1;
  2076. prev_value:=h;
  2077. h:=tenumsym(syms[i]).value;
  2078. inc(i);
  2079. end;
  2080. if r>sym_count then
  2081. mode:=search; {Don't waste more than 50% space.}
  2082. end;
  2083. { write rtti data; make sure that the alignment matches the corresponding data structure
  2084. in the code that uses it (if alignment is required). }
  2085. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_data_force_indirect]);
  2086. { use TConstPtrUInt packrecords to ensure good alignment }
  2087. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  2088. targetinfos[target_info.system]^.alignment.recordalignmin);
  2089. { now emit the data: first the mode }
  2090. tcb.emit_tai(Tai_const.create_32bit(longint(mode)),u32inttype);
  2091. { align }
  2092. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,sizeof(PInt)),
  2093. targetinfos[target_info.system]^.alignment.recordalignmin);
  2094. if mode=lookup then
  2095. begin
  2096. o:=tenumsym(syms[0]).value; {Start with min value.}
  2097. for i:=0 to sym_count-1 do
  2098. begin
  2099. while o<tenumsym(syms[i]).value do
  2100. begin
  2101. tcb.emit_tai(Tai_const.create_nil_dataptr,ptruinttype);
  2102. inc(o);
  2103. end;
  2104. inc(o);
  2105. tcb.queue_init(voidpointertype);
  2106. tcb.queue_subscriptn_multiple_by_name(rttidef,
  2107. ['size_start_rec',
  2108. 'typ_union_rec',
  2109. 'min_max_rec',
  2110. 'basetype_array_rec',
  2111. tsym(syms[i]).Name]
  2112. );
  2113. tcb.queue_emit_asmsym(mainrtti,rttidef);
  2114. end;
  2115. end
  2116. else
  2117. begin
  2118. tcb.emit_ord_const(sym_count,u32inttype);
  2119. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,sizeof(PInt)),
  2120. targetinfos[target_info.system]^.alignment.recordalignmin);
  2121. for i:=0 to sym_count-1 do
  2122. begin
  2123. tcb.emit_ord_const(tenumsym(syms[i]).value,s32inttype);
  2124. tcb.queue_init(voidpointertype);
  2125. tcb.queue_subscriptn_multiple_by_name(rttidef,
  2126. ['size_start_rec',
  2127. 'typ_union_rec',
  2128. 'min_max_rec',
  2129. 'basetype_array_rec',
  2130. tsym(syms[i]).Name]
  2131. );
  2132. tcb.queue_emit_asmsym(mainrtti,rttidef);
  2133. end;
  2134. tcb.end_anonymous_record;
  2135. end;
  2136. tcb.end_anonymous_record;
  2137. tabledef:=tcb.end_anonymous_record;
  2138. rttilab:=current_asmdata.DefineAsmSymbol(Tstoreddef(def).rtti_mangledname(rt)+'_o2s',AB_GLOBAL,AT_DATA_NOINDIRECT,tabledef);
  2139. current_asmdata.asmlists[al_rtti].concatlist(tcb.get_final_asmlist(
  2140. rttilab,tabledef,sec_rodata,
  2141. rttilab.name,sizeof(PInt)));
  2142. tcb.free;
  2143. current_module.add_public_asmsym(rttilab);
  2144. end;
  2145. { Writes a helper table for accelerated conversion of string to ordinal enum values.
  2146. If you change something in this method, make sure to adapt the corresponding code
  2147. in sstrings.inc. }
  2148. procedure enumdef_rtti_string2ordindex(rttidef: trecorddef; const syms: tfplist);
  2149. var
  2150. tcb: ttai_typedconstbuilder;
  2151. rttilab: Tasmsymbol;
  2152. i:longint;
  2153. tabledef: tdef;
  2154. begin
  2155. { write rtti data }
  2156. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_data_force_indirect]);
  2157. { begin of Tstring_to_ord }
  2158. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,sizeof(PInt)),
  2159. targetinfos[target_info.system]^.alignment.recordalignmin);
  2160. tcb.emit_ord_const(syms.count,s32inttype);
  2161. { begin of "data" array in Tstring_to_ord }
  2162. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,sizeof(PInt)),
  2163. targetinfos[target_info.system]^.alignment.recordalignmin);
  2164. for i:=0 to syms.count-1 do
  2165. begin
  2166. tcb.emit_ord_const(tenumsym(syms[i]).value,s32inttype);
  2167. { alignment of pointer value handled by enclosing record already }
  2168. tcb.queue_init(voidpointertype);
  2169. tcb.queue_subscriptn_multiple_by_name(rttidef,
  2170. ['size_start_rec',
  2171. 'typ_union_rec',
  2172. 'min_max_rec',
  2173. 'basetype_array_rec',
  2174. tsym(syms[i]).Name]
  2175. );
  2176. tcb.queue_emit_asmsym(mainrtti,rttidef);
  2177. end;
  2178. tcb.end_anonymous_record;
  2179. tabledef:=tcb.end_anonymous_record;
  2180. rttilab:=current_asmdata.DefineAsmSymbol(Tstoreddef(def).rtti_mangledname(rt)+'_s2o',AB_GLOBAL,AT_DATA_NOINDIRECT,tabledef);
  2181. current_asmdata.asmlists[al_rtti].concatlist(tcb.get_final_asmlist(
  2182. rttilab,tabledef,sec_rodata,
  2183. rttilab.name,sizeof(PInt)));
  2184. tcb.free;
  2185. current_module.add_public_asmsym(rttilab);
  2186. end;
  2187. procedure enumdef_rtti_extrasyms(def:Tenumdef);
  2188. var
  2189. t:Tenumsym;
  2190. syms:tfplist;
  2191. i:longint;
  2192. rttitypesym: ttypesym;
  2193. rttidef: trecorddef;
  2194. s:TIDString;
  2195. begin
  2196. { collect enumsyms belonging to this enum type (could be a subsection
  2197. in case of a subrange type) }
  2198. syms:=tfplist.create;
  2199. for i := 0 to def.symtable.SymList.Count - 1 do
  2200. begin
  2201. t:=tenumsym(def.symtable.SymList[i]);
  2202. if t.value<def.minval then
  2203. continue
  2204. else
  2205. if t.value>def.maxval then
  2206. break;
  2207. syms.add(t);
  2208. end;
  2209. { sort the syms by enum name }
  2210. syms.sort(@enumsym_compare_name);
  2211. s:=internaltypeprefixName[itp_rttidef]+def.rtti_mangledname(fullrtti);
  2212. rttitypesym:=try_search_current_module_type(s);
  2213. if not assigned(rttitypesym) or
  2214. (ttypesym(rttitypesym).typedef.typ<>recorddef) then
  2215. internalerror(2015071402);
  2216. rttidef:=trecorddef(ttypesym(rttitypesym).typedef);
  2217. enumdef_rtti_string2ordindex(rttidef,syms);
  2218. { sort the syms by enum value }
  2219. syms.sort(@enumsym_compare_value);
  2220. enumdef_rtti_ord2stringindex(rttidef,syms);
  2221. syms.free;
  2222. end;
  2223. begin
  2224. case def.typ of
  2225. enumdef:
  2226. if rt=fullrtti then
  2227. begin
  2228. enumdef_rtti_extrasyms(Tenumdef(def));
  2229. end;
  2230. else
  2231. ;
  2232. end;
  2233. end;
  2234. procedure TRTTIWriter.write_child_rtti_data(def:tdef;rt:trttitype);
  2235. begin
  2236. case def.typ of
  2237. enumdef :
  2238. if assigned(tenumdef(def).basedef) then
  2239. write_rtti(tenumdef(def).basedef,rt);
  2240. setdef :
  2241. write_rtti(tsetdef(def).elementdef,rt);
  2242. arraydef :
  2243. begin
  2244. write_rtti(tarraydef(def).rangedef,rt);
  2245. write_rtti(tarraydef(def).elementdef,rt);
  2246. end;
  2247. recorddef :
  2248. begin
  2249. { guarantee initrtti for any record for RTTI purposes
  2250. also for fpc_initialize, fpc_finalize }
  2251. if (rt=fullrtti) then
  2252. begin
  2253. include(def.defstates,ds_init_table_used);
  2254. write_rtti(def, initrtti);
  2255. end;
  2256. fields_write_rtti(trecorddef(def).symtable,rt);
  2257. end;
  2258. objectdef :
  2259. begin
  2260. if assigned(tobjectdef(def).childof) then
  2261. write_rtti(tobjectdef(def).childof,rt);
  2262. if (rt=initrtti) or (tobjectdef(def).objecttype=odt_object) then
  2263. fields_write_rtti(tobjectdef(def).symtable,rt)
  2264. else
  2265. published_write_rtti(tobjectdef(def),rt);
  2266. if (rt=fullrtti) then
  2267. begin
  2268. { guarantee initrtti for any object for RTTI purposes
  2269. also for fpc_initialize, fpc_finalize }
  2270. if (tobjectdef(def).objecttype=odt_object) then
  2271. begin
  2272. include(def.defstates,ds_init_table_used);
  2273. write_rtti(def,initrtti);
  2274. end;
  2275. if (is_interface(def) or is_dispinterface(def))
  2276. and (oo_can_have_published in tobjectdef(def).objectoptions) then
  2277. methods_write_rtti(tobjectdef(def).symtable,rt,[vis_published],true);
  2278. end;
  2279. end;
  2280. classrefdef,
  2281. pointerdef:
  2282. if not is_objc_class_or_protocol(tabstractpointerdef(def).pointeddef) then
  2283. write_rtti(tabstractpointerdef(def).pointeddef,rt);
  2284. procvardef:
  2285. params_write_rtti(tabstractprocdef(def),rt,false);
  2286. else
  2287. ;
  2288. end;
  2289. end;
  2290. procedure TRTTIWriter.write_rtti_reference(tcb: ttai_typedconstbuilder; def: tdef; rt: trttitype);
  2291. begin
  2292. { we don't care about the real type here, because
  2293. a) we don't index into these elements
  2294. b) we may not have the rtti type available at the point that we
  2295. are emitting this data, because of forward definitions etc
  2296. c) if the rtti is emitted in another unit, we won't have the type
  2297. available at all
  2298. For the cases where the type is emitted in the current unit and hence
  2299. the underlying system will detect and complain about symbol def
  2300. mismatches, type conversions will have to be inserted afterwards (like
  2301. in llvm/llvmtype)
  2302. }
  2303. if not assigned(def) or is_void(def) or ((rt<>initrtti) and is_objc_class_or_protocol(def)) then
  2304. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype)
  2305. else
  2306. tcb.emit_tai(Tai_const.Create_sym(get_rtti_label(def,rt,true)),voidpointertype);
  2307. end;
  2308. function TRTTIWriter.ref_rtti(def:tdef;rt:trttitype;indirect:boolean;suffix:tsymstr):tasmsymbol;
  2309. var
  2310. s : tsymstr;
  2311. begin
  2312. s:=def.rtti_mangledname(rt)+suffix;
  2313. result:=current_asmdata.RefAsmSymbol(s,AT_DATA,indirect);
  2314. if def.owner.moduleid<>current_module.moduleid then
  2315. current_module.add_extern_asmsym(s,AB_EXTERNAL,AT_DATA);
  2316. end;
  2317. procedure TRTTIWriter.write_rtti(def:tdef;rt:trttitype);
  2318. var
  2319. tcb: ttai_typedconstbuilder;
  2320. opts : ttcasmlistoptions;
  2321. rttilab: tasmsymbol;
  2322. rttidef: tdef;
  2323. s: TIDString;
  2324. begin
  2325. { only write rtti of definitions from the current module }
  2326. if not findunitsymtable(def.owner).iscurrentunit then
  2327. exit;
  2328. { check if separate initrtti is actually needed }
  2329. if (rt=initrtti) and (not def.needs_separate_initrtti) then
  2330. rt:=fullrtti;
  2331. { prevent recursion }
  2332. if rttidefstate[rt] in def.defstates then
  2333. exit;
  2334. include(def.defstates,rttidefstate[rt]);
  2335. { write first all dependencies }
  2336. write_child_rtti_data(def,rt);
  2337. { write rtti data }
  2338. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_data_force_indirect]);
  2339. s:=internaltypeprefixName[itp_rttidef]+tstoreddef(def).rtti_mangledname(rt);
  2340. maybe_add_comment(tcb,'RTTI: begin Type '+def.GetTypeName+' ('+rttitypenames[rt]+')');
  2341. tcb.begin_anonymous_record(
  2342. s,
  2343. defaultpacking,reqalign,
  2344. targetinfos[target_info.system]^.alignment.recordalignmin
  2345. );
  2346. write_rtti_data(tcb,def,rt);
  2347. rttidef:=tcb.end_anonymous_record;
  2348. maybe_add_comment(tcb,'RTTI: end Type '+def.GetTypeName+' ('+rttitypenames[rt]+')');
  2349. rttilab:=current_asmdata.DefineAsmSymbol(tstoreddef(def).rtti_mangledname(rt),AB_GLOBAL,AT_DATA_NOINDIRECT,rttidef);
  2350. current_asmdata.AsmLists[al_rtti].concatList(
  2351. tcb.get_final_asmlist(rttilab,rttidef,sec_rodata,rttilab.name,min(target_info.alignment.maxCrecordalign,SizeOf(QWord))));
  2352. tcb.free;
  2353. current_module.add_public_asmsym(rttilab);
  2354. { write additional data }
  2355. write_rtti_extrasyms(def,rt,rttilab);
  2356. end;
  2357. procedure TRTTIWriter.maybe_add_comment(tcb:ttai_typedconstbuilder;const comment : string);
  2358. begin
  2359. if addcomments then
  2360. tcb.emit_comment(comment);
  2361. end;
  2362. constructor TRTTIWriter.create;
  2363. begin
  2364. addcomments:=cs_asm_rtti_source in current_settings.globalswitches;
  2365. if tf_requires_proper_alignment in target_info.flags then
  2366. begin
  2367. reqalign:=min(sizeof(QWord),target_info.alignment.maxCrecordalign);
  2368. defaultpacking:=C_alignment;
  2369. end
  2370. else
  2371. begin
  2372. reqalign:=1;
  2373. defaultpacking:=1;
  2374. end;
  2375. end;
  2376. end.