ncgrtti.pas 68 KB

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