ncgrtti.pas 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950
  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_rtti_set_outer],
  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.begin_anonymous_record(
  926. internaltypeprefixName[itp_rtti_set_inner],
  927. defaultpacking,reqalign,
  928. targetinfos[target_info.system]^.alignment.recordalignmin,
  929. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  930. tcb.emit_ord_const(def.size,sizesinttype);
  931. write_rtti_reference(tcb,def.elementdef,rt);
  932. tcb.end_anonymous_record;
  933. tcb.end_anonymous_record;
  934. end;
  935. procedure arraydef_rtti(def:tarraydef);
  936. var
  937. i,dimcount: byte;
  938. totalcount: asizeuint;
  939. finaldef: tdef;
  940. curdef:tarraydef;
  941. begin
  942. if ado_IsDynamicArray in def.arrayoptions then
  943. tcb.emit_ord_const(tkDynArray,u8inttype)
  944. else
  945. tcb.emit_ord_const(tkArray,u8inttype);
  946. write_rtti_name(tcb,def);
  947. if not(ado_IsDynamicArray in def.arrayoptions) then
  948. begin
  949. { remember tha last instruction. we will need to insert some
  950. calculated values after it }
  951. finaldef:=def;
  952. totalcount:=1;
  953. dimcount:=0;
  954. repeat
  955. curdef:=tarraydef(finaldef);
  956. finaldef:=curdef.elementdef;
  957. { Dims[i] PTypeInfo }
  958. inc(dimcount);
  959. totalcount:=totalcount*curdef.elecount;
  960. until (finaldef.typ<>arraydef) or
  961. (ado_IsDynamicArray in tarraydef(finaldef).arrayoptions);
  962. tcb.begin_anonymous_record(
  963. internaltypeprefixName[itp_rtti_normal_array]+tostr(dimcount),
  964. defaultpacking,reqalign,
  965. targetinfos[target_info.system]^.alignment.recordalignmin,
  966. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  967. { total size = elecount * elesize of the first arraydef }
  968. tcb.emit_tai(Tai_const.Create_sizeint(def.elecount*def.elesize),sizeuinttype);
  969. { total element count }
  970. tcb.emit_tai(Tai_const.Create_sizeint(asizeint(totalcount)),sizeuinttype);
  971. { last dimension element type }
  972. tcb.emit_tai(Tai_const.Create_sym(get_rtti_label(curdef.elementdef,rt,true)),voidpointertype);
  973. { dimension count }
  974. tcb.emit_ord_const(dimcount,u8inttype);
  975. finaldef:=def;
  976. { ranges of the dimensions }
  977. for i:=1 to dimcount do
  978. begin
  979. curdef:=tarraydef(finaldef);
  980. finaldef:=curdef.elementdef;
  981. { Dims[i] PPTypeInfo }
  982. write_rtti_reference(tcb,curdef.rangedef,rt);
  983. end;
  984. end
  985. else
  986. { write a delphi almost compatible dyn. array entry:
  987. there are two types, eltype and eltype2, the latter is nil if the element type needs
  988. no finalization, the former is always valid, delphi has this swapped, but for
  989. compatibility with older fpc versions we do it different, to be delphi compatible,
  990. the names are swapped in typinfo.pp
  991. }
  992. begin
  993. tcb.begin_anonymous_record(
  994. internaltypeprefixName[itp_rtti_dyn_array],
  995. defaultpacking,reqalign,
  996. targetinfos[target_info.system]^.alignment.recordalignmin,
  997. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  998. { size of elements }
  999. tcb.emit_tai(Tai_const.Create_sizeint(def.elesize),sizeuinttype);
  1000. { element type }
  1001. write_rtti_reference(tcb,def.elementdef,rt);
  1002. { variant type }
  1003. tcb.emit_ord_const(tstoreddef(def.elementdef).getvardef,s32inttype);
  1004. { element type }
  1005. if def.elementdef.needs_inittable then
  1006. write_rtti_reference(tcb,def.elementdef,rt)
  1007. else
  1008. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1009. { write unit name }
  1010. tcb.emit_shortstring_const(current_module.realmodulename^);
  1011. end;
  1012. tcb.end_anonymous_record;
  1013. end;
  1014. procedure classrefdef_rtti(def:tclassrefdef);
  1015. begin
  1016. write_header(tcb,def,tkClassRef);
  1017. tcb.begin_anonymous_record(
  1018. internaltypeprefixName[itp_rtti_ref],
  1019. defaultpacking,reqalign,
  1020. targetinfos[target_info.system]^.alignment.recordalignmin,
  1021. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1022. write_rtti_reference(tcb,def.pointeddef,rt);
  1023. tcb.end_anonymous_record;
  1024. end;
  1025. procedure pointerdef_rtti(def:tpointerdef);
  1026. begin
  1027. write_header(tcb,def,tkPointer);
  1028. tcb.begin_anonymous_record(
  1029. internaltypeprefixName[itp_rtti_ref],
  1030. defaultpacking,reqalign,
  1031. targetinfos[target_info.system]^.alignment.recordalignmin,
  1032. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1033. write_rtti_reference(tcb,def.pointeddef,rt);
  1034. tcb.end_anonymous_record;
  1035. end;
  1036. procedure recorddef_rtti(def:trecorddef);
  1037. procedure write_record_operators;
  1038. var
  1039. rttilab: Tasmsymbol;
  1040. rttidef: tdef;
  1041. tcb: ttai_typedconstbuilder;
  1042. mop: tmanagementoperator;
  1043. procdef: tprocdef;
  1044. begin
  1045. rttilab := current_asmdata.DefineAsmSymbol(
  1046. internaltypeprefixName[itp_init_record_operators]+def.rtti_mangledname(rt),
  1047. AB_GLOBAL,AT_DATA,def);
  1048. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable]);
  1049. tcb.begin_anonymous_record(
  1050. rttilab.Name,
  1051. defaultpacking,reqalign,
  1052. targetinfos[target_info.system]^.alignment.recordalignmin,
  1053. targetinfos[target_info.system]^.alignment.maxCrecordalign
  1054. );
  1055. { use "succ" to omit first enum item "mop_none" }
  1056. for mop := succ(low(tmanagementoperator)) to high(tmanagementoperator) do
  1057. begin
  1058. if not (mop in trecordsymtable(def.symtable).managementoperators) then
  1059. tcb.emit_tai(Tai_const.Create_nil_codeptr,voidcodepointertype)
  1060. else
  1061. begin
  1062. procdef := search_management_operator(mop, def);
  1063. if procdef = nil then
  1064. internalerror(201603021)
  1065. else
  1066. tcb.emit_tai(Tai_const.Createname(procdef.mangledname,AT_FUNCTION,0),
  1067. cprocvardef.getreusableprocaddr(procdef));
  1068. end;
  1069. end;
  1070. rttidef := tcb.end_anonymous_record;
  1071. current_asmdata.AsmLists[al_rtti].concatList(
  1072. tcb.get_final_asmlist(rttilab,rttidef,sec_rodata,rttilab.name,
  1073. sizeof(PInt)));
  1074. tcb.free;
  1075. end;
  1076. begin
  1077. write_header(tcb,def,tkRecord);
  1078. { need extra reqalign record, because otherwise the u32 int will
  1079. only be aligned to 4 even on 64 bit target (while the rtti code
  1080. in typinfo expects alignments to sizeof(pointer)) }
  1081. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1082. targetinfos[target_info.system]^.alignment.recordalignmin,
  1083. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1084. { store special terminator for init table for more optimal rtl operations
  1085. strictly related to RecordRTTI procedure in rtti.inc (directly
  1086. related to RTTIRecordRttiInfoToInitInfo function) }
  1087. if (rt=initrtti) then
  1088. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype)
  1089. else
  1090. { we use a direct reference as the init RTTI is always in the same
  1091. unit as the full RTTI }
  1092. tcb.emit_tai(Tai_const.Create_sym(get_rtti_label(def,initrtti,false)),voidpointertype);
  1093. tcb.emit_ord_const(def.size,u32inttype);
  1094. { store rtti management operators only for init table }
  1095. if (rt=initrtti) then
  1096. if (trecordsymtable(def.symtable).managementoperators=[]) then
  1097. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype)
  1098. else
  1099. tcb.emit_tai(Tai_const.Createname(
  1100. internaltypeprefixName[itp_init_record_operators]+def.rtti_mangledname(rt),
  1101. AT_DATA_FORCEINDIRECT,0),voidpointertype);
  1102. fields_write_rtti_data(tcb,def,rt);
  1103. tcb.end_anonymous_record;
  1104. { write pointers to operators if needed }
  1105. if (rt=initrtti) and (trecordsymtable(def.symtable).managementoperators<>[]) then
  1106. write_record_operators;
  1107. end;
  1108. procedure procvardef_rtti(def:tprocvardef);
  1109. procedure write_para(parasym:tparavarsym);
  1110. begin
  1111. { only store user visible parameters }
  1112. if not(vo_is_hidden_para in parasym.varoptions) then
  1113. begin
  1114. { write flags for current parameter }
  1115. write_param_flag(tcb,parasym);
  1116. { write name of current parameter }
  1117. tcb.emit_shortstring_const(parasym.realname);
  1118. { write name of type of current parameter }
  1119. write_rtti_name(tcb,parasym.vardef);
  1120. end;
  1121. end;
  1122. procedure write_procedure_param(parasym:tparavarsym);
  1123. begin
  1124. { only store user visible parameters }
  1125. if not(vo_is_hidden_para in parasym.varoptions) then
  1126. begin
  1127. { every parameter is expected to start aligned }
  1128. tcb.begin_anonymous_record(
  1129. internaltypeprefixName[itp_rtti_proc_param]+tostr(length(parasym.realname)),
  1130. defaultpacking,min(reqalign,SizeOf(PInt)),
  1131. targetinfos[target_info.system]^.alignment.recordalignmin,
  1132. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1133. { write flags for current parameter }
  1134. write_param_flag(tcb,parasym);
  1135. { write param type }
  1136. if is_open_array(parasym.vardef) then
  1137. write_rtti_reference(tcb,tarraydef(parasym.vardef).elementdef,fullrtti)
  1138. else
  1139. write_rtti_reference(tcb,parasym.vardef,fullrtti);
  1140. { write name of current parameter }
  1141. tcb.emit_shortstring_const(parasym.realname);
  1142. tcb.end_anonymous_record;
  1143. end;
  1144. end;
  1145. var
  1146. methodkind : byte;
  1147. i : integer;
  1148. begin
  1149. if po_methodpointer in def.procoptions then
  1150. begin
  1151. { write method id and name }
  1152. write_header(tcb,def,tkMethod);
  1153. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1154. targetinfos[target_info.system]^.alignment.recordalignmin,
  1155. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1156. { write kind of method }
  1157. methodkind:=write_methodkind(tcb,def);
  1158. { write parameter info. The parameters must be written in reverse order
  1159. if this method uses right to left parameter pushing! }
  1160. tcb.emit_ord_const(def.maxparacount,u8inttype);
  1161. for i:=0 to def.paras.count-1 do
  1162. write_para(tparavarsym(def.paras[i]));
  1163. if (methodkind=mkFunction) or (methodkind=mkClassFunction) then
  1164. begin
  1165. { write name of result type }
  1166. write_rtti_name(tcb,def.returndef);
  1167. { enclosing record takes care of alignment }
  1168. { write result typeinfo }
  1169. write_rtti_reference(tcb,def.returndef,fullrtti);
  1170. end;
  1171. { write calling convention }
  1172. write_callconv(tcb,def);
  1173. { enclosing record takes care of alignment }
  1174. { write params typeinfo }
  1175. for i:=0 to def.paras.count-1 do
  1176. if not(vo_is_hidden_para in tparavarsym(def.paras[i]).varoptions) then
  1177. begin
  1178. if is_open_array(tparavarsym(def.paras[i]).vardef) then
  1179. write_rtti_reference(tcb,tarraydef(tparavarsym(def.paras[i]).vardef).elementdef,fullrtti)
  1180. else
  1181. write_rtti_reference(tcb,tparavarsym(def.paras[i]).vardef,fullrtti);
  1182. end;
  1183. tcb.end_anonymous_record;
  1184. end
  1185. else
  1186. begin
  1187. write_header(tcb,def,tkProcvar);
  1188. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1189. targetinfos[target_info.system]^.alignment.recordalignmin,
  1190. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1191. { flags }
  1192. tcb.emit_ord_const(0,u8inttype);
  1193. { write calling convention }
  1194. write_callconv(tcb,def);
  1195. { enclosing record takes care of alignment }
  1196. { write result typeinfo }
  1197. write_rtti_reference(tcb,def.returndef,fullrtti);
  1198. { write parameter count }
  1199. tcb.emit_ord_const(def.maxparacount,u8inttype);
  1200. for i:=0 to def.paras.count-1 do
  1201. write_procedure_param(tparavarsym(def.paras[i]));
  1202. tcb.end_anonymous_record;
  1203. end;
  1204. end;
  1205. procedure objectdef_rtti(def: tobjectdef);
  1206. procedure objectdef_rtti_fields(def:tobjectdef);
  1207. begin
  1208. { - for compatiblity with record RTTI we need to write a terminator-
  1209. Nil pointer for initrtti as well for objects
  1210. - for RTTI consistency for objects we need point from fullrtti
  1211. to initrtti
  1212. - classes are assumed to have the same INIT RTTI as records
  1213. (see TObject.CleanupInstance)
  1214. - neither helper nor class type have fullrtti for fields
  1215. }
  1216. if (rt=initrtti) then
  1217. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype)
  1218. else
  1219. if (def.objecttype=odt_object) then
  1220. tcb.emit_tai(Tai_const.Create_sym(get_rtti_label(def,initrtti,false)),voidpointertype)
  1221. else
  1222. internalerror(2017011801);
  1223. tcb.emit_ord_const(def.size, u32inttype);
  1224. { pointer to management operators available only for initrtti }
  1225. if (rt=initrtti) then
  1226. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1227. { enclosing record takes care of alignment }
  1228. fields_write_rtti_data(tcb,def,rt);
  1229. end;
  1230. procedure objectdef_rtti_interface_init(def:tobjectdef);
  1231. begin
  1232. tcb.emit_ord_const(def.size, u32inttype);
  1233. end;
  1234. procedure objectdef_rtti_class_full(def:tobjectdef);
  1235. var
  1236. propnamelist : TFPHashObjectList;
  1237. begin
  1238. { Collect unique property names with nameindex }
  1239. propnamelist:=TFPHashObjectList.Create;
  1240. collect_propnamelist(propnamelist,def);
  1241. if not is_objectpascal_helper(def) then
  1242. if (oo_has_vmt in def.objectoptions) then
  1243. tcb.emit_tai(
  1244. Tai_const.Createname(def.vmt_mangledname,AT_DATA_FORCEINDIRECT,0),
  1245. cpointerdef.getreusable(def.vmt_def))
  1246. else
  1247. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1248. { write parent typeinfo }
  1249. write_rtti_reference(tcb,def.childof,fullrtti);
  1250. { write typeinfo of extended type }
  1251. if is_objectpascal_helper(def) then
  1252. if assigned(def.extendeddef) then
  1253. write_rtti_reference(tcb,def.extendeddef,fullrtti)
  1254. else
  1255. InternalError(2011033001);
  1256. { total number of unique properties }
  1257. tcb.emit_ord_const(propnamelist.count,u16inttype);
  1258. { write unit name }
  1259. tcb.emit_shortstring_const(current_module.realmodulename^);
  1260. { write published properties for this object }
  1261. published_properties_write_rtti_data(tcb,propnamelist,def.symtable);
  1262. propnamelist.free;
  1263. end;
  1264. procedure objectdef_rtti_interface_full(def:tobjectdef);
  1265. var
  1266. propnamelist : TFPHashObjectList;
  1267. { if changed to a set, make sure it's still a byte large, and
  1268. swap appropriately when cross-compiling
  1269. }
  1270. IntfFlags: byte;
  1271. begin
  1272. { Collect unique property names with nameindex }
  1273. propnamelist:=TFPHashObjectList.Create;
  1274. collect_propnamelist(propnamelist,def);
  1275. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1276. targetinfos[target_info.system]^.alignment.recordalignmin,
  1277. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1278. { write parent typeinfo }
  1279. write_rtti_reference(tcb,def.childof,fullrtti);
  1280. { interface: write flags, iid and iidstr }
  1281. IntfFlags:=0;
  1282. if assigned(def.iidguid) then
  1283. IntfFlags:=IntfFlags or (1 shl ord(ifHasGuid));
  1284. if (def.objecttype=odt_interfacecorba) and (def.iidstr^<>'') then
  1285. IntfFlags:=IntfFlags or (1 shl ord(ifHasStrGUID));
  1286. if (def.objecttype=odt_dispinterface) then
  1287. IntfFlags:=IntfFlags or (1 shl ord(ifDispInterface));
  1288. if (target_info.endian=endian_big) then
  1289. IntfFlags:=reverse_byte(IntfFlags);
  1290. {
  1291. ifDispatch, }
  1292. tcb.emit_ord_const(IntfFlags,u8inttype);
  1293. { write GUID }
  1294. tcb.emit_guid_const(def.iidguid^);
  1295. { write unit name }
  1296. tcb.emit_shortstring_const(current_module.realmodulename^);
  1297. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1298. targetinfos[target_info.system]^.alignment.recordalignmin,
  1299. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1300. { write iidstr }
  1301. if def.objecttype=odt_interfacecorba then
  1302. begin
  1303. { prepareguid always allocates an empty string }
  1304. if not assigned(def.iidstr) then
  1305. internalerror(2016021901);
  1306. tcb.emit_shortstring_const(def.iidstr^)
  1307. end;
  1308. { write published properties for this object }
  1309. published_properties_write_rtti_data(tcb,propnamelist,def.symtable);
  1310. { write published methods for this interface }
  1311. write_methods(tcb,def.symtable,[vis_published]);
  1312. tcb.end_anonymous_record;
  1313. tcb.end_anonymous_record;
  1314. propnamelist.free;
  1315. end;
  1316. begin
  1317. case def.objecttype of
  1318. odt_class:
  1319. tcb.emit_ord_const(tkclass,u8inttype);
  1320. odt_object:
  1321. tcb.emit_ord_const(tkobject,u8inttype);
  1322. odt_dispinterface,
  1323. odt_interfacecom:
  1324. tcb.emit_ord_const(tkInterface,u8inttype);
  1325. odt_interfacecorba:
  1326. tcb.emit_ord_const(tkinterfaceCorba,u8inttype);
  1327. odt_helper:
  1328. tcb.emit_ord_const(tkhelper,u8inttype);
  1329. else
  1330. internalerror(200611034);
  1331. end;
  1332. { generate the name }
  1333. tcb.emit_shortstring_const(def.objrealname^);
  1334. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1335. targetinfos[target_info.system]^.alignment.recordalignmin,
  1336. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1337. case rt of
  1338. initrtti :
  1339. begin
  1340. if def.objecttype in [odt_class,odt_object,odt_helper] then
  1341. objectdef_rtti_fields(def)
  1342. else
  1343. objectdef_rtti_interface_init(def);
  1344. end;
  1345. fullrtti :
  1346. begin
  1347. case def.objecttype of
  1348. odt_helper,
  1349. odt_class:
  1350. objectdef_rtti_class_full(def);
  1351. odt_object:
  1352. objectdef_rtti_fields(def);
  1353. else
  1354. objectdef_rtti_interface_full(def);
  1355. end;
  1356. end;
  1357. end;
  1358. tcb.end_anonymous_record;
  1359. end;
  1360. begin
  1361. case def.typ of
  1362. variantdef :
  1363. variantdef_rtti(tvariantdef(def));
  1364. stringdef :
  1365. stringdef_rtti(tstringdef(def));
  1366. enumdef :
  1367. enumdef_rtti(tenumdef(def));
  1368. orddef :
  1369. orddef_rtti(torddef(def));
  1370. floatdef :
  1371. floatdef_rtti(tfloatdef(def));
  1372. setdef :
  1373. setdef_rtti(tsetdef(def));
  1374. procvardef :
  1375. procvardef_rtti(tprocvardef(def));
  1376. arraydef :
  1377. begin
  1378. if ado_IsBitPacked in tarraydef(def).arrayoptions then
  1379. unknown_rtti(tstoreddef(def))
  1380. else
  1381. arraydef_rtti(tarraydef(def));
  1382. end;
  1383. recorddef :
  1384. begin
  1385. if trecorddef(def).is_packed then
  1386. unknown_rtti(tstoreddef(def))
  1387. else
  1388. recorddef_rtti(trecorddef(def));
  1389. end;
  1390. objectdef :
  1391. objectdef_rtti(tobjectdef(def));
  1392. classrefdef :
  1393. classrefdef_rtti(tclassrefdef(def));
  1394. pointerdef :
  1395. pointerdef_rtti(tpointerdef(def));
  1396. else
  1397. unknown_rtti(tstoreddef(def));
  1398. end;
  1399. end;
  1400. function enumsym_compare_name(item1, item2: pointer): Integer;
  1401. var
  1402. enum1: tenumsym absolute item1;
  1403. enum2: tenumsym absolute item2;
  1404. begin
  1405. if enum1=enum2 then
  1406. result:=0
  1407. else if enum1.name>enum2.name then
  1408. result:=1
  1409. else
  1410. { there can't be equal names, identifiers are unique }
  1411. result:=-1;
  1412. end;
  1413. function enumsym_compare_value(item1, item2: pointer): Integer;
  1414. var
  1415. enum1: tenumsym absolute item1;
  1416. enum2: tenumsym absolute item2;
  1417. begin
  1418. if enum1.value>enum2.value then
  1419. result:=1
  1420. else if enum1.value<enum2.value then
  1421. result:=-1
  1422. else
  1423. result:=0;
  1424. end;
  1425. procedure TRTTIWriter.write_rtti_extrasyms(def:Tdef;rt:Trttitype;mainrtti:Tasmsymbol);
  1426. type Penumsym = ^Tenumsym;
  1427. { Writes a helper table for accelerated conversion of ordinal enum values to strings.
  1428. If you change something in this method, make sure to adapt the corresponding code
  1429. in sstrings.inc. }
  1430. procedure enumdef_rtti_ord2stringindex(rttidef: trecorddef; const syms: tfplist);
  1431. var rttilab:Tasmsymbol;
  1432. h,i,o,prev_value:longint;
  1433. mode:(lookup,search); {Modify with care, ordinal value of enum is written.}
  1434. r:single; {Must be real type because of integer overflow risk.}
  1435. tcb: ttai_typedconstbuilder;
  1436. sym_count: integer;
  1437. tabledef: tdef;
  1438. begin
  1439. {Decide wether a lookup array is size efficient.}
  1440. mode:=lookup;
  1441. sym_count:=syms.count;
  1442. if sym_count>0 then
  1443. begin
  1444. i:=1;
  1445. r:=0;
  1446. h:=tenumsym(syms[0]).value; {Next expected enum value is min.}
  1447. { set prev_value for the first iteration to a value that is
  1448. different from the first one without risking overflow (it's used
  1449. to detect whether two enum values are the same) }
  1450. if h=0 then
  1451. prev_value:=1
  1452. else
  1453. prev_value:=0;
  1454. while i<sym_count do
  1455. begin
  1456. { if two enum values are the same, we have to create a table }
  1457. if (prev_value=h) then
  1458. begin
  1459. mode:=search;
  1460. break;
  1461. end;
  1462. {Calculate size of hole between values. Avoid integer overflows.}
  1463. r:=r+(single(tenumsym(syms[i]).value)-single(h))-1;
  1464. prev_value:=h;
  1465. h:=tenumsym(syms[i]).value;
  1466. inc(i);
  1467. end;
  1468. if r>sym_count then
  1469. mode:=search; {Don't waste more than 50% space.}
  1470. end;
  1471. { write rtti data; make sure that the alignment matches the corresponding data structure
  1472. in the code that uses it (if alignment is required). }
  1473. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_data_force_indirect]);
  1474. { use TConstPtrUInt packrecords to ensure good alignment }
  1475. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1476. targetinfos[target_info.system]^.alignment.recordalignmin,
  1477. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1478. { now emit the data: first the mode }
  1479. tcb.emit_tai(Tai_const.create_32bit(longint(mode)),u32inttype);
  1480. { align }
  1481. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,sizeof(PInt)),
  1482. targetinfos[target_info.system]^.alignment.recordalignmin,
  1483. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1484. if mode=lookup then
  1485. begin
  1486. o:=tenumsym(syms[0]).value; {Start with min value.}
  1487. for i:=0 to sym_count-1 do
  1488. begin
  1489. while o<tenumsym(syms[i]).value do
  1490. begin
  1491. tcb.emit_tai(Tai_const.create_nil_dataptr,ptruinttype);
  1492. inc(o);
  1493. end;
  1494. inc(o);
  1495. tcb.queue_init(voidpointertype);
  1496. tcb.queue_subscriptn_multiple_by_name(rttidef,
  1497. ['size_start_rec',
  1498. 'min_max_rec',
  1499. 'basetype_array_rec',
  1500. tsym(syms[i]).Name]
  1501. );
  1502. tcb.queue_emit_asmsym(mainrtti,rttidef);
  1503. end;
  1504. end
  1505. else
  1506. begin
  1507. tcb.emit_ord_const(sym_count,u32inttype);
  1508. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,sizeof(PInt)),
  1509. targetinfos[target_info.system]^.alignment.recordalignmin,
  1510. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1511. for i:=0 to sym_count-1 do
  1512. begin
  1513. tcb.emit_ord_const(tenumsym(syms[i]).value,s32inttype);
  1514. tcb.queue_init(voidpointertype);
  1515. tcb.queue_subscriptn_multiple_by_name(rttidef,
  1516. ['size_start_rec',
  1517. 'min_max_rec',
  1518. 'basetype_array_rec',
  1519. tsym(syms[i]).Name]
  1520. );
  1521. tcb.queue_emit_asmsym(mainrtti,rttidef);
  1522. end;
  1523. tcb.end_anonymous_record;
  1524. end;
  1525. tcb.end_anonymous_record;
  1526. tabledef:=tcb.end_anonymous_record;
  1527. rttilab:=current_asmdata.DefineAsmSymbol(Tstoreddef(def).rtti_mangledname(rt)+'_o2s',AB_GLOBAL,AT_DATA_NOINDIRECT,tabledef);
  1528. current_asmdata.asmlists[al_rtti].concatlist(tcb.get_final_asmlist(
  1529. rttilab,tabledef,sec_rodata,
  1530. rttilab.name,sizeof(PInt)));
  1531. tcb.free;
  1532. current_module.add_public_asmsym(rttilab);
  1533. end;
  1534. { Writes a helper table for accelerated conversion of string to ordinal enum values.
  1535. If you change something in this method, make sure to adapt the corresponding code
  1536. in sstrings.inc. }
  1537. procedure enumdef_rtti_string2ordindex(rttidef: trecorddef; const syms: tfplist);
  1538. var
  1539. tcb: ttai_typedconstbuilder;
  1540. rttilab: Tasmsymbol;
  1541. i:longint;
  1542. tabledef: tdef;
  1543. begin
  1544. { write rtti data }
  1545. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_data_force_indirect]);
  1546. { begin of Tstring_to_ord }
  1547. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,sizeof(PInt)),
  1548. targetinfos[target_info.system]^.alignment.recordalignmin,
  1549. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1550. tcb.emit_ord_const(syms.count,s32inttype);
  1551. { begin of "data" array in Tstring_to_ord }
  1552. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,sizeof(PInt)),
  1553. targetinfos[target_info.system]^.alignment.recordalignmin,
  1554. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1555. for i:=0 to syms.count-1 do
  1556. begin
  1557. tcb.emit_ord_const(tenumsym(syms[i]).value,s32inttype);
  1558. { alignment of pointer value handled by enclosing record already }
  1559. tcb.queue_init(voidpointertype);
  1560. tcb.queue_subscriptn_multiple_by_name(rttidef,
  1561. ['size_start_rec',
  1562. 'min_max_rec',
  1563. 'basetype_array_rec',
  1564. tsym(syms[i]).Name]
  1565. );
  1566. tcb.queue_emit_asmsym(mainrtti,rttidef);
  1567. end;
  1568. tcb.end_anonymous_record;
  1569. tabledef:=tcb.end_anonymous_record;
  1570. rttilab:=current_asmdata.DefineAsmSymbol(Tstoreddef(def).rtti_mangledname(rt)+'_s2o',AB_GLOBAL,AT_DATA_NOINDIRECT,tabledef);
  1571. current_asmdata.asmlists[al_rtti].concatlist(tcb.get_final_asmlist(
  1572. rttilab,tabledef,sec_rodata,
  1573. rttilab.name,sizeof(PInt)));
  1574. tcb.free;
  1575. current_module.add_public_asmsym(rttilab);
  1576. end;
  1577. procedure enumdef_rtti_extrasyms(def:Tenumdef);
  1578. var
  1579. t:Tenumsym;
  1580. syms:tfplist;
  1581. i:longint;
  1582. rttitypesym: ttypesym;
  1583. rttidef: trecorddef;
  1584. begin
  1585. { collect enumsyms belonging to this enum type (could be a subsection
  1586. in case of a subrange type) }
  1587. syms:=tfplist.create;
  1588. for i := 0 to def.symtable.SymList.Count - 1 do
  1589. begin
  1590. t:=tenumsym(def.symtable.SymList[i]);
  1591. if t.value<def.minval then
  1592. continue
  1593. else
  1594. if t.value>def.maxval then
  1595. break;
  1596. syms.add(t);
  1597. end;
  1598. { sort the syms by enum name }
  1599. syms.sort(@enumsym_compare_name);
  1600. rttitypesym:=try_search_current_module_type(internaltypeprefixName[itp_rttidef]+def.rtti_mangledname(fullrtti));
  1601. if not assigned(rttitypesym) or
  1602. (ttypesym(rttitypesym).typedef.typ<>recorddef) then
  1603. internalerror(2015071402);
  1604. rttidef:=trecorddef(ttypesym(rttitypesym).typedef);
  1605. enumdef_rtti_string2ordindex(rttidef,syms);
  1606. { sort the syms by enum value }
  1607. syms.sort(@enumsym_compare_value);
  1608. enumdef_rtti_ord2stringindex(rttidef,syms);
  1609. syms.free;
  1610. end;
  1611. begin
  1612. case def.typ of
  1613. enumdef:
  1614. if rt=fullrtti then
  1615. begin
  1616. enumdef_rtti_extrasyms(Tenumdef(def));
  1617. end;
  1618. end;
  1619. end;
  1620. procedure TRTTIWriter.write_child_rtti_data(def:tdef;rt:trttitype);
  1621. begin
  1622. case def.typ of
  1623. enumdef :
  1624. if assigned(tenumdef(def).basedef) then
  1625. write_rtti(tenumdef(def).basedef,rt);
  1626. setdef :
  1627. write_rtti(tsetdef(def).elementdef,rt);
  1628. arraydef :
  1629. begin
  1630. write_rtti(tarraydef(def).rangedef,rt);
  1631. write_rtti(tarraydef(def).elementdef,rt);
  1632. end;
  1633. recorddef :
  1634. begin
  1635. { guarantee initrtti for any record for RTTI purposes
  1636. also for fpc_initialize, fpc_finalize }
  1637. if (rt=fullrtti) then
  1638. begin
  1639. include(def.defstates,ds_init_table_used);
  1640. write_rtti(def, initrtti);
  1641. end;
  1642. fields_write_rtti(trecorddef(def).symtable,rt);
  1643. end;
  1644. objectdef :
  1645. begin
  1646. if assigned(tobjectdef(def).childof) then
  1647. write_rtti(tobjectdef(def).childof,rt);
  1648. if (rt=initrtti) or (tobjectdef(def).objecttype=odt_object) then
  1649. fields_write_rtti(tobjectdef(def).symtable,rt)
  1650. else
  1651. published_write_rtti(tobjectdef(def).symtable,rt);
  1652. if (rt=fullrtti) then
  1653. begin
  1654. { guarantee initrtti for any object for RTTI purposes
  1655. also for fpc_initialize, fpc_finalize }
  1656. if (tobjectdef(def).objecttype=odt_object) then
  1657. begin
  1658. include(def.defstates,ds_init_table_used);
  1659. write_rtti(def,initrtti);
  1660. end;
  1661. if (is_interface(def) or is_dispinterface(def))
  1662. and (oo_can_have_published in tobjectdef(def).objectoptions) then
  1663. methods_write_rtti(tobjectdef(def).symtable,rt,[vis_published],true);
  1664. end;
  1665. end;
  1666. classrefdef,
  1667. pointerdef:
  1668. if not is_objc_class_or_protocol(tabstractpointerdef(def).pointeddef) then
  1669. write_rtti(tabstractpointerdef(def).pointeddef,rt);
  1670. procvardef:
  1671. params_write_rtti(tabstractprocdef(def),rt,false);
  1672. end;
  1673. end;
  1674. procedure TRTTIWriter.write_rtti_reference(tcb: ttai_typedconstbuilder; def: tdef; rt: trttitype);
  1675. begin
  1676. { we don't care about the real type here, because
  1677. a) we don't index into these elements
  1678. b) we may not have the rtti type available at the point that we
  1679. are emitting this data, because of forward definitions etc
  1680. c) if the rtti is emitted in another unit, we won't have the type
  1681. available at all
  1682. For the cases where the type is emitted in the current unit and hence
  1683. the underlying system will detect and complain about symbol def
  1684. mismatches, type conversions will have to be inserted afterwards (like
  1685. in llvm/llvmtype)
  1686. }
  1687. if not assigned(def) or is_void(def) or ((rt<>initrtti) and is_objc_class_or_protocol(def)) then
  1688. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype)
  1689. else
  1690. tcb.emit_tai(Tai_const.Create_sym(get_rtti_label(def,rt,true)),voidpointertype);
  1691. end;
  1692. function TRTTIWriter.ref_rtti(def:tdef;rt:trttitype;indirect:boolean;suffix:tsymstr):tasmsymbol;
  1693. var
  1694. s : tsymstr;
  1695. begin
  1696. s:=def.rtti_mangledname(rt)+suffix;
  1697. result:=current_asmdata.RefAsmSymbol(s,AT_DATA,indirect);
  1698. if (cs_create_pic in current_settings.moduleswitches) and
  1699. assigned(current_procinfo) then
  1700. include(current_procinfo.flags,pi_needs_got);
  1701. if def.owner.moduleid<>current_module.moduleid then
  1702. current_module.add_extern_asmsym(s,AB_EXTERNAL,AT_DATA);
  1703. end;
  1704. procedure TRTTIWriter.write_rtti(def:tdef;rt:trttitype);
  1705. var
  1706. tcb: ttai_typedconstbuilder;
  1707. rttilab: tasmsymbol;
  1708. rttidef: tdef;
  1709. begin
  1710. { only write rtti of definitions from the current module }
  1711. if not findunitsymtable(def.owner).iscurrentunit then
  1712. exit;
  1713. { check if separate initrtti is actually needed }
  1714. if (rt=initrtti) and (not def.needs_separate_initrtti) then
  1715. rt:=fullrtti;
  1716. { prevent recursion }
  1717. if rttidefstate[rt] in def.defstates then
  1718. exit;
  1719. include(def.defstates,rttidefstate[rt]);
  1720. { write first all dependencies }
  1721. write_child_rtti_data(def,rt);
  1722. { write rtti data }
  1723. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_data_force_indirect]);
  1724. tcb.begin_anonymous_record(
  1725. internaltypeprefixName[itp_rttidef]+tstoreddef(def).rtti_mangledname(rt),
  1726. defaultpacking,reqalign,
  1727. targetinfos[target_info.system]^.alignment.recordalignmin,
  1728. targetinfos[target_info.system]^.alignment.maxCrecordalign
  1729. );
  1730. write_rtti_data(tcb,def,rt);
  1731. rttidef:=tcb.end_anonymous_record;
  1732. rttilab:=current_asmdata.DefineAsmSymbol(tstoreddef(def).rtti_mangledname(rt),AB_GLOBAL,AT_DATA_NOINDIRECT,rttidef);
  1733. current_asmdata.AsmLists[al_rtti].concatList(
  1734. tcb.get_final_asmlist(rttilab,rttidef,sec_rodata,rttilab.name,min(target_info.alignment.maxCrecordalign,SizeOf(QWord))));
  1735. tcb.free;
  1736. current_module.add_public_asmsym(rttilab);
  1737. { write additional data }
  1738. write_rtti_extrasyms(def,rt,rttilab);
  1739. end;
  1740. constructor TRTTIWriter.create;
  1741. begin
  1742. if tf_requires_proper_alignment in target_info.flags then
  1743. begin
  1744. reqalign:=min(sizeof(QWord),target_info.alignment.maxCrecordalign);
  1745. defaultpacking:=C_alignment;
  1746. end
  1747. else
  1748. begin
  1749. reqalign:=1;
  1750. defaultpacking:=1;
  1751. end;
  1752. end;
  1753. function TRTTIWriter.get_rtti_label(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  1754. begin
  1755. result:=ref_rtti(def,rt,indirect,'');
  1756. end;
  1757. function TRTTIWriter.get_rtti_label_ord2str(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  1758. begin
  1759. result:=ref_rtti(def,rt,indirect,'_o2s');
  1760. end;
  1761. function TRTTIWriter.get_rtti_label_str2ord(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  1762. begin
  1763. result:=ref_rtti(def,rt,indirect,'_s2o');
  1764. end;
  1765. end.