ncgrtti.pas 102 KB

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