ncgrtti.pas 60 KB

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