ncgrtti.pas 65 KB

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