ncgrtti.pas 66 KB

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