ncgrtti.pas 68 KB

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