ncgrtti.pas 68 KB

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