ncgrtti.pas 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  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,
  23. symbase,symconst,symtype,symdef;
  24. type
  25. { TRTTIWriter }
  26. TRTTIWriter=class
  27. private
  28. procedure fields_write_rtti(st:tsymtable;rt:trttitype);
  29. procedure fields_write_rtti_data(def:tabstractrecorddef;rt:trttitype);
  30. procedure write_rtti_extrasyms(def:Tdef;rt:Trttitype;mainrtti:Tasmsymbol);
  31. procedure published_write_rtti(st:tsymtable;rt:trttitype);
  32. function published_properties_count(st:tsymtable):longint;
  33. procedure published_properties_write_rtti_data(propnamelist:TFPHashObjectList;st:tsymtable);
  34. procedure collect_propnamelist(propnamelist:TFPHashObjectList;objdef:tobjectdef);
  35. function ref_rtti(def:tdef;rt:trttitype):tasmsymbol;
  36. procedure write_rtti_name(def:tdef);
  37. procedure write_rtti_data(def:tdef;rt:trttitype);
  38. procedure write_child_rtti_data(def:tdef;rt:trttitype);
  39. procedure write_rtti_reference(def:tdef;rt:trttitype);
  40. procedure write_header(def: tdef; typekind: byte);
  41. procedure write_string(const s: string);
  42. procedure maybe_write_align;
  43. public
  44. procedure write_rtti(def:tdef;rt:trttitype);
  45. function get_rtti_label(def:tdef;rt:trttitype):tasmsymbol;
  46. function get_rtti_label_ord2str(def:tdef;rt:trttitype):tasmsymbol;
  47. function get_rtti_label_str2ord(def:tdef;rt:trttitype):tasmsymbol;
  48. end;
  49. var
  50. RTTIWriter : TRTTIWriter;
  51. implementation
  52. uses
  53. cutils,
  54. globals,globtype,verbose,systems,
  55. fmodule, procinfo,
  56. symsym,
  57. aasmtai,aasmdata,
  58. defutil,
  59. wpobase
  60. ;
  61. const
  62. rttidefstate : array[trttitype] of tdefstate =
  63. (ds_rtti_table_written,ds_init_table_written,
  64. { Objective-C related, does not pass here }
  65. symconst.ds_none,symconst.ds_none,
  66. symconst.ds_none,symconst.ds_none);
  67. type
  68. TPropNameListItem = class(TFPHashObject)
  69. propindex : longint;
  70. propowner : TSymtable;
  71. end;
  72. {***************************************************************************
  73. TRTTIWriter
  74. ***************************************************************************}
  75. procedure TRTTIWriter.maybe_write_align;
  76. begin
  77. if (tf_requires_proper_alignment in target_info.flags) then
  78. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  79. end;
  80. procedure TRTTIWriter.write_string(const s: string);
  81. begin
  82. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(s)));
  83. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(s));
  84. end;
  85. procedure TRTTIWriter.write_header(def: tdef; typekind: byte);
  86. begin
  87. if def.typ=arraydef then
  88. InternalError(201012211);
  89. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(typekind));
  90. if assigned(def.typesym) then
  91. write_string(ttypesym(def.typesym).realname)
  92. else
  93. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(#0));
  94. end;
  95. procedure TRTTIWriter.write_rtti_name(def:tdef);
  96. var
  97. hs : string;
  98. begin
  99. if is_open_array(def) then
  100. { open arrays never have a typesym with a name, since you cannot
  101. define an "open array type". Kylix prints the type of the
  102. elements in the array in this case (so together with the pfArray
  103. flag, you can reconstruct the full typename, I assume (JM))
  104. }
  105. def:=tarraydef(def).elementdef;
  106. { name }
  107. if assigned(def.typesym) then
  108. begin
  109. hs:=ttypesym(def.typesym).realname;
  110. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(chr(length(hs))+hs));
  111. end
  112. else
  113. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(#0));
  114. end;
  115. { writes a 32-bit count followed by array of field infos for given symtable }
  116. procedure TRTTIWriter.fields_write_rtti_data(def:tabstractrecorddef;rt:trttitype);
  117. var
  118. i : longint;
  119. sym : tsym;
  120. fieldcnt: longint;
  121. lastai: TLinkedListItem;
  122. st: tsymtable;
  123. begin
  124. fieldcnt:=0;
  125. { Count will be inserted at this location. It cannot be nil as we've just
  126. written header for this symtable owner. But stay safe. }
  127. lastai:=current_asmdata.asmlists[al_rtti].last;
  128. if lastai=nil then
  129. InternalError(201012212);
  130. { For objects, treat parent (if any) as a field with offset 0. This
  131. provides correct handling of entire instance with RTL rtti routines. }
  132. if (def.typ=objectdef) and (tobjectdef(def).objecttype=odt_object) and
  133. Assigned(tobjectdef(def).childof) and
  134. ((rt=fullrtti) or (tobjectdef(def).childof.needs_inittable)) then
  135. begin
  136. write_rtti_reference(tobjectdef(def).childof,rt);
  137. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_pint(0));
  138. inc(fieldcnt);
  139. end;
  140. st:=def.symtable;
  141. for i:=0 to st.SymList.Count-1 do
  142. begin
  143. sym:=tsym(st.SymList[i]);
  144. if (tsym(sym).typ=fieldvarsym) and
  145. not(sp_static in tsym(sym).symoptions) and
  146. (
  147. (rt=fullrtti) or
  148. tfieldvarsym(sym).vardef.needs_inittable
  149. ) and
  150. not is_objc_class_or_protocol(tfieldvarsym(sym).vardef) then
  151. begin
  152. write_rtti_reference(tfieldvarsym(sym).vardef,rt);
  153. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_pint(tfieldvarsym(sym).fieldoffset));
  154. inc(fieldcnt);
  155. end;
  156. end;
  157. { insert field count before data }
  158. current_asmdata.asmlists[al_rtti].InsertAfter(Tai_const.Create_32bit(fieldcnt),lastai)
  159. end;
  160. procedure TRTTIWriter.fields_write_rtti(st:tsymtable;rt:trttitype);
  161. var
  162. i : longint;
  163. sym : tsym;
  164. begin
  165. for i:=0 to st.SymList.Count-1 do
  166. begin
  167. sym:=tsym(st.SymList[i]);
  168. if (tsym(sym).typ=fieldvarsym) and
  169. not(sp_static in tsym(sym).symoptions) and
  170. (
  171. (rt=fullrtti) or
  172. tfieldvarsym(sym).vardef.needs_inittable
  173. ) then
  174. write_rtti(tfieldvarsym(sym).vardef,rt);
  175. end;
  176. end;
  177. procedure TRTTIWriter.published_write_rtti(st:tsymtable;rt:trttitype);
  178. var
  179. i : longint;
  180. sym : tsym;
  181. begin
  182. for i:=0 to st.SymList.Count-1 do
  183. begin
  184. sym:=tsym(st.SymList[i]);
  185. if (sym.visibility=vis_published) then
  186. begin
  187. case tsym(sym).typ of
  188. propertysym:
  189. write_rtti(tpropertysym(sym).propdef,rt);
  190. fieldvarsym:
  191. write_rtti(tfieldvarsym(sym).vardef,rt);
  192. end;
  193. end;
  194. end;
  195. end;
  196. function TRTTIWriter.published_properties_count(st:tsymtable):longint;
  197. var
  198. i : longint;
  199. sym : tsym;
  200. begin
  201. result:=0;
  202. for i:=0 to st.SymList.Count-1 do
  203. begin
  204. sym:=tsym(st.SymList[i]);
  205. if (tsym(sym).typ=propertysym) and
  206. (sym.visibility=vis_published) then
  207. inc(result);
  208. end;
  209. end;
  210. procedure TRTTIWriter.collect_propnamelist(propnamelist:TFPHashObjectList;objdef:tobjectdef);
  211. var
  212. i : longint;
  213. sym : tsym;
  214. pn : tpropnamelistitem;
  215. begin
  216. if assigned(objdef.childof) then
  217. collect_propnamelist(propnamelist,objdef.childof);
  218. for i:=0 to objdef.symtable.SymList.Count-1 do
  219. begin
  220. sym:=tsym(objdef.symtable.SymList[i]);
  221. if (tsym(sym).typ=propertysym) and
  222. (sym.visibility=vis_published) then
  223. begin
  224. pn:=TPropNameListItem(propnamelist.Find(tsym(sym).name));
  225. if not assigned(pn) then
  226. begin
  227. pn:=tpropnamelistitem.create(propnamelist,tsym(sym).name);
  228. pn.propindex:=propnamelist.count-1;
  229. pn.propowner:=tsym(sym).owner;
  230. end;
  231. end;
  232. end;
  233. end;
  234. procedure TRTTIWriter.published_properties_write_rtti_data(propnamelist:TFPHashObjectList;st:tsymtable);
  235. var
  236. i : longint;
  237. sym : tsym;
  238. proctypesinfo : byte;
  239. propnameitem : tpropnamelistitem;
  240. procedure writeaccessproc(pap:tpropaccesslisttypes; shiftvalue : byte; unsetvalue: byte);
  241. var
  242. typvalue : byte;
  243. hp : ppropaccesslistitem;
  244. address,space : longint;
  245. def : tdef;
  246. hpropsym : tpropertysym;
  247. propaccesslist : tpropaccesslist;
  248. begin
  249. hpropsym:=tpropertysym(sym);
  250. repeat
  251. propaccesslist:=hpropsym.propaccesslist[pap];
  252. if not propaccesslist.empty then
  253. break;
  254. hpropsym:=hpropsym.overriddenpropsym;
  255. until not assigned(hpropsym);
  256. if not(assigned(propaccesslist) and assigned(propaccesslist.firstsym)) then
  257. begin
  258. current_asmdata.asmlists[al_rtti].concat(Tai_const.create(aitconst_ptr,unsetvalue));
  259. typvalue:=3;
  260. end
  261. else if propaccesslist.firstsym^.sym.typ=fieldvarsym then
  262. begin
  263. address:=0;
  264. hp:=propaccesslist.firstsym;
  265. def:=nil;
  266. while assigned(hp) do
  267. begin
  268. case hp^.sltype of
  269. sl_load :
  270. begin
  271. def:=tfieldvarsym(hp^.sym).vardef;
  272. inc(address,tfieldvarsym(hp^.sym).fieldoffset);
  273. end;
  274. sl_subscript :
  275. begin
  276. if not(assigned(def) and
  277. ((def.typ=recorddef) or
  278. is_object(def))) then
  279. internalerror(200402171);
  280. inc(address,tfieldvarsym(hp^.sym).fieldoffset);
  281. def:=tfieldvarsym(hp^.sym).vardef;
  282. end;
  283. sl_vec :
  284. begin
  285. if not(assigned(def) and (def.typ=arraydef)) then
  286. internalerror(200402172);
  287. def:=tarraydef(def).elementdef;
  288. {Hp.value is a Tconstexprint, which can be rather large,
  289. sanity check for longint overflow.}
  290. space:=(high(address)-address) div def.size;
  291. if int64(space)<hp^.value then
  292. internalerror(200706101);
  293. inc(address,int64(def.size*hp^.value));
  294. end;
  295. end;
  296. hp:=hp^.next;
  297. end;
  298. current_asmdata.asmlists[al_rtti].concat(Tai_const.create(aitconst_ptr,address));
  299. typvalue:=0;
  300. end
  301. else
  302. begin
  303. { When there was an error then procdef is not assigned }
  304. if not assigned(propaccesslist.procdef) then
  305. exit;
  306. if not(po_virtualmethod in tprocdef(propaccesslist.procdef).procoptions) or
  307. is_objectpascal_helper(tprocdef(propaccesslist.procdef).struct) then
  308. begin
  309. current_asmdata.asmlists[al_rtti].concat(Tai_const.createname(tprocdef(propaccesslist.procdef).mangledname,0));
  310. typvalue:=1;
  311. end
  312. else
  313. begin
  314. { virtual method, write vmt offset }
  315. current_asmdata.asmlists[al_rtti].concat(Tai_const.create(aitconst_ptr,
  316. tobjectdef(tprocdef(propaccesslist.procdef).struct).vmtmethodoffset(tprocdef(propaccesslist.procdef).extnumber)));
  317. { register for wpo }
  318. tobjectdef(tprocdef(propaccesslist.procdef).struct).register_vmt_call(tprocdef(propaccesslist.procdef).extnumber);
  319. {$ifdef vtentry}
  320. { not sure if we can insert those vtentry symbols safely here }
  321. {$error register methods used for published properties}
  322. {$endif vtentry}
  323. typvalue:=2;
  324. end;
  325. end;
  326. proctypesinfo:=proctypesinfo or (typvalue shl shiftvalue);
  327. end;
  328. begin
  329. for i:=0 to st.SymList.Count-1 do
  330. begin
  331. sym:=tsym(st.SymList[i]);
  332. if (sym.typ=propertysym) and
  333. (sym.visibility=vis_published) then
  334. begin
  335. if ppo_indexed in tpropertysym(sym).propoptions then
  336. proctypesinfo:=$40
  337. else
  338. proctypesinfo:=0;
  339. write_rtti_reference(tpropertysym(sym).propdef,fullrtti);
  340. writeaccessproc(palt_read,0,0);
  341. writeaccessproc(palt_write,2,0);
  342. { is it stored ? }
  343. if not(ppo_stored in tpropertysym(sym).propoptions) then
  344. begin
  345. { no, so put a constant zero }
  346. current_asmdata.asmlists[al_rtti].concat(Tai_const.create(aitconst_ptr,0));
  347. proctypesinfo:=proctypesinfo or (3 shl 4);
  348. end
  349. else
  350. writeaccessproc(palt_stored,4,1); { maybe; if no procedure put a constant 1 (=true) }
  351. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tpropertysym(sym).index));
  352. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tpropertysym(sym).default));
  353. propnameitem:=TPropNameListItem(propnamelist.Find(tpropertysym(sym).name));
  354. if not assigned(propnameitem) then
  355. internalerror(200512201);
  356. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(propnameitem.propindex));
  357. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(proctypesinfo));
  358. write_string(tpropertysym(sym).realname);
  359. maybe_write_align;
  360. end;
  361. end;
  362. end;
  363. procedure TRTTIWriter.write_rtti_data(def:tdef;rt:trttitype);
  364. procedure unknown_rtti(def:tstoreddef);
  365. begin
  366. current_asmdata.asmlists[al_rtti].concat(tai_const.create_8bit(tkUnknown));
  367. write_rtti_name(def);
  368. end;
  369. procedure variantdef_rtti(def:tvariantdef);
  370. begin
  371. write_header(def,tkVariant);
  372. end;
  373. procedure stringdef_rtti(def:tstringdef);
  374. begin
  375. case def.stringtype of
  376. st_ansistring:
  377. begin
  378. write_header(def,tkAString);
  379. maybe_write_align;
  380. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(def.encoding));
  381. end;
  382. st_widestring:
  383. write_header(def,tkWString);
  384. st_unicodestring:
  385. write_header(def,tkUString);
  386. st_longstring:
  387. write_header(def,tkLString);
  388. st_shortstring:
  389. begin
  390. write_header(def,tkSString);
  391. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.len));
  392. maybe_write_align; // is align necessary here?
  393. end;
  394. end;
  395. end;
  396. procedure enumdef_rtti(def:tenumdef);
  397. var
  398. i : integer;
  399. hp : tenumsym;
  400. begin
  401. write_header(def,tkEnumeration);
  402. maybe_write_align;
  403. case longint(def.size) of
  404. 1 :
  405. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUByte));
  406. 2 :
  407. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUWord));
  408. 4 :
  409. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otULong));
  410. end;
  411. { we need to align by Tconstptruint here to satisfy the alignment rules set by
  412. records: in the typinfo unit we overlay a TTypeData record on this data, which at
  413. the innermost variant record needs an alignment of TConstPtrUint due to e.g.
  414. the "CompType" member for tkSet (also the "BaseType" member for tkEnumeration).
  415. We need to adhere to this, otherwise things will break.
  416. Note that other code (e.g. enumdef_rtti_calcstringtablestart()) relies on the
  417. exact sequence too. }
  418. maybe_write_align;
  419. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.min));
  420. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.max));
  421. maybe_write_align; // is align necessary here?
  422. { write base type }
  423. write_rtti_reference(def.basedef,rt);
  424. for i := 0 to def.symtable.SymList.Count - 1 do
  425. begin
  426. hp:=tenumsym(def.symtable.SymList[i]);
  427. if hp.value<def.minval then
  428. continue
  429. else
  430. if hp.value>def.maxval then
  431. break;
  432. write_string(hp.realname);
  433. end;
  434. { write unit name }
  435. write_string(current_module.realmodulename^);
  436. { write zero which is required by RTL }
  437. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(0));
  438. end;
  439. procedure orddef_rtti(def:torddef);
  440. procedure dointeger(typekind: byte);
  441. const
  442. trans : array[tordtype] of byte =
  443. (otUByte{otNone},
  444. otUByte,otUWord,otULong,otUByte{otNone},
  445. otSByte,otSWord,otSLong,otUByte{otNone},
  446. otUByte,otUWord,otULong,otUByte,
  447. otSByte,otSWord,otSLong,otSByte,
  448. otUByte,otUWord,otUByte);
  449. begin
  450. write_header(def,typekind);
  451. maybe_write_align;
  452. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(byte(trans[def.ordtype])));
  453. maybe_write_align;
  454. {Convert to longint to smuggle values in high(longint)+1..high(cardinal) into asmlist.}
  455. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(def.low.svalue)));
  456. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(def.high.svalue)));
  457. end;
  458. begin
  459. case def.ordtype of
  460. s64bit :
  461. begin
  462. write_header(def,tkInt64);
  463. maybe_write_align;
  464. { low }
  465. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(def.low.svalue));
  466. { high }
  467. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(def.high.svalue));
  468. end;
  469. u64bit :
  470. begin
  471. write_header(def,tkQWord);
  472. maybe_write_align;
  473. {use svalue because Create_64bit accepts int64, prevents range checks}
  474. { low }
  475. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(def.low.svalue));
  476. { high }
  477. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(def.high.svalue));
  478. end;
  479. pasbool8:
  480. dointeger(tkBool);
  481. uchar:
  482. dointeger(tkChar);
  483. uwidechar:
  484. dointeger(tkWChar);
  485. scurrency:
  486. begin
  487. write_header(def,tkFloat);
  488. maybe_write_align;
  489. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(ftCurr));
  490. end;
  491. else
  492. dointeger(tkInteger);
  493. end;
  494. end;
  495. procedure floatdef_rtti(def:tfloatdef);
  496. const
  497. {tfloattype = (s32real,s64real,s80real,sc80real,s64bit,s128bit);}
  498. translate : array[tfloattype] of byte =
  499. (ftSingle,ftDouble,ftExtended,ftExtended,ftComp,ftCurr,ftFloat128);
  500. begin
  501. write_header(def,tkFloat);
  502. maybe_write_align;
  503. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(translate[def.floattype]));
  504. end;
  505. procedure setdef_rtti(def:tsetdef);
  506. begin
  507. write_header(def,tkSet);
  508. maybe_write_align;
  509. case def.size of
  510. 1:
  511. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUByte));
  512. 2:
  513. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUWord));
  514. 4:
  515. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otULong));
  516. else
  517. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUByte));
  518. end;
  519. maybe_write_align;
  520. write_rtti_reference(def.elementdef,rt);
  521. end;
  522. procedure arraydef_rtti(def:tarraydef);
  523. var
  524. lastai: TLinkedListItem;
  525. dimcount: byte;
  526. totalcount: asizeuint;
  527. curdef:tarraydef;
  528. begin
  529. if ado_IsDynamicArray in def.arrayoptions then
  530. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkdynarray))
  531. else
  532. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkarray));
  533. write_rtti_name(def);
  534. maybe_write_align;
  535. if not(ado_IsDynamicArray in def.arrayoptions) then
  536. begin
  537. { remember tha last instruction. we will need to insert some
  538. calculated values after it }
  539. lastai:=current_asmdata.asmlists[al_rtti].last;
  540. curdef:=def;
  541. totalcount:=1;
  542. dimcount:=0;
  543. while assigned(curdef) do
  544. begin
  545. { Dims[i] PTypeInfo }
  546. write_rtti_reference(curdef.rangedef,rt);
  547. inc(dimcount);
  548. totalcount:=totalcount*curdef.elecount;
  549. { get the next static array }
  550. if assigned(curdef.elementdef) and
  551. (curdef.elementdef.typ=arraydef) and
  552. not(ado_IsDynamicArray in tarraydef(curdef.elementdef).arrayoptions) then
  553. curdef:=tarraydef(curdef.elementdef)
  554. else
  555. break;
  556. end;
  557. { dimension count }
  558. current_asmdata.asmlists[al_rtti].InsertAfter(Tai_const.Create_8bit(dimcount),lastai);
  559. { last dimension element type }
  560. current_asmdata.asmlists[al_rtti].InsertAfter(Tai_const.Create_sym(ref_rtti(curdef.elementdef,rt)),lastai);
  561. { total element count }
  562. current_asmdata.asmlists[al_rtti].InsertAfter(Tai_const.Create_pint(pint(totalcount)),lastai);
  563. { total size = elecount * elesize of the first arraydef }
  564. current_asmdata.asmlists[al_rtti].InsertAfter(Tai_const.Create_pint(def.elecount*def.elesize),lastai);
  565. end
  566. else
  567. { write a delphi almost compatible dyn. array entry:
  568. there are two types, eltype and eltype2, the latter is nil if the element type needs
  569. no finalization, the former is always valid, delphi has this swapped, but for
  570. compatibility with older fpc versions we do it different, to be delphi compatible,
  571. the names are swapped in typinfo.pp
  572. }
  573. begin
  574. { size of elements }
  575. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_pint(def.elesize));
  576. { element type }
  577. write_rtti_reference(def.elementdef,rt);
  578. { variant type }
  579. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tstoreddef(def.elementdef).getvardef));
  580. { element type }
  581. if def.elementdef.needs_inittable then
  582. write_rtti_reference(def.elementdef,rt)
  583. else
  584. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_pint(0));
  585. { write unit name }
  586. write_string(current_module.realmodulename^);
  587. end;
  588. end;
  589. procedure classrefdef_rtti(def:tclassrefdef);
  590. begin
  591. write_header(def,tkClassRef);
  592. maybe_write_align;
  593. write_rtti_reference(def.pointeddef,rt);
  594. end;
  595. procedure pointerdef_rtti(def:tpointerdef);
  596. begin
  597. write_header(def,tkPointer);
  598. maybe_write_align;
  599. write_rtti_reference(def.pointeddef,rt);
  600. end;
  601. procedure recorddef_rtti(def:trecorddef);
  602. begin
  603. write_header(def,tkRecord);
  604. maybe_write_align;
  605. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  606. fields_write_rtti_data(def,rt);
  607. end;
  608. procedure procvardef_rtti(def:tprocvardef);
  609. const
  610. ProcCallOptionToCallConv: array[tproccalloption] of byte = (
  611. { pocall_none } 0,
  612. { pocall_cdecl } 1,
  613. { pocall_cppdecl } 5,
  614. { pocall_far16 } 6,
  615. { pocall_oldfpccall } 7,
  616. { pocall_internproc } 8,
  617. { pocall_syscall } 9,
  618. { pocall_pascal } 2,
  619. { pocall_register } 0,
  620. { pocall_safecall } 4,
  621. { pocall_stdcall } 3,
  622. { pocall_softfloat } 10,
  623. { pocall_mwpascal } 11,
  624. { pocall_interrupt } 12
  625. );
  626. procedure write_param_flag(parasym:tparavarsym);
  627. var
  628. paraspec : byte;
  629. begin
  630. case parasym.varspez of
  631. vs_value : paraspec := 0;
  632. vs_const : paraspec := pfConst;
  633. vs_var : paraspec := pfVar;
  634. vs_out : paraspec := pfOut;
  635. vs_constref: paraspec := pfConstRef;
  636. end;
  637. { Kylix also seems to always add both pfArray and pfReference
  638. in this case
  639. }
  640. if is_open_array(parasym.vardef) then
  641. paraspec:=paraspec or pfArray or pfReference;
  642. { and these for classes and interfaces (maybe because they
  643. are themselves addresses?)
  644. }
  645. if is_class_or_interface(parasym.vardef) then
  646. paraspec:=paraspec or pfAddress;
  647. { set bits run from the highest to the lowest bit on
  648. big endian systems
  649. }
  650. if (target_info.endian = endian_big) then
  651. paraspec:=reverse_byte(paraspec);
  652. { write flags for current parameter }
  653. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(paraspec));
  654. end;
  655. procedure write_para(parasym:tparavarsym);
  656. begin
  657. { only store user visible parameters }
  658. if not(vo_is_hidden_para in parasym.varoptions) then
  659. begin
  660. { write flags for current parameter }
  661. write_param_flag(parasym);
  662. { write name of current parameter }
  663. write_string(parasym.realname);
  664. { write name of type of current parameter }
  665. write_rtti_name(parasym.vardef);
  666. end;
  667. end;
  668. procedure write_procedure_param(parasym:tparavarsym);
  669. begin
  670. { only store user visible parameters }
  671. if not(vo_is_hidden_para in parasym.varoptions) then
  672. begin
  673. { write flags for current parameter }
  674. write_param_flag(parasym);
  675. { write param type }
  676. write_rtti_reference(parasym.vardef,fullrtti);
  677. { write name of current parameter }
  678. write_string(parasym.realname);
  679. end;
  680. end;
  681. var
  682. methodkind : byte;
  683. i : integer;
  684. begin
  685. if po_methodpointer in def.procoptions then
  686. begin
  687. { write method id and name }
  688. write_header(def,tkMethod);
  689. maybe_write_align;
  690. { write kind of method }
  691. case def.proctypeoption of
  692. potype_constructor: methodkind:=mkConstructor;
  693. potype_destructor: methodkind:=mkDestructor;
  694. potype_class_constructor: methodkind:=mkClassConstructor;
  695. potype_class_destructor: methodkind:=mkClassDestructor;
  696. potype_operator: methodkind:=mkOperatorOverload;
  697. potype_procedure:
  698. if po_classmethod in def.procoptions then
  699. methodkind:=mkClassProcedure
  700. else
  701. methodkind:=mkProcedure;
  702. potype_function:
  703. if po_classmethod in def.procoptions then
  704. methodkind:=mkClassFunction
  705. else
  706. methodkind:=mkFunction;
  707. else
  708. begin
  709. if def.returndef = voidtype then
  710. methodkind:=mkProcedure
  711. else
  712. methodkind:=mkFunction;
  713. end;
  714. end;
  715. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(methodkind));
  716. { write parameter info. The parameters must be written in reverse order
  717. if this method uses right to left parameter pushing! }
  718. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.maxparacount));
  719. for i:=0 to def.paras.count-1 do
  720. write_para(tparavarsym(def.paras[i]));
  721. if (methodkind=mkFunction) or (methodkind=mkClassFunction) then
  722. begin
  723. { write name of result type }
  724. write_rtti_name(def.returndef);
  725. maybe_write_align;
  726. { write result typeinfo }
  727. write_rtti_reference(def.returndef,fullrtti);
  728. end;
  729. { write calling convention }
  730. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(ProcCallOptionToCallConv[def.proccalloption]));
  731. maybe_write_align;
  732. { write params typeinfo }
  733. for i:=0 to def.paras.count-1 do
  734. if not(vo_is_hidden_para in tparavarsym(def.paras[i]).varoptions) then
  735. write_rtti_reference(tparavarsym(def.paras[i]).vardef,fullrtti);
  736. end
  737. else
  738. begin
  739. write_header(def,tkProcvar);
  740. maybe_write_align;
  741. { flags }
  742. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(0));
  743. maybe_write_align;
  744. { write calling convention }
  745. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(ProcCallOptionToCallConv[def.proccalloption]));
  746. maybe_write_align;
  747. { write result typeinfo }
  748. write_rtti_reference(def.returndef,fullrtti);
  749. { write parameter count }
  750. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.maxparacount));
  751. maybe_write_align;
  752. for i:=0 to def.paras.count-1 do
  753. write_procedure_param(tparavarsym(def.paras[i]));
  754. end;
  755. end;
  756. procedure objectdef_rtti(def:tobjectdef);
  757. procedure objectdef_rtti_fields(def:tobjectdef);
  758. begin
  759. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  760. fields_write_rtti_data(def,rt);
  761. end;
  762. procedure objectdef_rtti_interface_init(def:tobjectdef);
  763. begin
  764. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  765. end;
  766. procedure objectdef_rtti_class_full(def:tobjectdef);
  767. var
  768. propnamelist : TFPHashObjectList;
  769. begin
  770. { Collect unique property names with nameindex }
  771. propnamelist:=TFPHashObjectList.Create;
  772. collect_propnamelist(propnamelist,def);
  773. if not is_objectpascal_helper(def) then
  774. if (oo_has_vmt in def.objectoptions) then
  775. current_asmdata.asmlists[al_rtti].concat(Tai_const.Createname(def.vmt_mangledname,0))
  776. else
  777. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  778. { write parent typeinfo }
  779. write_rtti_reference(def.childof,fullrtti);
  780. { write typeinfo of extended type }
  781. if is_objectpascal_helper(def) then
  782. if assigned(def.extendeddef) then
  783. write_rtti_reference(def.extendeddef,fullrtti)
  784. else
  785. InternalError(2011033001);
  786. { total number of unique properties }
  787. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(propnamelist.count));
  788. { write unit name }
  789. write_string(current_module.realmodulename^);
  790. maybe_write_align;
  791. { write published properties for this object }
  792. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(published_properties_count(def.symtable)));
  793. maybe_write_align;
  794. published_properties_write_rtti_data(propnamelist,def.symtable);
  795. propnamelist.free;
  796. end;
  797. procedure objectdef_rtti_interface_full(def:tobjectdef);
  798. var
  799. i : longint;
  800. propnamelist : TFPHashObjectList;
  801. { if changed to a set, make sure it's still a byte large, and
  802. swap appropriately when cross-compiling
  803. }
  804. IntfFlags: byte;
  805. begin
  806. { Collect unique property names with nameindex }
  807. propnamelist:=TFPHashObjectList.Create;
  808. collect_propnamelist(propnamelist,def);
  809. { write parent typeinfo }
  810. write_rtti_reference(def.childof,fullrtti);
  811. { interface: write flags, iid and iidstr }
  812. IntfFlags:=0;
  813. if assigned(def.iidguid) then
  814. IntfFlags:=IntfFlags or (1 shl ord(ifHasGuid));
  815. if assigned(def.iidstr) then
  816. IntfFlags:=IntfFlags or (1 shl ord(ifHasStrGUID));
  817. if (def.objecttype=odt_dispinterface) then
  818. IntfFlags:=IntfFlags or (1 shl ord(ifDispInterface));
  819. if (target_info.endian=endian_big) then
  820. IntfFlags:=reverse_byte(IntfFlags);
  821. {
  822. ifDispatch, }
  823. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(IntfFlags));
  824. maybe_write_align;
  825. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(def.iidguid^.D1)));
  826. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(def.iidguid^.D2));
  827. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(def.iidguid^.D3));
  828. for i:=Low(def.iidguid^.D4) to High(def.iidguid^.D4) do
  829. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.iidguid^.D4[i]));
  830. { write unit name }
  831. write_string(current_module.realmodulename^);
  832. maybe_write_align;
  833. { write iidstr }
  834. if assigned(def.iidstr) then
  835. write_string(def.iidstr^)
  836. else
  837. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(0));
  838. maybe_write_align;
  839. { write published properties for this object }
  840. published_properties_write_rtti_data(propnamelist,def.symtable);
  841. propnamelist.free;
  842. end;
  843. begin
  844. case def.objecttype of
  845. odt_class:
  846. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkclass));
  847. odt_object:
  848. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkobject));
  849. odt_dispinterface,
  850. odt_interfacecom:
  851. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkinterface));
  852. odt_interfacecorba:
  853. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkinterfaceCorba));
  854. odt_helper:
  855. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkhelper));
  856. else
  857. internalerror(200611034);
  858. end;
  859. { generate the name }
  860. write_string(def.objrealname^);
  861. maybe_write_align;
  862. case rt of
  863. initrtti :
  864. begin
  865. if def.objecttype in [odt_class,odt_object,odt_helper] then
  866. objectdef_rtti_fields(def)
  867. else
  868. objectdef_rtti_interface_init(def);
  869. end;
  870. fullrtti :
  871. begin
  872. case def.objecttype of
  873. odt_helper,
  874. odt_class:
  875. objectdef_rtti_class_full(def);
  876. odt_object:
  877. objectdef_rtti_fields(def);
  878. else
  879. objectdef_rtti_interface_full(def);
  880. end;
  881. end;
  882. end;
  883. end;
  884. begin
  885. case def.typ of
  886. variantdef :
  887. variantdef_rtti(tvariantdef(def));
  888. stringdef :
  889. stringdef_rtti(tstringdef(def));
  890. enumdef :
  891. enumdef_rtti(tenumdef(def));
  892. orddef :
  893. orddef_rtti(torddef(def));
  894. floatdef :
  895. floatdef_rtti(tfloatdef(def));
  896. setdef :
  897. setdef_rtti(tsetdef(def));
  898. procvardef :
  899. procvardef_rtti(tprocvardef(def));
  900. arraydef :
  901. begin
  902. if ado_IsBitPacked in tarraydef(def).arrayoptions then
  903. unknown_rtti(tstoreddef(def))
  904. else
  905. arraydef_rtti(tarraydef(def));
  906. end;
  907. recorddef :
  908. begin
  909. if trecorddef(def).is_packed then
  910. unknown_rtti(tstoreddef(def))
  911. else
  912. recorddef_rtti(trecorddef(def));
  913. end;
  914. objectdef :
  915. objectdef_rtti(tobjectdef(def));
  916. classrefdef :
  917. classrefdef_rtti(tclassrefdef(def));
  918. pointerdef :
  919. pointerdef_rtti(tpointerdef(def));
  920. else
  921. unknown_rtti(tstoreddef(def));
  922. end;
  923. end;
  924. procedure TRTTIWriter.write_rtti_extrasyms(def:Tdef;rt:Trttitype;mainrtti:Tasmsymbol);
  925. type Penumsym = ^Tenumsym;
  926. function enumdef_rtti_calcstringtablestart(const def : Tenumdef) : integer;
  927. begin
  928. { the alignment calls must correspond to the ones used during generating the
  929. actual data structure created elsewhere in this file }
  930. result:=1;
  931. if assigned(def.typesym) then
  932. inc(result,length(def.typesym.realname)+1)
  933. else
  934. inc(result);
  935. if (tf_requires_proper_alignment in target_info.flags) then
  936. result:=align(result,sizeof(Tconstptruint));
  937. inc(result);
  938. if (tf_requires_proper_alignment in target_info.flags) then
  939. result:=align(result,sizeof(Tconstptruint));
  940. inc(result, sizeof(longint) * 2);
  941. if (tf_requires_proper_alignment in target_info.flags) then
  942. result:=align(result,sizeof(Tconstptruint));
  943. inc(result, sizeof(pint));
  944. end;
  945. { Writes a helper table for accelerated conversion of ordinal enum values to strings.
  946. If you change something in this method, make sure to adapt the corresponding code
  947. in sstrings.inc. }
  948. procedure enumdef_rtti_ord2stringindex(const sym_count:longint; const offsets:plongint; const syms:Penumsym; const st:longint);
  949. var rttilab:Tasmsymbol;
  950. h,i,o,prev_value:longint;
  951. mode:(lookup,search); {Modify with care, ordinal value of enum is written.}
  952. r:single; {Must be real type because of integer overflow risk.}
  953. begin
  954. {Decide wether a lookup array is size efficient.}
  955. mode:=lookup;
  956. if sym_count>0 then
  957. begin
  958. i:=1;
  959. r:=0;
  960. h:=syms[0].value; {Next expected enum value is min.}
  961. { set prev_value for the first iteration to a value that is
  962. different from the first one without risking overflow (it's used
  963. to detect whether two enum values are the same) }
  964. if h=0 then
  965. prev_value:=1
  966. else
  967. prev_value:=0;
  968. while i<sym_count do
  969. begin
  970. { if two enum values are the same, we have to create a table }
  971. if (prev_value=h) then
  972. begin
  973. mode:=search;
  974. break;
  975. end;
  976. {Calculate size of hole between values. Avoid integer overflows.}
  977. r:=r+(single(syms[i].value)-single(h))-1;
  978. prev_value:=h;
  979. h:=syms[i].value;
  980. inc(i);
  981. end;
  982. if r>sym_count then
  983. mode:=search; {Don't waste more than 50% space.}
  984. end;
  985. { write rtti data; make sure that the alignment matches the corresponding data structure
  986. in the code that uses it (if alignment is required). }
  987. with current_asmdata do
  988. begin
  989. rttilab:=defineasmsymbol(Tstoreddef(def).rtti_mangledname(rt)+'_o2s',AB_GLOBAL,AT_DATA);
  990. maybe_new_object_file(asmlists[al_rtti]);
  991. new_section(asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(pint)));
  992. asmlists[al_rtti].concat(Tai_symbol.create_global(rttilab,0));
  993. asmlists[al_rtti].concat(Tai_const.create_32bit(longint(mode)));
  994. if mode=lookup then
  995. begin
  996. maybe_write_align;
  997. o:=syms[0].value; {Start with min value.}
  998. for i:=0 to sym_count-1 do
  999. begin
  1000. while o<syms[i].value do
  1001. begin
  1002. asmlists[al_rtti].concat(Tai_const.create_pint(0));
  1003. inc(o);
  1004. end;
  1005. inc(o);
  1006. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  1007. end;
  1008. end
  1009. else
  1010. begin
  1011. maybe_write_align;
  1012. asmlists[al_rtti].concat(Tai_const.create_32bit(sym_count));
  1013. for i:=0 to sym_count-1 do
  1014. begin
  1015. maybe_write_align;
  1016. asmlists[al_rtti].concat(Tai_const.create_32bit(syms[i].value));
  1017. maybe_write_align;
  1018. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  1019. end;
  1020. end;
  1021. asmlists[al_rtti].concat(Tai_symbol_end.create(rttilab));
  1022. end;
  1023. end;
  1024. { Writes a helper table for accelerated conversion of string to ordinal enum values.
  1025. If you change something in this method, make sure to adapt the corresponding code
  1026. in sstrings.inc. }
  1027. procedure enumdef_rtti_string2ordindex(const sym_count:longint; const offsets:plongint; const syms:Penumsym; const st:longint);
  1028. var rttilab:Tasmsymbol;
  1029. i:longint;
  1030. begin
  1031. { write rtti data }
  1032. with current_asmdata do
  1033. begin
  1034. rttilab:=defineasmsymbol(Tstoreddef(def).rtti_mangledname(rt)+'_s2o',AB_GLOBAL,AT_DATA);
  1035. maybe_new_object_file(asmlists[al_rtti]);
  1036. new_section(asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(pint)));
  1037. asmlists[al_rtti].concat(Tai_symbol.create_global(rttilab,0));
  1038. asmlists[al_rtti].concat(Tai_const.create_32bit(sym_count));
  1039. { need to align the entry record according to the largest member }
  1040. maybe_write_align;
  1041. for i:=0 to sym_count-1 do
  1042. begin
  1043. if (tf_requires_proper_alignment in target_info.flags) then
  1044. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(4)); // necessary?
  1045. asmlists[al_rtti].concat(Tai_const.create_32bit(syms[i].value));
  1046. maybe_write_align;
  1047. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  1048. end;
  1049. asmlists[al_rtti].concat(Tai_symbol_end.create(rttilab));
  1050. end;
  1051. end;
  1052. procedure enumdef_rtti_extrasyms(def:Tenumdef);
  1053. var
  1054. t:Tenumsym;
  1055. syms:Penumsym;
  1056. sym_count,sym_alloc:sizeuint;
  1057. offsets:^longint;
  1058. h,i,p,o,st:longint;
  1059. begin
  1060. {Random access needed, put in array.}
  1061. getmem(syms,64*sizeof(Tenumsym));
  1062. getmem(offsets,64*sizeof(longint));
  1063. sym_count:=0;
  1064. sym_alloc:=64;
  1065. st:=0;
  1066. for i := 0 to def.symtable.SymList.Count - 1 do
  1067. begin
  1068. t:=tenumsym(def.symtable.SymList[i]);
  1069. if t.value<def.minval then
  1070. continue
  1071. else
  1072. if t.value>def.maxval then
  1073. break;
  1074. if sym_count>=sym_alloc then
  1075. begin
  1076. reallocmem(syms,2*sym_alloc*sizeof(Tenumsym));
  1077. reallocmem(offsets,2*sym_alloc*sizeof(longint));
  1078. sym_alloc:=sym_alloc*2;
  1079. end;
  1080. syms[sym_count]:=t;
  1081. offsets[sym_count]:=st;
  1082. inc(sym_count);
  1083. st:=st+length(t.realname)+1;
  1084. end;
  1085. {Sort the syms by enum name}
  1086. if sym_count>=2 then
  1087. begin
  1088. p:=1;
  1089. while 2*p<sym_count do
  1090. p:=2*p;
  1091. while p<>0 do
  1092. begin
  1093. for h:=p to sym_count-1 do
  1094. begin
  1095. i:=h;
  1096. t:=syms[i];
  1097. o:=offsets[i];
  1098. repeat
  1099. if syms[i-p].name<=t.name then
  1100. break;
  1101. syms[i]:=syms[i-p];
  1102. offsets[i]:=offsets[i-p];
  1103. dec(i,p);
  1104. until i<p;
  1105. syms[i]:=t;
  1106. offsets[i]:=o;
  1107. end;
  1108. p:=p shr 1;
  1109. end;
  1110. end;
  1111. st:=enumdef_rtti_calcstringtablestart(def);
  1112. enumdef_rtti_string2ordindex(sym_count,offsets,syms,st);
  1113. { Sort the syms by enum value }
  1114. if sym_count>=2 then
  1115. begin
  1116. p:=1;
  1117. while 2*p<sym_count do
  1118. p:=2*p;
  1119. while p<>0 do
  1120. begin
  1121. for h:=p to sym_count-1 do
  1122. begin
  1123. i:=h;
  1124. t:=syms[i];
  1125. o:=offsets[i];
  1126. repeat
  1127. if syms[i-p].value<=t.value then
  1128. break;
  1129. syms[i]:=syms[i-p];
  1130. offsets[i]:=offsets[i-p];
  1131. dec(i,p);
  1132. until i<p;
  1133. syms[i]:=t;
  1134. offsets[i]:=o;
  1135. end;
  1136. p:=p shr 1;
  1137. end;
  1138. end;
  1139. enumdef_rtti_ord2stringindex(sym_count,offsets,syms,st);
  1140. freemem(syms);
  1141. freemem(offsets);
  1142. end;
  1143. begin
  1144. case def.typ of
  1145. enumdef:
  1146. if rt=fullrtti then
  1147. begin
  1148. enumdef_rtti_extrasyms(Tenumdef(def));
  1149. end;
  1150. end;
  1151. end;
  1152. procedure TRTTIWriter.write_child_rtti_data(def:tdef;rt:trttitype);
  1153. begin
  1154. case def.typ of
  1155. enumdef :
  1156. if assigned(tenumdef(def).basedef) then
  1157. write_rtti(tenumdef(def).basedef,rt);
  1158. setdef :
  1159. write_rtti(tsetdef(def).elementdef,rt);
  1160. arraydef :
  1161. begin
  1162. write_rtti(tarraydef(def).rangedef,rt);
  1163. write_rtti(tarraydef(def).elementdef,rt);
  1164. end;
  1165. recorddef :
  1166. fields_write_rtti(trecorddef(def).symtable,rt);
  1167. objectdef :
  1168. begin
  1169. if assigned(tobjectdef(def).childof) then
  1170. write_rtti(tobjectdef(def).childof,rt);
  1171. if (rt=initrtti) or (tobjectdef(def).objecttype=odt_object) then
  1172. fields_write_rtti(tobjectdef(def).symtable,rt)
  1173. else
  1174. published_write_rtti(tobjectdef(def).symtable,rt);
  1175. end;
  1176. classrefdef,
  1177. pointerdef:
  1178. if not is_objc_class_or_protocol(tabstractpointerdef(def).pointeddef) then
  1179. write_rtti(tabstractpointerdef(def).pointeddef,rt);
  1180. end;
  1181. end;
  1182. procedure TRTTIWriter.write_rtti_reference(def:tdef;rt:trttitype);
  1183. begin
  1184. if not assigned(def) or is_void(def) or is_objc_class_or_protocol(def) then
  1185. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(nil))
  1186. else
  1187. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def,rt)));
  1188. end;
  1189. function TRTTIWriter.ref_rtti(def:tdef;rt:trttitype):tasmsymbol;
  1190. begin
  1191. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt));
  1192. if (cs_create_pic in current_settings.moduleswitches) and
  1193. assigned(current_procinfo) then
  1194. include(current_procinfo.flags,pi_needs_got);
  1195. end;
  1196. procedure TRTTIWriter.write_rtti(def:tdef;rt:trttitype);
  1197. var
  1198. rttilab : tasmsymbol;
  1199. begin
  1200. { only write rtti of definitions from the current module }
  1201. if not findunitsymtable(def.owner).iscurrentunit then
  1202. exit;
  1203. { check if separate initrtti is actually needed }
  1204. if (rt=initrtti) and (not def.needs_separate_initrtti) then
  1205. rt:=fullrtti;
  1206. { prevent recursion }
  1207. if rttidefstate[rt] in def.defstates then
  1208. exit;
  1209. include(def.defstates,rttidefstate[rt]);
  1210. { write first all dependencies }
  1211. write_child_rtti_data(def,rt);
  1212. { write rtti data }
  1213. rttilab:=current_asmdata.DefineAsmSymbol(tstoreddef(def).rtti_mangledname(rt),AB_GLOBAL,AT_DATA);
  1214. maybe_new_object_file(current_asmdata.asmlists[al_rtti]);
  1215. new_section(current_asmdata.asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(pint)));
  1216. current_asmdata.asmlists[al_rtti].concat(Tai_symbol.Create_global(rttilab,0));
  1217. write_rtti_data(def,rt);
  1218. current_asmdata.asmlists[al_rtti].concat(Tai_symbol_end.Create(rttilab));
  1219. write_rtti_extrasyms(def,rt,rttilab);
  1220. end;
  1221. function TRTTIWriter.get_rtti_label(def:tdef;rt:trttitype):tasmsymbol;
  1222. begin
  1223. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt));
  1224. if (cs_create_pic in current_settings.moduleswitches) and
  1225. assigned(current_procinfo) then
  1226. include(current_procinfo.flags,pi_needs_got);
  1227. end;
  1228. function TRTTIWriter.get_rtti_label_ord2str(def:tdef;rt:trttitype):tasmsymbol;
  1229. begin
  1230. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt)+'_o2s');
  1231. if (cs_create_pic in current_settings.moduleswitches) and
  1232. assigned(current_procinfo) then
  1233. include(current_procinfo.flags,pi_needs_got);
  1234. end;
  1235. function TRTTIWriter.get_rtti_label_str2ord(def:tdef;rt:trttitype):tasmsymbol;
  1236. begin
  1237. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt)+'_s2o');
  1238. if (cs_create_pic in current_settings.moduleswitches) and
  1239. assigned(current_procinfo) then
  1240. include(current_procinfo.flags,pi_needs_got);
  1241. end;
  1242. end.