ncgrtti.pas 102 KB

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