ncgrtti.pas 76 KB

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