ncgrtti.pas 103 KB

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