ncgrtti.pas 72 KB

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