ncgrtti.pas 86 KB

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