ncgrtti.pas 81 KB

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