ncgrtti.pas 51 KB

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