ncgrtti.pas 62 KB

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