ncgrtti.pas 68 KB

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