2
0

ncgrtti.pas 102 KB

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