ncgrtti.pas 78 KB

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