ncgrtti.pas 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277
  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,globtype,
  22. aasmbase,aasmcnst,
  23. symbase,symconst,symtype,symdef,symsym,
  24. parabase;
  25. type
  26. { TRTTIWriter }
  27. TRTTIWriter=class
  28. private
  29. { required internal alignment of the rtti data }
  30. reqalign: shortint;
  31. { required packing of all structures except for ttypeinfo and tpropinfo,
  32. which always use packrecords 1 }
  33. defaultpacking: shortint;
  34. procedure fields_write_rtti(st:tsymtable;rt:trttitype);
  35. procedure params_write_rtti(def:tabstractprocdef;rt:trttitype;allow_hidden:boolean);
  36. procedure fields_write_rtti_data(tcb: ttai_typedconstbuilder; def: tabstractrecorddef; rt: trttitype);
  37. procedure methods_write_rtti(st:tsymtable;rt:trttitype;visibilities:tvisibilities;allow_hidden:boolean);
  38. procedure write_rtti_extrasyms(def:Tdef;rt:Trttitype;mainrtti:Tasmsymbol);
  39. procedure published_write_rtti(st:tsymtable;rt:trttitype);
  40. function published_properties_count(st:tsymtable):longint;
  41. procedure published_properties_write_rtti_data(tcb: ttai_typedconstbuilder; propnamelist: TFPHashObjectList; st: tsymtable);
  42. procedure collect_propnamelist(propnamelist:TFPHashObjectList;objdef:tobjectdef);
  43. { only use a direct reference if the referenced type can *only* reside
  44. in the same unit as the current one }
  45. function ref_rtti(def:tdef;rt:trttitype;indirect:boolean;suffix:tsymstr):tasmsymbol;
  46. procedure write_rtti_name(tcb: ttai_typedconstbuilder; def: tdef);
  47. procedure write_common_rtti_data(tcb:ttai_typedconstbuilder;def:tdef;rt:trttitype);
  48. procedure write_rtti_data(tcb: ttai_typedconstbuilder; def:tdef; rt: trttitype);
  49. procedure write_attribute_data(tcb: ttai_typedconstbuilder;attr_list:trtti_attribute_list);
  50. procedure write_child_rtti_data(def:tdef;rt:trttitype);
  51. procedure write_rtti_reference(tcb: ttai_typedconstbuilder; def: tdef; rt: trttitype);
  52. procedure write_methods(tcb:ttai_typedconstbuilder;st:tsymtable;visibilities:tvisibilities);
  53. procedure write_header(tcb: ttai_typedconstbuilder; def: tdef; typekind: byte);
  54. function write_methodkind(tcb:ttai_typedconstbuilder;def:tabstractprocdef):byte;
  55. procedure write_callconv(tcb:ttai_typedconstbuilder;def:tabstractprocdef);
  56. procedure write_paralocs(tcb:ttai_typedconstbuilder;para:pcgpara);
  57. procedure write_param_flag(tcb:ttai_typedconstbuilder;parasym:tparavarsym);
  58. procedure write_mop_offset_table(tcb:ttai_typedconstbuilder;def:tabstractrecorddef;mop:tmanagementoperator);
  59. public
  60. constructor create;
  61. procedure write_rtti(def:tdef;rt:trttitype);
  62. function get_rtti_label(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol; inline;
  63. function get_rtti_label_ord2str(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol; inline;
  64. function get_rtti_label_str2ord(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol; inline;
  65. end;
  66. { generate RTTI and init tables }
  67. procedure write_persistent_type_info(st:tsymtable;is_global:boolean);
  68. var
  69. RTTIWriter : TRTTIWriter;
  70. implementation
  71. uses
  72. cutils,
  73. globals,verbose,systems,
  74. node,ncal,ncon,
  75. fmodule, procinfo,
  76. symtable,
  77. aasmtai,aasmdata,
  78. defutil,
  79. paramgr
  80. ;
  81. const
  82. rttidefstate : array[trttitype] of tdefstate =
  83. (ds_rtti_table_written,ds_init_table_written,
  84. { Objective-C related, does not pass here }
  85. symconst.ds_none,symconst.ds_none,
  86. symconst.ds_none,symconst.ds_none);
  87. type
  88. TPropNameListItem = class(TFPHashObject)
  89. propindex : longint;
  90. propowner : TSymtable;
  91. end;
  92. procedure write_persistent_type_info(st: tsymtable; is_global: boolean);
  93. var
  94. i : longint;
  95. def : tdef;
  96. begin
  97. { no Delphi-style RTTI for managed platforms }
  98. if target_info.system in systems_managed_vm then
  99. exit;
  100. for i:=0 to st.DefList.Count-1 do
  101. begin
  102. def:=tdef(st.DefList[i]);
  103. { skip generics }
  104. if [df_generic,df_genconstraint]*def.defoptions<>[] then
  105. continue;
  106. case def.typ of
  107. recorddef:
  108. write_persistent_type_info(trecorddef(def).symtable,is_global);
  109. objectdef :
  110. begin
  111. { Skip forward defs }
  112. if (oo_is_forward in tobjectdef(def).objectoptions) then
  113. continue;
  114. write_persistent_type_info(tobjectdef(def).symtable,is_global);
  115. end;
  116. procdef :
  117. begin
  118. if assigned(tprocdef(def).localst) and
  119. (tprocdef(def).localst.symtabletype=localsymtable) then
  120. write_persistent_type_info(tprocdef(def).localst,false);
  121. if assigned(tprocdef(def).parast) then
  122. write_persistent_type_info(tprocdef(def).parast,false);
  123. end;
  124. errordef:
  125. { we shouldn't have come this far if we have an errordef somewhere }
  126. internalerror(2017010701);
  127. undefineddef:
  128. { don't write any RTTI for these }
  129. continue;
  130. else
  131. ;
  132. end;
  133. { always generate persistent tables for types in the interface so
  134. they can be reused in other units and give always the same pointer
  135. location. }
  136. { Init }
  137. if (
  138. assigned(def.typesym) and
  139. is_global and
  140. not is_objc_class_or_protocol(def)
  141. ) or
  142. is_managed_type(def) or
  143. (ds_init_table_used in def.defstates) then
  144. RTTIWriter.write_rtti(def,initrtti);
  145. { RTTI }
  146. if (
  147. assigned(def.typesym) and
  148. is_global and
  149. not is_objc_class_or_protocol(def)
  150. ) or
  151. (ds_rtti_table_used in def.defstates) then
  152. RTTIWriter.write_rtti(def,fullrtti);
  153. end;
  154. end;
  155. {***************************************************************************
  156. TRTTIWriter
  157. ***************************************************************************}
  158. function TRTTIWriter.get_rtti_label(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  159. begin
  160. result:=ref_rtti(def,rt,indirect,'');
  161. end;
  162. function TRTTIWriter.get_rtti_label_ord2str(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  163. begin
  164. result:=ref_rtti(def,rt,indirect,'_o2s');
  165. end;
  166. function TRTTIWriter.get_rtti_label_str2ord(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  167. begin
  168. result:=ref_rtti(def,rt,indirect,'_s2o');
  169. end;
  170. procedure TRTTIWriter.write_methods(tcb:ttai_typedconstbuilder;st:tsymtable;visibilities:tvisibilities);
  171. var
  172. rtticount,
  173. totalcount,
  174. i,j,k : longint;
  175. sym : tprocsym;
  176. def : tprocdef;
  177. para : tparavarsym;
  178. begin
  179. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,SizeOf(PInt)),
  180. targetinfos[target_info.system]^.alignment.recordalignmin,
  181. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  182. totalcount:=0;
  183. rtticount:=0;
  184. for i:=0 to st.symlist.count-1 do
  185. if tsym(st.symlist[i]).typ=procsym then
  186. begin
  187. sym:=tprocsym(st.symlist[i]);
  188. inc(totalcount,sym.procdeflist.count);
  189. for j:=0 to sym.procdeflist.count-1 do
  190. if tprocdef(sym.procdeflist[j]).visibility in visibilities then
  191. inc(rtticount);
  192. end;
  193. tcb.emit_ord_const(totalcount,u16inttype);
  194. if rtticount = 0 then
  195. tcb.emit_ord_const($FFFF,u16inttype)
  196. else
  197. begin
  198. tcb.emit_ord_const(rtticount,u16inttype);
  199. for i:=0 to st.symlist.count-1 do
  200. if tsym(st.symlist[i]).typ=procsym then
  201. begin
  202. sym:=tprocsym(st.symlist[i]);
  203. for j:=0 to sym.procdeflist.count-1 do
  204. begin
  205. def:=tprocdef(sym.procdeflist[j]);
  206. if not (def.visibility in visibilities) then
  207. continue;
  208. def.init_paraloc_info(callerside);
  209. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,SizeOf(PInt)),
  210. targetinfos[target_info.system]^.alignment.recordalignmin,
  211. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  212. write_rtti_reference(tcb,def.returndef,fullrtti);
  213. write_callconv(tcb,def);
  214. write_methodkind(tcb,def);
  215. tcb.emit_ord_const(def.paras.count,u16inttype);
  216. tcb.emit_ord_const(def.callerargareasize,ptrsinttype);
  217. tcb.emit_pooled_shortstring_const_ref(sym.realname);
  218. for k:=0 to def.paras.count-1 do
  219. begin
  220. para:=tparavarsym(def.paras[k]);
  221. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,SizeOf(PInt)),
  222. targetinfos[target_info.system]^.alignment.recordalignmin,
  223. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  224. if is_open_array(para.vardef) or is_array_of_const(para.vardef) then
  225. write_rtti_reference(tcb,tarraydef(para.vardef).elementdef,fullrtti)
  226. else if para.vardef=cformaltype then
  227. write_rtti_reference(tcb,nil,fullrtti)
  228. else
  229. write_rtti_reference(tcb,para.vardef,fullrtti);
  230. write_param_flag(tcb,para);
  231. tcb.emit_pooled_shortstring_const_ref(para.realname);
  232. write_paralocs(tcb,@para.paraloc[callerside]);
  233. tcb.end_anonymous_record;
  234. end;
  235. if not is_void(def.returndef) then
  236. write_paralocs(tcb,@def.funcretloc[callerside]);
  237. tcb.end_anonymous_record;
  238. end;
  239. end;
  240. end;
  241. tcb.end_anonymous_record;
  242. end;
  243. procedure TRTTIWriter.write_header(tcb: ttai_typedconstbuilder; def: tdef; typekind: byte);
  244. var
  245. name: shortstring;
  246. begin
  247. if assigned(def.typesym) then
  248. name:=ttypesym(def.typesym).realname
  249. else
  250. name:='';
  251. { TTypeInfo, always packed and doesn't need alignment }
  252. tcb.begin_anonymous_record(
  253. internaltypeprefixName[itp_rtti_header]+tostr(length(name)),1,1,
  254. targetinfos[target_info.system]^.alignment.recordalignmin,
  255. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  256. if def.typ=arraydef then
  257. InternalError(201012211);
  258. tcb.emit_tai(Tai_const.Create_8bit(typekind),u8inttype);
  259. tcb.emit_shortstring_const(name);
  260. tcb.end_anonymous_record;
  261. end;
  262. function TRTTIWriter.write_methodkind(tcb:ttai_typedconstbuilder;def:tabstractprocdef):byte;
  263. begin
  264. case def.proctypeoption of
  265. potype_constructor: result:=mkConstructor;
  266. potype_destructor: result:=mkDestructor;
  267. potype_class_constructor: result:=mkClassConstructor;
  268. potype_class_destructor: result:=mkClassDestructor;
  269. potype_operator: result:=mkOperatorOverload;
  270. potype_procedure:
  271. if po_classmethod in def.procoptions then
  272. result:=mkClassProcedure
  273. else
  274. result:=mkProcedure;
  275. potype_function:
  276. if po_classmethod in def.procoptions then
  277. result:=mkClassFunction
  278. else
  279. result:=mkFunction;
  280. else
  281. begin
  282. if def.returndef = voidtype then
  283. result:=mkProcedure
  284. else
  285. result:=mkFunction;
  286. end;
  287. end;
  288. tcb.emit_ord_const(result,u8inttype);
  289. end;
  290. procedure TRTTIWriter.write_callconv(tcb:ttai_typedconstbuilder;def:tabstractprocdef);
  291. const
  292. ProcCallOptionToCallConv: array[tproccalloption] of byte = (
  293. { pocall_none } 0,
  294. { pocall_cdecl } 1,
  295. { pocall_cppdecl } 5,
  296. { pocall_far16 } 6,
  297. { pocall_oldfpccall } 7,
  298. { pocall_internproc } 8,
  299. { pocall_syscall } 9,
  300. { pocall_pascal } 2,
  301. { pocall_register } 0,
  302. { pocall_safecall } 4,
  303. { pocall_stdcall } 3,
  304. { pocall_softfloat } 10,
  305. { pocall_mwpascal } 11,
  306. { pocall_interrupt } 12,
  307. { pocall_hardfloat } 13,
  308. { pocall_sysv_abi_default } 14,
  309. { pocall_sysv_abi_cdecl } 15,
  310. { pocall_ms_abi_default } 16,
  311. { pocall_ms_abi_cdecl } 17,
  312. { pocall_vectorcall } 18
  313. );
  314. begin
  315. tcb.emit_ord_const(ProcCallOptionToCallConv[def.proccalloption],u8inttype);
  316. end;
  317. procedure TRTTIWriter.write_paralocs(tcb:ttai_typedconstbuilder;para:pcgpara);
  318. var
  319. locs : trttiparalocs;
  320. i : longint;
  321. pool : THashSet;
  322. entry : PHashSetItem;
  323. loclab : TAsmLabel;
  324. loctcb : ttai_typedconstbuilder;
  325. datadef : tdef;
  326. begin
  327. locs:=paramanager.cgparalocs_to_rttiparalocs(para^.location);
  328. if length(locs)>high(byte) then
  329. internalerror(2017010601);
  330. if length(locs)=0 then
  331. begin
  332. { *shrugs* }
  333. tcb.emit_tai(Tai_const.Create_nil_codeptr,voidpointertype);
  334. exit;
  335. end;
  336. { do we have such a paraloc already in the pool? }
  337. pool:=current_asmdata.ConstPools[sp_paraloc];
  338. entry:=pool.FindOrAdd(@locs[0],length(locs)*sizeof(trttiparaloc));
  339. if not assigned(entry^.Data) then
  340. begin
  341. current_asmdata.getglobaldatalabel(loclab);
  342. loctcb:=ctai_typedconstbuilder.create([tcalo_is_lab,tcalo_make_dead_strippable,tcalo_apply_constalign]);
  343. loctcb.begin_anonymous_record('',defaultpacking,min(reqalign,SizeOf(PInt)),
  344. targetinfos[target_info.system]^.alignment.recordalignmin,
  345. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  346. loctcb.emit_ord_const(length(locs),u8inttype);
  347. for i:=low(locs) to high(locs) do
  348. begin
  349. loctcb.begin_anonymous_record('',defaultpacking,min(reqalign,SizeOf(PInt)),
  350. targetinfos[target_info.system]^.alignment.recordalignmin,
  351. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  352. loctcb.emit_ord_const(locs[i].loctype,u8inttype);
  353. loctcb.emit_ord_const(locs[i].regsub,u8inttype);
  354. loctcb.emit_ord_const(locs[i].regindex,u16inttype);
  355. { the corresponding type for aint is alusinttype }
  356. loctcb.emit_ord_const(locs[i].offset,alusinttype);
  357. loctcb.end_anonymous_record;
  358. end;
  359. datadef:=loctcb.end_anonymous_record;
  360. current_asmdata.asmlists[al_typedconsts].concatList(
  361. loctcb.get_final_asmlist(loclab,datadef,sec_rodata_norel,loclab.name,const_align(sizeof(pint)))
  362. );
  363. loctcb.free;
  364. entry^.data:=loclab;
  365. end
  366. else
  367. loclab:=TAsmLabel(entry^.Data);
  368. tcb.emit_tai(Tai_const.Create_sym(loclab),voidpointertype);
  369. end;
  370. procedure TRTTIWriter.write_param_flag(tcb:ttai_typedconstbuilder;parasym:tparavarsym);
  371. var
  372. paraspec : word;
  373. begin
  374. case parasym.varspez of
  375. vs_value : paraspec := 0;
  376. vs_const : paraspec := pfConst;
  377. vs_var : paraspec := pfVar;
  378. vs_out : paraspec := pfOut;
  379. vs_constref: paraspec := pfConstRef;
  380. else
  381. internalerror(2013112904);
  382. end;
  383. { Kylix also seems to always add both pfArray and pfReference
  384. in this case
  385. }
  386. if is_open_array(parasym.vardef) or is_array_of_const(parasym.vardef) then
  387. paraspec:=paraspec or pfArray or pfReference;
  388. { and these for classes and interfaces (maybe because they
  389. are themselves addresses?)
  390. }
  391. if is_class_or_interface(parasym.vardef) then
  392. paraspec:=paraspec or pfAddress;
  393. { flags for the hidden parameters }
  394. if vo_is_hidden_para in parasym.varoptions then
  395. paraspec:=paraspec or pfHidden;
  396. if vo_is_high_para in parasym.varoptions then
  397. paraspec:=paraspec or pfHigh;
  398. if vo_is_self in parasym.varoptions then
  399. paraspec:=paraspec or pfSelf;
  400. if vo_is_vmt in parasym.varoptions then
  401. paraspec:=paraspec or pfVmt;
  402. if vo_is_funcret in parasym.varoptions then
  403. paraspec:=paraspec or pfResult;
  404. { set bits run from the highest to the lowest bit on
  405. big endian systems
  406. }
  407. if (target_info.endian = endian_big) then
  408. paraspec:=reverse_word(paraspec);
  409. { write flags for current parameter }
  410. tcb.emit_ord_const(paraspec,u16inttype);
  411. end;
  412. function compare_mop_offset_entry(item1,item2:pointer):longint;
  413. var
  414. entry1: pmanagementoperator_offset_entry absolute item1;
  415. entry2: pmanagementoperator_offset_entry absolute item2;
  416. begin
  417. if entry1^.offset<entry2^.offset then
  418. result:=-1
  419. else if entry1^.offset>entry2^.offset then
  420. result:=1
  421. else
  422. result:=0;
  423. end;
  424. procedure TRTTIWriter.write_mop_offset_table(tcb:ttai_typedconstbuilder;def:tabstractrecorddef;mop:tmanagementoperator);
  425. var
  426. list : tfplist;
  427. datatcb : ttai_typedconstbuilder;
  428. tbllbl : TAsmLabel;
  429. entry : pmanagementoperator_offset_entry;
  430. datadef,entrydef : tdef;
  431. i : longint;
  432. pdef : tobjectdef;
  433. begin
  434. list:=tfplist.create;
  435. tabstractrecordsymtable(def.symtable).get_managementoperator_offset_list(mop,list);
  436. if (def.typ=objectdef) then
  437. begin
  438. pdef:=tobjectdef(def).childof;
  439. while assigned(pdef) do
  440. begin
  441. tabstractrecordsymtable(pdef.symtable).get_managementoperator_offset_list(mop,list);
  442. pdef:=pdef.childof;
  443. end;
  444. list.sort(@compare_mop_offset_entry);
  445. end;
  446. if list.count=0 then
  447. tcb.emit_tai(tai_const.create_nil_dataptr,voidpointertype)
  448. else
  449. begin
  450. tcb.start_internal_data_builder(current_asmdata.AsmLists[al_rtti],sec_rodata,'',datatcb,tbllbl);
  451. datatcb.begin_anonymous_record('',defaultpacking,min(reqalign,SizeOf(PInt)),
  452. targetinfos[target_info.system]^.alignment.recordalignmin,
  453. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  454. datatcb.emit_ord_const(list.count,u32inttype);
  455. entrydef:=get_recorddef(itp_init_mop_offset_entry,[voidcodepointertype,sizeuinttype],defaultpacking);
  456. for i:=0 to list.count-1 do
  457. begin
  458. entry:=pmanagementoperator_offset_entry(list[i]);
  459. datatcb.maybe_begin_aggregate(entrydef);
  460. datatcb.queue_init(voidcodepointertype);
  461. datatcb.queue_emit_proc(entry^.pd);
  462. datatcb.queue_init(sizeuinttype);
  463. datatcb.queue_emit_ordconst(entry^.offset,sizeuinttype);
  464. datatcb.maybe_end_aggregate(entrydef);
  465. dispose(entry);
  466. end;
  467. datadef:=datatcb.end_anonymous_record;
  468. tcb.finish_internal_data_builder(datatcb,tbllbl,datadef,sizeof(pint));
  469. tcb.emit_tai(tai_const.Create_sym(tbllbl),voidpointertype);
  470. end;
  471. list.free;
  472. end;
  473. procedure TRTTIWriter.write_rtti_name(tcb: ttai_typedconstbuilder; def: tdef);
  474. begin
  475. if is_open_array(def) then
  476. { open arrays never have a typesym with a name, since you cannot
  477. define an "open array type". Kylix prints the type of the
  478. elements in the array in this case (so together with the pfArray
  479. flag, you can reconstruct the full typename, I assume (JM))
  480. }
  481. def:=tarraydef(def).elementdef;
  482. { name }
  483. if assigned(def.typesym) then
  484. tcb.emit_shortstring_const(ttypesym(def.typesym).realname)
  485. else
  486. tcb.emit_shortstring_const('');
  487. end;
  488. procedure TRTTIWriter.write_common_rtti_data(tcb:ttai_typedconstbuilder;def:tdef;rt:trttitype);
  489. begin
  490. { important: we need to align this the same way as the type data itself
  491. is aligned }
  492. tcb.begin_anonymous_record(
  493. internaltypeprefixName[itp_rtti_common_data],
  494. defaultpacking,reqalign,
  495. targetinfos[target_info.system]^.alignment.recordalignmin,
  496. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  497. if rt<>fullrtti then
  498. begin
  499. write_attribute_data(tcb,nil);
  500. end
  501. else
  502. begin
  503. write_attribute_data(tcb,tstoreddef(def).rtti_attribute_list);
  504. end;
  505. tcb.end_anonymous_record;
  506. end;
  507. { writes a 32-bit count followed by array of field infos for given symtable }
  508. procedure TRTTIWriter.fields_write_rtti_data(tcb: ttai_typedconstbuilder; def: tabstractrecorddef; rt: trttitype);
  509. var
  510. i : longint;
  511. sym : tsym;
  512. fieldcnt: longint;
  513. st: tsymtable;
  514. fields: tfplist;
  515. parentrtti: boolean;
  516. begin
  517. fieldcnt:=0;
  518. parentrtti:=false;
  519. st:=def.symtable;
  520. fields:=tfplist.create;
  521. fields.capacity:=st.symlist.count+1;
  522. { For objects, treat parent (if any) as a field with offset 0. This
  523. provides correct handling of entire instance with RTL rtti routines. }
  524. if (def.typ=objectdef) and (tobjectdef(def).objecttype=odt_object) and
  525. Assigned(tobjectdef(def).childof) and
  526. ((rt=fullrtti) or (tobjectdef(def).childof.needs_inittable)) then
  527. begin
  528. parentrtti:=true;
  529. inc(fieldcnt);
  530. end;
  531. for i:=0 to st.SymList.Count-1 do
  532. begin
  533. sym:=tsym(st.SymList[i]);
  534. if (tsym(sym).typ=fieldvarsym) and
  535. not(sp_static in tsym(sym).symoptions) and
  536. (
  537. (rt=fullrtti) or
  538. tfieldvarsym(sym).vardef.needs_inittable
  539. ) and
  540. not is_objc_class_or_protocol(tfieldvarsym(sym).vardef) then
  541. begin
  542. fields.add(tfieldvarsym(sym));
  543. inc(fieldcnt);
  544. end;
  545. end;
  546. { insert field count before data }
  547. tcb.emit_ord_const(fieldcnt,u32inttype);
  548. { parent object? }
  549. if parentrtti then
  550. begin
  551. write_rtti_reference(tcb,tobjectdef(def).childof,rt);
  552. tcb.emit_ord_const(0,ptruinttype);
  553. end;
  554. { fields }
  555. for i:=0 to fields.count-1 do
  556. begin
  557. sym:=tsym(fields[i]);
  558. write_rtti_reference(tcb,tfieldvarsym(sym).vardef,rt);
  559. tcb.emit_ord_const(tfieldvarsym(sym).fieldoffset,ptruinttype);
  560. end;
  561. fields.free;
  562. end;
  563. procedure TRTTIWriter.fields_write_rtti(st:tsymtable;rt:trttitype);
  564. var
  565. i : longint;
  566. sym : tsym;
  567. begin
  568. for i:=0 to st.SymList.Count-1 do
  569. begin
  570. sym:=tsym(st.SymList[i]);
  571. if (tsym(sym).typ=fieldvarsym) and
  572. not(sp_static in tsym(sym).symoptions) and
  573. (
  574. (rt=fullrtti) or
  575. tfieldvarsym(sym).vardef.needs_inittable
  576. ) then
  577. write_rtti(tfieldvarsym(sym).vardef,rt);
  578. end;
  579. end;
  580. procedure TRTTIWriter.params_write_rtti(def:tabstractprocdef;rt:trttitype;allow_hidden:boolean);
  581. var
  582. i : longint;
  583. sym : tparavarsym;
  584. begin
  585. for i:=0 to def.paras.count-1 do
  586. begin
  587. sym:=tparavarsym(def.paras[i]);
  588. if not (vo_is_hidden_para in sym.varoptions) or allow_hidden then
  589. begin
  590. if is_open_array(sym.vardef) or is_array_of_const(sym.vardef) then
  591. write_rtti(tarraydef(sym.vardef).elementdef,rt)
  592. else
  593. write_rtti(sym.vardef,rt);
  594. end;
  595. end;
  596. end;
  597. procedure TRTTIWriter.methods_write_rtti(st:tsymtable;rt:trttitype;visibilities:tvisibilities;allow_hidden:boolean);
  598. var
  599. i,j : longint;
  600. sym : tprocsym;
  601. def : tabstractprocdef;
  602. begin
  603. for i:=0 to st.symlist.count-1 do
  604. if tsym(st.symlist[i]).typ=procsym then
  605. begin
  606. sym:=tprocsym(st.symlist[i]);
  607. for j:=0 to sym.procdeflist.count-1 do
  608. begin
  609. def:=tabstractprocdef(sym.procdeflist[j]);
  610. write_rtti(def.returndef,rt);
  611. params_write_rtti(def,rt,allow_hidden);
  612. end;
  613. end;
  614. end;
  615. procedure TRTTIWriter.published_write_rtti(st:tsymtable;rt:trttitype);
  616. var
  617. i : longint;
  618. sym : tsym;
  619. begin
  620. for i:=0 to st.SymList.Count-1 do
  621. begin
  622. sym:=tsym(st.SymList[i]);
  623. if (sym.visibility=vis_published) then
  624. begin
  625. case tsym(sym).typ of
  626. propertysym:
  627. write_rtti(tpropertysym(sym).propdef,rt);
  628. fieldvarsym:
  629. write_rtti(tfieldvarsym(sym).vardef,rt);
  630. else
  631. ;
  632. end;
  633. end;
  634. end;
  635. end;
  636. function TRTTIWriter.published_properties_count(st:tsymtable):longint;
  637. var
  638. i : longint;
  639. sym : tsym;
  640. begin
  641. result:=0;
  642. for i:=0 to st.SymList.Count-1 do
  643. begin
  644. sym:=tsym(st.SymList[i]);
  645. if (tsym(sym).typ=propertysym) and
  646. (sym.visibility=vis_published) then
  647. inc(result);
  648. end;
  649. end;
  650. procedure TRTTIWriter.collect_propnamelist(propnamelist:TFPHashObjectList;objdef:tobjectdef);
  651. var
  652. i : longint;
  653. sym : tsym;
  654. pn : tpropnamelistitem;
  655. begin
  656. if assigned(objdef.childof) then
  657. collect_propnamelist(propnamelist,objdef.childof);
  658. for i:=0 to objdef.symtable.SymList.Count-1 do
  659. begin
  660. sym:=tsym(objdef.symtable.SymList[i]);
  661. if (tsym(sym).typ=propertysym) and
  662. (sym.visibility=vis_published) then
  663. begin
  664. pn:=TPropNameListItem(propnamelist.Find(tsym(sym).name));
  665. if not assigned(pn) then
  666. begin
  667. pn:=tpropnamelistitem.create(propnamelist,tsym(sym).name);
  668. pn.propindex:=propnamelist.count-1;
  669. pn.propowner:=tsym(sym).owner;
  670. end;
  671. end;
  672. end;
  673. end;
  674. procedure TRTTIWriter.published_properties_write_rtti_data(tcb: ttai_typedconstbuilder; propnamelist:TFPHashObjectList;st:tsymtable);
  675. var
  676. i : longint;
  677. sym : tsym;
  678. proctypesinfo : byte;
  679. propnameitem : tpropnamelistitem;
  680. propdefname : string;
  681. procedure writeaccessproc(pap:tpropaccesslisttypes; shiftvalue : byte; unsetvalue: byte);
  682. var
  683. typvalue : byte;
  684. hp : ppropaccesslistitem;
  685. extnumber: longint;
  686. address,space : longint;
  687. def : tdef;
  688. hpropsym : tpropertysym;
  689. propaccesslist : tpropaccesslist;
  690. begin
  691. hpropsym:=tpropertysym(sym);
  692. repeat
  693. propaccesslist:=hpropsym.propaccesslist[pap];
  694. if not propaccesslist.empty then
  695. break;
  696. hpropsym:=hpropsym.overriddenpropsym;
  697. until not assigned(hpropsym);
  698. if not(assigned(propaccesslist) and assigned(propaccesslist.firstsym)) then
  699. begin
  700. tcb.emit_tai(Tai_const.Create_int_codeptr(unsetvalue),codeptruinttype);
  701. typvalue:=3;
  702. end
  703. else if propaccesslist.firstsym^.sym.typ=fieldvarsym then
  704. begin
  705. address:=0;
  706. hp:=propaccesslist.firstsym;
  707. def:=nil;
  708. while assigned(hp) do
  709. begin
  710. case hp^.sltype of
  711. sl_load :
  712. begin
  713. def:=tfieldvarsym(hp^.sym).vardef;
  714. inc(address,tfieldvarsym(hp^.sym).fieldoffset);
  715. end;
  716. sl_subscript :
  717. begin
  718. if not(assigned(def) and
  719. ((def.typ=recorddef) or
  720. is_object(def))) then
  721. internalerror(200402171);
  722. inc(address,tfieldvarsym(hp^.sym).fieldoffset);
  723. def:=tfieldvarsym(hp^.sym).vardef;
  724. end;
  725. sl_vec :
  726. begin
  727. if not(assigned(def) and (def.typ=arraydef)) then
  728. internalerror(200402172);
  729. def:=tarraydef(def).elementdef;
  730. {Hp.value is a Tconstexprint, which can be rather large,
  731. sanity check for longint overflow.}
  732. space:=(high(address)-address) div def.size;
  733. if int64(space)<hp^.value then
  734. internalerror(200706101);
  735. inc(address,int64(def.size*hp^.value));
  736. end;
  737. else
  738. internalerror(2019050523);
  739. end;
  740. hp:=hp^.next;
  741. end;
  742. tcb.emit_tai(Tai_const.Create_int_codeptr(address),codeptruinttype);
  743. typvalue:=0;
  744. end
  745. else
  746. begin
  747. { When there was an error then procdef is not assigned }
  748. if not assigned(propaccesslist.procdef) then
  749. exit;
  750. if not(po_virtualmethod in tprocdef(propaccesslist.procdef).procoptions) or
  751. is_objectpascal_helper(tprocdef(propaccesslist.procdef).struct) then
  752. begin
  753. tcb.queue_init(codeptruinttype);
  754. tcb.queue_emit_proc(tprocdef(propaccesslist.procdef));
  755. typvalue:=1;
  756. end
  757. else
  758. begin
  759. { virtual method, write vmt offset }
  760. extnumber:=tprocdef(propaccesslist.procdef).extnumber;
  761. tcb.emit_tai(Tai_const.Create_int_codeptr(
  762. tobjectdef(tprocdef(propaccesslist.procdef).struct).vmtmethodoffset(extnumber)),
  763. codeptruinttype);
  764. { register for wpo }
  765. tobjectdef(tprocdef(propaccesslist.procdef).struct).register_vmt_call(extnumber);
  766. {$ifdef vtentry}
  767. { not sure if we can insert those vtentry symbols safely here }
  768. {$error register methods used for published properties}
  769. {$endif vtentry}
  770. typvalue:=2;
  771. end;
  772. end;
  773. proctypesinfo:=proctypesinfo or (typvalue shl shiftvalue);
  774. end;
  775. begin
  776. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,SizeOf(PInt)),
  777. targetinfos[target_info.system]^.alignment.recordalignmin,
  778. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  779. tcb.emit_ord_const(published_properties_count(st),u16inttype);
  780. for i:=0 to st.SymList.Count-1 do
  781. begin
  782. sym:=tsym(st.SymList[i]);
  783. if (sym.typ=propertysym) and
  784. (sym.visibility=vis_published) then
  785. begin
  786. { we can only easily reuse defs if the property is not stored,
  787. because otherwise the rtti layout depends on how the "stored"
  788. is defined (field, indexed expression, virtual method, ...) }
  789. if not(ppo_stored in tpropertysym(sym).propoptions) then
  790. propdefname:=internaltypeprefixName[itp_rtti_prop]+tostr(length(tpropertysym(sym).realname))
  791. else
  792. propdefname:='';
  793. { TPropInfo is a packed record (even on targets that require
  794. alignment), but it starts aligned }
  795. tcb.begin_anonymous_record(
  796. propdefname,
  797. 1,min(reqalign,SizeOf(PInt)),
  798. targetinfos[target_info.system]^.alignment.recordalignmin,
  799. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  800. if ppo_indexed in tpropertysym(sym).propoptions then
  801. proctypesinfo:=$40
  802. else
  803. proctypesinfo:=0;
  804. write_rtti_reference(tcb,tpropertysym(sym).propdef,fullrtti);
  805. writeaccessproc(palt_read,0,0);
  806. writeaccessproc(palt_write,2,0);
  807. { is it stored ? }
  808. if not(ppo_stored in tpropertysym(sym).propoptions) then
  809. begin
  810. { no, so put a constant zero }
  811. tcb.emit_tai(Tai_const.Create_nil_codeptr,codeptruinttype);
  812. proctypesinfo:=proctypesinfo or (3 shl 4);
  813. end
  814. else
  815. writeaccessproc(palt_stored,4,1); { maybe; if no procedure put a constant 1 (=true) }
  816. tcb.emit_ord_const(tpropertysym(sym).index,u32inttype);
  817. tcb.emit_ord_const(tpropertysym(sym).default,u32inttype);
  818. propnameitem:=TPropNameListItem(propnamelist.Find(tpropertysym(sym).name));
  819. if not assigned(propnameitem) then
  820. internalerror(200512201);
  821. tcb.emit_ord_const(propnameitem.propindex,u16inttype);
  822. tcb.emit_ord_const(proctypesinfo,u8inttype);
  823. { write reference to attribute table }
  824. write_attribute_data(tcb,tpropertysym(sym).rtti_attribute_list);
  825. { Write property name }
  826. tcb.emit_shortstring_const(tpropertysym(sym).realname);
  827. tcb.end_anonymous_record;
  828. end;
  829. end;
  830. tcb.end_anonymous_record;
  831. end;
  832. procedure TRTTIWriter.write_rtti_data(tcb: ttai_typedconstbuilder; def: tdef; rt: trttitype);
  833. procedure unknown_rtti(def:tstoreddef);
  834. begin
  835. tcb.emit_ord_const(tkUnknown,u8inttype);
  836. write_rtti_name(tcb,def);
  837. write_common_rtti_data(tcb,def,rt);
  838. end;
  839. procedure variantdef_rtti(def:tvariantdef);
  840. begin
  841. write_header(tcb,def,tkVariant);
  842. write_common_rtti_data(tcb,def,rt);
  843. end;
  844. procedure stringdef_rtti(def:tstringdef);
  845. begin
  846. case def.stringtype of
  847. st_ansistring:
  848. begin
  849. write_header(tcb,def,tkAString);
  850. write_common_rtti_data(tcb,def,rt);
  851. { align }
  852. tcb.begin_anonymous_record(
  853. internaltypeprefixName[itp_rtti_ansistr],
  854. defaultpacking,reqalign,
  855. targetinfos[target_info.system]^.alignment.recordalignmin,
  856. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  857. tcb.emit_ord_const(def.encoding,u16inttype);
  858. tcb.end_anonymous_record;
  859. end;
  860. st_widestring:
  861. begin
  862. write_header(tcb,def,tkWString);
  863. write_common_rtti_data(tcb,def,rt);
  864. end;
  865. st_unicodestring:
  866. begin
  867. write_header(tcb,def,tkUString);
  868. write_common_rtti_data(tcb,def,rt);
  869. end;
  870. st_longstring:
  871. begin
  872. write_common_rtti_data(tcb,def,rt);
  873. write_header(tcb,def,tkLString);
  874. end;
  875. st_shortstring:
  876. begin
  877. write_header(tcb,def,tkSString);
  878. write_common_rtti_data(tcb,def,rt);
  879. tcb.emit_ord_const(def.len,u8inttype);
  880. end;
  881. end;
  882. end;
  883. procedure enumdef_rtti(def: tenumdef);
  884. var
  885. i : integer;
  886. hp : tenumsym;
  887. begin
  888. write_header(tcb,def,tkEnumeration);
  889. write_common_rtti_data(tcb,def,rt);
  890. { align; the named fields are so that we can let the compiler
  891. calculate the string offsets later on }
  892. tcb.next_field_name:='size_start_rec';
  893. { add a typename so that it can be reused when writing the the s2o
  894. and o2s arrays for llvm (otherwise we have to write out the entire
  895. type definition every time we access an element from this record) }
  896. tcb.begin_anonymous_record(internaltypeprefixName[itp_rtti_enum_size_start_rec]+def.unique_id_str,defaultpacking,reqalign,
  897. targetinfos[target_info.system]^.alignment.recordalignmin,
  898. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  899. case longint(def.size) of
  900. 1 :
  901. tcb.emit_ord_const(otUByte,u8inttype);
  902. 2 :
  903. tcb.emit_ord_const(otUWord,u8inttype);
  904. 4 :
  905. tcb.emit_ord_const(otULong,u8inttype);
  906. end;
  907. { we need to align by Tconstptruint here to satisfy the alignment
  908. rules set by records: in the typinfo unit we overlay a TTypeData
  909. record on this data, which at the innermost variant record needs an
  910. alignment of TConstPtrUint due to e.g. the "CompType" member for
  911. tkSet (also the "BaseType" member for tkEnumeration).
  912. We need to adhere to this, otherwise things will break. }
  913. tcb.next_field_name:='min_max_rec';
  914. tcb.begin_anonymous_record(internaltypeprefixName[itp_rtti_enum_min_max_rec]+def.unique_id_str,defaultpacking,reqalign,
  915. targetinfos[target_info.system]^.alignment.recordalignmin,
  916. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  917. tcb.emit_ord_const(def.min,s32inttype);
  918. tcb.emit_ord_const(def.max,s32inttype);
  919. tcb.next_field_name:='basetype_array_rec';
  920. { all strings must appear right after each other -> from now on
  921. packrecords 1 (but the start must still be aligned) }
  922. tcb.begin_anonymous_record(internaltypeprefixName[itp_rtti_enum_basetype_array_rec]+def.unique_id_str,1,reqalign,
  923. targetinfos[target_info.system]^.alignment.recordalignmin,
  924. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  925. { write base type }
  926. write_rtti_reference(tcb,def.basedef,rt);
  927. for i:=0 to def.symtable.SymList.Count-1 do
  928. begin
  929. hp:=tenumsym(def.symtable.SymList[i]);
  930. if hp.value<def.minval then
  931. continue
  932. else
  933. if hp.value>def.maxval then
  934. break;
  935. tcb.next_field_name:=hp.name;
  936. tcb.emit_shortstring_const(hp.realname);
  937. end;
  938. { write unit name }
  939. tcb.emit_shortstring_const(current_module.realmodulename^);
  940. { write zero which is required by RTL }
  941. tcb.emit_ord_const(0,u8inttype);
  942. { terminate all records }
  943. tcb.end_anonymous_record;
  944. tcb.end_anonymous_record;
  945. tcb.end_anonymous_record;
  946. end;
  947. procedure orddef_rtti(def:torddef);
  948. procedure doint32_64(typekind: byte;min,max:int64);
  949. const
  950. trans : array[tordtype] of byte =
  951. (otUByte{otNone},
  952. otUByte,otUWord,otULong,otUQWord,otUByte{otNone},
  953. otSByte,otSWord,otSLong,otSQWord,otUByte{otNone},
  954. otUByte,otUByte,otUWord,otULong,otUQWord,
  955. otSByte,otSWord,otSLong,otSQWord,
  956. otUByte,otUWord,otUByte,255);
  957. var
  958. elesize: string[1];
  959. deftrans: byte;
  960. begin
  961. write_header(tcb,def,typekind);
  962. write_common_rtti_data(tcb,def,rt);
  963. deftrans:=trans[def.ordtype];
  964. case deftrans of
  965. otUQWord,
  966. otSQWord:
  967. elesize:='8';
  968. 255:
  969. begin
  970. if def.packedbitsize<=32 then
  971. begin
  972. elesize:='4';
  973. if def.low<0 then
  974. deftrans:=otSLong
  975. else
  976. deftrans:=otULong;
  977. end
  978. else
  979. begin
  980. elesize:='8';
  981. if def.low<0 then
  982. deftrans:=otSQWord
  983. else
  984. deftrans:=otUQWord;
  985. end;
  986. end
  987. else
  988. elesize:='4'
  989. end;
  990. tcb.begin_anonymous_record(
  991. internaltypeprefixName[itp_rtti_ord_outer]+elesize,
  992. defaultpacking,reqalign,
  993. targetinfos[target_info.system]^.alignment.recordalignmin,
  994. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  995. tcb.emit_ord_const(byte(trans[def.ordtype]),u8inttype);
  996. tcb.begin_anonymous_record(
  997. internaltypeprefixName[itp_rtti_ord_inner]+elesize,
  998. defaultpacking,reqalign,
  999. targetinfos[target_info.system]^.alignment.recordalignmin,
  1000. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1001. {Convert to longint to smuggle values in high(longint)+1..high(cardinal) into asmlist.}
  1002. case deftrans of
  1003. otUQWord:
  1004. begin
  1005. tcb.emit_ord_const(min,u64inttype);
  1006. tcb.emit_ord_const(max,u64inttype);
  1007. end;
  1008. otSQWord:
  1009. begin
  1010. tcb.emit_ord_const(min,s64inttype);
  1011. tcb.emit_ord_const(max,s64inttype);
  1012. end;
  1013. else
  1014. begin
  1015. tcb.emit_ord_const(longint(min),s32inttype);
  1016. tcb.emit_ord_const(longint(max),s32inttype);
  1017. end;
  1018. end;
  1019. tcb.end_anonymous_record;
  1020. tcb.end_anonymous_record;
  1021. end;
  1022. procedure dointeger(typekind:byte);inline;
  1023. begin
  1024. doint32_64(typekind,int64(def.low.svalue),int64(def.high.svalue));
  1025. end;
  1026. begin
  1027. case def.ordtype of
  1028. s64bit :
  1029. dointeger(tkInt64);
  1030. u64bit :
  1031. dointeger(tkQWord);
  1032. pasbool1,
  1033. pasbool8,
  1034. pasbool16,
  1035. pasbool32,
  1036. pasbool64:
  1037. dointeger(tkBool);
  1038. { use different low/high values to be Delphi compatible }
  1039. bool8bit,
  1040. bool16bit,
  1041. bool32bit:
  1042. doint32_64(tkBool,longint(low(longint)),longint(high(longint)));
  1043. bool64bit:
  1044. doint32_64(tkBool,low(int64),high(int64));
  1045. uchar:
  1046. dointeger(tkChar);
  1047. uwidechar:
  1048. dointeger(tkWChar);
  1049. scurrency:
  1050. begin
  1051. write_header(tcb,def,tkFloat);
  1052. write_common_rtti_data(tcb,def,rt);
  1053. tcb.begin_anonymous_record(
  1054. internaltypeprefixName[itp_1byte],
  1055. defaultpacking,reqalign,
  1056. targetinfos[target_info.system]^.alignment.recordalignmin,
  1057. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1058. tcb.emit_ord_const(ftCurr,u8inttype);
  1059. tcb.end_anonymous_record;
  1060. end;
  1061. else
  1062. dointeger(tkInteger);
  1063. end;
  1064. end;
  1065. procedure floatdef_rtti(def:tfloatdef);
  1066. const
  1067. {tfloattype = (s32real,s64real,s80real,sc80real,s64bit,s128bit);}
  1068. translate : array[tfloattype] of byte =
  1069. (ftSingle,ftDouble,ftExtended,ftExtended,ftComp,ftCurr,ftFloat128);
  1070. begin
  1071. write_header(tcb,def,tkFloat);
  1072. write_common_rtti_data(tcb,def,rt);
  1073. tcb.begin_anonymous_record(
  1074. internaltypeprefixName[itp_1byte],
  1075. defaultpacking,reqalign,
  1076. targetinfos[target_info.system]^.alignment.recordalignmin,
  1077. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1078. tcb.emit_ord_const(translate[def.floattype],u8inttype);
  1079. tcb.end_anonymous_record;
  1080. end;
  1081. procedure setdef_rtti(def:tsetdef);
  1082. begin
  1083. write_header(tcb,def,tkSet);
  1084. write_common_rtti_data(tcb,def,rt);
  1085. tcb.begin_anonymous_record(
  1086. internaltypeprefixName[itp_rtti_set_outer],
  1087. defaultpacking,reqalign,
  1088. targetinfos[target_info.system]^.alignment.recordalignmin,
  1089. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1090. case def.size of
  1091. 1:
  1092. tcb.emit_ord_const(otUByte,u8inttype);
  1093. 2:
  1094. tcb.emit_ord_const(otUWord,u8inttype);
  1095. 4:
  1096. tcb.emit_ord_const(otULong,u8inttype);
  1097. else
  1098. tcb.emit_ord_const(otUByte,u8inttype);
  1099. end;
  1100. tcb.begin_anonymous_record(
  1101. internaltypeprefixName[itp_rtti_set_inner],
  1102. defaultpacking,reqalign,
  1103. targetinfos[target_info.system]^.alignment.recordalignmin,
  1104. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1105. tcb.emit_ord_const(def.size,sizesinttype);
  1106. write_rtti_reference(tcb,def.elementdef,rt);
  1107. tcb.end_anonymous_record;
  1108. tcb.end_anonymous_record;
  1109. end;
  1110. procedure arraydef_rtti(def:tarraydef);
  1111. var
  1112. i,dimcount: byte;
  1113. totalcount: asizeuint;
  1114. finaldef: tdef;
  1115. curdef:tarraydef;
  1116. begin
  1117. if ado_IsDynamicArray in def.arrayoptions then
  1118. tcb.emit_ord_const(tkDynArray,u8inttype)
  1119. else
  1120. tcb.emit_ord_const(tkArray,u8inttype);
  1121. write_rtti_name(tcb,def);
  1122. write_common_rtti_data(tcb,def,rt);
  1123. if not(ado_IsDynamicArray in def.arrayoptions) then
  1124. begin
  1125. { remember tha last instruction. we will need to insert some
  1126. calculated values after it }
  1127. finaldef:=def;
  1128. totalcount:=1;
  1129. dimcount:=0;
  1130. repeat
  1131. curdef:=tarraydef(finaldef);
  1132. finaldef:=curdef.elementdef;
  1133. { Dims[i] PTypeInfo }
  1134. inc(dimcount);
  1135. totalcount:=totalcount*curdef.elecount;
  1136. until (finaldef.typ<>arraydef) or
  1137. (ado_IsDynamicArray in tarraydef(finaldef).arrayoptions);
  1138. tcb.begin_anonymous_record(
  1139. internaltypeprefixName[itp_rtti_normal_array]+tostr(dimcount),
  1140. defaultpacking,reqalign,
  1141. targetinfos[target_info.system]^.alignment.recordalignmin,
  1142. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1143. { total size = elecount * elesize of the first arraydef }
  1144. tcb.emit_tai(Tai_const.Create_sizeint(def.elecount*def.elesize),sizeuinttype);
  1145. { total element count }
  1146. tcb.emit_tai(Tai_const.Create_sizeint(asizeint(totalcount)),sizeuinttype);
  1147. { last dimension element type }
  1148. tcb.emit_tai(Tai_const.Create_sym(get_rtti_label(curdef.elementdef,rt,true)),voidpointertype);
  1149. { dimension count }
  1150. tcb.emit_ord_const(dimcount,u8inttype);
  1151. finaldef:=def;
  1152. { ranges of the dimensions }
  1153. for i:=1 to dimcount do
  1154. begin
  1155. curdef:=tarraydef(finaldef);
  1156. finaldef:=curdef.elementdef;
  1157. { Dims[i] PPTypeInfo }
  1158. write_rtti_reference(tcb,curdef.rangedef,rt);
  1159. end;
  1160. end
  1161. else
  1162. { write a delphi almost compatible dyn. array entry:
  1163. there are two types, eltype and eltype2, the latter is nil if the element type needs
  1164. no finalization, the former is always valid, delphi has this swapped, but for
  1165. compatibility with older fpc versions we do it different, to be delphi compatible,
  1166. the names are swapped in typinfo.pp
  1167. }
  1168. begin
  1169. tcb.begin_anonymous_record(
  1170. internaltypeprefixName[itp_rtti_dyn_array],
  1171. defaultpacking,reqalign,
  1172. targetinfos[target_info.system]^.alignment.recordalignmin,
  1173. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1174. { size of elements }
  1175. tcb.emit_tai(Tai_const.Create_sizeint(def.elesize),sizeuinttype);
  1176. { element type }
  1177. write_rtti_reference(tcb,def.elementdef,rt);
  1178. { variant type }
  1179. tcb.emit_ord_const(tstoreddef(def.elementdef).getvardef,s32inttype);
  1180. { element type }
  1181. if def.elementdef.needs_inittable then
  1182. write_rtti_reference(tcb,def.elementdef,rt)
  1183. else
  1184. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1185. { write unit name }
  1186. tcb.emit_shortstring_const(current_module.realmodulename^);
  1187. end;
  1188. tcb.end_anonymous_record;
  1189. end;
  1190. procedure classrefdef_rtti(def:tclassrefdef);
  1191. begin
  1192. write_header(tcb,def,tkClassRef);
  1193. write_common_rtti_data(tcb,def,rt);
  1194. tcb.begin_anonymous_record(
  1195. internaltypeprefixName[itp_rtti_ref],
  1196. defaultpacking,reqalign,
  1197. targetinfos[target_info.system]^.alignment.recordalignmin,
  1198. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1199. write_rtti_reference(tcb,def.pointeddef,rt);
  1200. tcb.end_anonymous_record;
  1201. end;
  1202. procedure pointerdef_rtti(def:tpointerdef);
  1203. begin
  1204. write_header(tcb,def,tkPointer);
  1205. write_common_rtti_data(tcb,def,rt);
  1206. tcb.begin_anonymous_record(
  1207. internaltypeprefixName[itp_rtti_ref],
  1208. defaultpacking,reqalign,
  1209. targetinfos[target_info.system]^.alignment.recordalignmin,
  1210. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1211. write_rtti_reference(tcb,def.pointeddef,rt);
  1212. tcb.end_anonymous_record;
  1213. end;
  1214. procedure recorddef_rtti(def:trecorddef);
  1215. procedure write_record_operators;
  1216. var
  1217. rttilab: Tasmsymbol;
  1218. rttidef: tdef;
  1219. tcb: ttai_typedconstbuilder;
  1220. mop: tmanagementoperator;
  1221. procdef: tprocdef;
  1222. begin
  1223. rttilab := current_asmdata.DefineAsmSymbol(
  1224. internaltypeprefixName[itp_init_record_operators]+def.rtti_mangledname(rt),
  1225. AB_GLOBAL,AT_DATA,def);
  1226. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable]);
  1227. tcb.begin_anonymous_record(
  1228. rttilab.Name,
  1229. defaultpacking,reqalign,
  1230. targetinfos[target_info.system]^.alignment.recordalignmin,
  1231. targetinfos[target_info.system]^.alignment.maxCrecordalign
  1232. );
  1233. { use "succ" to omit first enum item "mop_none" }
  1234. for mop := succ(low(tmanagementoperator)) to high(tmanagementoperator) do
  1235. begin
  1236. if not (mop in trecordsymtable(def.symtable).managementoperators) then
  1237. tcb.emit_tai(Tai_const.Create_nil_codeptr,voidcodepointertype)
  1238. else
  1239. begin
  1240. procdef := search_management_operator(mop, def);
  1241. if procdef = nil then
  1242. internalerror(201603021)
  1243. else
  1244. tcb.emit_tai(Tai_const.Createname(procdef.mangledname,AT_FUNCTION,0),
  1245. cprocvardef.getreusableprocaddr(procdef));
  1246. end;
  1247. end;
  1248. rttidef := tcb.end_anonymous_record;
  1249. current_asmdata.AsmLists[al_rtti].concatList(
  1250. tcb.get_final_asmlist(rttilab,rttidef,sec_rodata,rttilab.name,
  1251. sizeof(PInt)));
  1252. tcb.free;
  1253. end;
  1254. begin
  1255. write_header(tcb,def,tkRecord);
  1256. write_common_rtti_data(tcb,def,rt);
  1257. { need extra reqalign record, because otherwise the u32 int will
  1258. only be aligned to 4 even on 64 bit target (while the rtti code
  1259. in typinfo expects alignments to sizeof(pointer)) }
  1260. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1261. targetinfos[target_info.system]^.alignment.recordalignmin,
  1262. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1263. { store special terminator for init table for more optimal rtl operations
  1264. strictly related to RecordRTTI procedure in rtti.inc (directly
  1265. related to RTTIRecordRttiInfoToInitInfo function) }
  1266. if (rt=initrtti) then
  1267. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype)
  1268. else
  1269. { we use a direct reference as the init RTTI is always in the same
  1270. unit as the full RTTI }
  1271. tcb.emit_tai(Tai_const.Create_sym(get_rtti_label(def,initrtti,false)),voidpointertype);
  1272. tcb.emit_ord_const(def.size,u32inttype);
  1273. { store rtti management operators only for init table }
  1274. if (rt=initrtti) then
  1275. begin
  1276. { for now records don't have the initializer table }
  1277. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1278. if (trecordsymtable(def.symtable).managementoperators=[]) then
  1279. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype)
  1280. else
  1281. tcb.emit_tai(Tai_const.Createname(
  1282. internaltypeprefixName[itp_init_record_operators]+def.rtti_mangledname(rt),
  1283. AT_DATA_FORCEINDIRECT,0),voidpointertype);
  1284. end;
  1285. fields_write_rtti_data(tcb,def,rt);
  1286. tcb.end_anonymous_record;
  1287. { write pointers to operators if needed }
  1288. if (rt=initrtti) and (trecordsymtable(def.symtable).managementoperators<>[]) then
  1289. write_record_operators;
  1290. end;
  1291. procedure procvardef_rtti(def:tprocvardef);
  1292. procedure write_para(parasym:tparavarsym);
  1293. begin
  1294. { write flags for current parameter }
  1295. write_param_flag(tcb,parasym);
  1296. { write name of current parameter }
  1297. tcb.emit_shortstring_const(parasym.realname);
  1298. { write name of type of current parameter }
  1299. write_rtti_name(tcb,parasym.vardef);
  1300. end;
  1301. procedure write_procedure_param(parasym:tparavarsym);
  1302. begin
  1303. { every parameter is expected to start aligned }
  1304. tcb.begin_anonymous_record(
  1305. internaltypeprefixName[itp_rtti_proc_param]+tostr(length(parasym.realname)),
  1306. defaultpacking,min(reqalign,SizeOf(PInt)),
  1307. targetinfos[target_info.system]^.alignment.recordalignmin,
  1308. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1309. { write flags for current parameter }
  1310. write_param_flag(tcb,parasym);
  1311. { write param type }
  1312. if is_open_array(parasym.vardef) or is_array_of_const(parasym.vardef) then
  1313. write_rtti_reference(tcb,tarraydef(parasym.vardef).elementdef,fullrtti)
  1314. else if parasym.vardef=cformaltype then
  1315. write_rtti_reference(tcb,nil,fullrtti)
  1316. else
  1317. write_rtti_reference(tcb,parasym.vardef,fullrtti);
  1318. { write name of current parameter }
  1319. tcb.emit_shortstring_const(parasym.realname);
  1320. tcb.end_anonymous_record;
  1321. end;
  1322. var
  1323. methodkind : byte;
  1324. i : integer;
  1325. begin
  1326. if po_methodpointer in def.procoptions then
  1327. begin
  1328. { write method id and name }
  1329. write_header(tcb,def,tkMethod);
  1330. write_common_rtti_data(tcb,def,rt);
  1331. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1332. targetinfos[target_info.system]^.alignment.recordalignmin,
  1333. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1334. { write kind of method }
  1335. methodkind:=write_methodkind(tcb,def);
  1336. { write parameter info. The parameters must be written in reverse order
  1337. if this method uses right to left parameter pushing! }
  1338. tcb.emit_ord_const(def.paras.count,u8inttype);
  1339. for i:=0 to def.paras.count-1 do
  1340. write_para(tparavarsym(def.paras[i]));
  1341. if (methodkind=mkFunction) or (methodkind=mkClassFunction) then
  1342. begin
  1343. { write name of result type }
  1344. write_rtti_name(tcb,def.returndef);
  1345. { enclosing record takes care of alignment }
  1346. { write result typeinfo }
  1347. write_rtti_reference(tcb,def.returndef,fullrtti);
  1348. end;
  1349. { write calling convention }
  1350. write_callconv(tcb,def);
  1351. { enclosing record takes care of alignment }
  1352. { write params typeinfo }
  1353. for i:=0 to def.paras.count-1 do
  1354. begin
  1355. if is_open_array(tparavarsym(def.paras[i]).vardef) or is_array_of_const(tparavarsym(def.paras[i]).vardef) then
  1356. write_rtti_reference(tcb,tarraydef(tparavarsym(def.paras[i]).vardef).elementdef,fullrtti)
  1357. else if tparavarsym(def.paras[i]).vardef=cformaltype then
  1358. write_rtti_reference(tcb,nil,fullrtti)
  1359. else
  1360. write_rtti_reference(tcb,tparavarsym(def.paras[i]).vardef,fullrtti);
  1361. end;
  1362. tcb.end_anonymous_record;
  1363. end
  1364. else
  1365. begin
  1366. write_header(tcb,def,tkProcvar);
  1367. write_common_rtti_data(tcb,def,rt);
  1368. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1369. targetinfos[target_info.system]^.alignment.recordalignmin,
  1370. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1371. { flags }
  1372. tcb.emit_ord_const(0,u8inttype);
  1373. { write calling convention }
  1374. write_callconv(tcb,def);
  1375. { enclosing record takes care of alignment }
  1376. { write result typeinfo }
  1377. write_rtti_reference(tcb,def.returndef,fullrtti);
  1378. { write parameter count }
  1379. tcb.emit_ord_const(def.paras.count,u8inttype);
  1380. for i:=0 to def.paras.count-1 do
  1381. write_procedure_param(tparavarsym(def.paras[i]));
  1382. tcb.end_anonymous_record;
  1383. end;
  1384. end;
  1385. procedure objectdef_rtti(def: tobjectdef);
  1386. procedure objectdef_rtti_fields(def:tobjectdef);
  1387. begin
  1388. { - for compatiblity with record RTTI we need to write a terminator-
  1389. Nil pointer for initrtti as well for objects
  1390. - for RTTI consistency for objects we need point from fullrtti
  1391. to initrtti
  1392. - classes are assumed to have the same INIT RTTI as records
  1393. (see TObject.CleanupInstance)
  1394. - neither helper nor class type have fullrtti for fields
  1395. }
  1396. if (rt=initrtti) then
  1397. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype)
  1398. else
  1399. if (def.objecttype=odt_object) then
  1400. tcb.emit_tai(Tai_const.Create_sym(get_rtti_label(def,initrtti,false)),voidpointertype)
  1401. else
  1402. internalerror(2017011801);
  1403. tcb.emit_ord_const(def.size, u32inttype);
  1404. { pointer to management operators available only for initrtti }
  1405. if (rt=initrtti) then
  1406. begin
  1407. { initializer table only available for classes currently }
  1408. if def.objecttype=odt_class then
  1409. write_mop_offset_table(tcb,def,mop_initialize)
  1410. else
  1411. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1412. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1413. end;
  1414. { enclosing record takes care of alignment }
  1415. fields_write_rtti_data(tcb,def,rt);
  1416. end;
  1417. procedure objectdef_rtti_interface_init(def:tobjectdef);
  1418. begin
  1419. tcb.emit_ord_const(def.size, u32inttype);
  1420. end;
  1421. procedure objectdef_rtti_class_full(def:tobjectdef);
  1422. var
  1423. propnamelist : TFPHashObjectList;
  1424. begin
  1425. { Collect unique property names with nameindex }
  1426. propnamelist:=TFPHashObjectList.Create;
  1427. collect_propnamelist(propnamelist,def);
  1428. if not is_objectpascal_helper(def) then
  1429. if (oo_has_vmt in def.objectoptions) then
  1430. tcb.emit_tai(
  1431. Tai_const.Createname(def.vmt_mangledname,AT_DATA_FORCEINDIRECT,0),
  1432. cpointerdef.getreusable(def.vmt_def))
  1433. else
  1434. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1435. { write parent typeinfo }
  1436. write_rtti_reference(tcb,def.childof,fullrtti);
  1437. { write typeinfo of extended type }
  1438. if is_objectpascal_helper(def) then
  1439. if assigned(def.extendeddef) then
  1440. write_rtti_reference(tcb,def.extendeddef,fullrtti)
  1441. else
  1442. InternalError(2011033001);
  1443. { total number of unique properties }
  1444. tcb.emit_ord_const(propnamelist.count,u16inttype);
  1445. { write unit name }
  1446. tcb.emit_shortstring_const(current_module.realmodulename^);
  1447. { write published properties for this object }
  1448. published_properties_write_rtti_data(tcb,propnamelist,def.symtable);
  1449. propnamelist.free;
  1450. end;
  1451. procedure objectdef_rtti_interface_full(def:tobjectdef);
  1452. var
  1453. propnamelist : TFPHashObjectList;
  1454. { if changed to a set, make sure it's still a byte large, and
  1455. swap appropriately when cross-compiling
  1456. }
  1457. IntfFlags: byte;
  1458. begin
  1459. { Collect unique property names with nameindex }
  1460. propnamelist:=TFPHashObjectList.Create;
  1461. collect_propnamelist(propnamelist,def);
  1462. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1463. targetinfos[target_info.system]^.alignment.recordalignmin,
  1464. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1465. { write parent typeinfo }
  1466. write_rtti_reference(tcb,def.childof,fullrtti);
  1467. { interface: write flags, iid and iidstr }
  1468. IntfFlags:=0;
  1469. if assigned(def.iidguid) then
  1470. IntfFlags:=IntfFlags or (1 shl ord(ifHasGuid));
  1471. if (def.objecttype=odt_interfacecorba) and (def.iidstr^<>'') then
  1472. IntfFlags:=IntfFlags or (1 shl ord(ifHasStrGUID));
  1473. if (def.objecttype=odt_dispinterface) then
  1474. IntfFlags:=IntfFlags or (1 shl ord(ifDispInterface));
  1475. if (target_info.endian=endian_big) then
  1476. IntfFlags:=reverse_byte(IntfFlags);
  1477. {
  1478. ifDispatch, }
  1479. tcb.emit_ord_const(IntfFlags,u8inttype);
  1480. { write GUID }
  1481. tcb.emit_guid_const(def.iidguid^);
  1482. { write unit name }
  1483. tcb.emit_shortstring_const(current_module.realmodulename^);
  1484. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1485. targetinfos[target_info.system]^.alignment.recordalignmin,
  1486. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1487. { write iidstr }
  1488. if def.objecttype=odt_interfacecorba then
  1489. begin
  1490. { prepareguid always allocates an empty string }
  1491. if not assigned(def.iidstr) then
  1492. internalerror(2016021901);
  1493. tcb.emit_shortstring_const(def.iidstr^)
  1494. end;
  1495. { write published properties for this object }
  1496. published_properties_write_rtti_data(tcb,propnamelist,def.symtable);
  1497. { write published methods for this interface }
  1498. write_methods(tcb,def.symtable,[vis_published]);
  1499. tcb.end_anonymous_record;
  1500. tcb.end_anonymous_record;
  1501. propnamelist.free;
  1502. end;
  1503. begin
  1504. case def.objecttype of
  1505. odt_class:
  1506. tcb.emit_ord_const(tkclass,u8inttype);
  1507. odt_object:
  1508. tcb.emit_ord_const(tkobject,u8inttype);
  1509. odt_dispinterface,
  1510. odt_interfacecom:
  1511. tcb.emit_ord_const(tkInterface,u8inttype);
  1512. odt_interfacecorba:
  1513. tcb.emit_ord_const(tkinterfaceCorba,u8inttype);
  1514. odt_helper:
  1515. tcb.emit_ord_const(tkhelper,u8inttype);
  1516. else
  1517. internalerror(200611034);
  1518. end;
  1519. { generate the name }
  1520. tcb.emit_shortstring_const(def.objrealname^);
  1521. write_common_rtti_data(tcb,def,rt);
  1522. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1523. targetinfos[target_info.system]^.alignment.recordalignmin,
  1524. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1525. case rt of
  1526. initrtti :
  1527. begin
  1528. if def.objecttype in [odt_class,odt_object,odt_helper] then
  1529. objectdef_rtti_fields(def)
  1530. else
  1531. objectdef_rtti_interface_init(def);
  1532. end;
  1533. fullrtti :
  1534. begin
  1535. case def.objecttype of
  1536. odt_helper,
  1537. odt_class:
  1538. objectdef_rtti_class_full(def);
  1539. odt_object:
  1540. objectdef_rtti_fields(def);
  1541. else
  1542. objectdef_rtti_interface_full(def);
  1543. end;
  1544. end;
  1545. else
  1546. ;
  1547. end;
  1548. tcb.end_anonymous_record;
  1549. end;
  1550. begin
  1551. case def.typ of
  1552. variantdef :
  1553. variantdef_rtti(tvariantdef(def));
  1554. stringdef :
  1555. stringdef_rtti(tstringdef(def));
  1556. enumdef :
  1557. enumdef_rtti(tenumdef(def));
  1558. orddef :
  1559. orddef_rtti(torddef(def));
  1560. floatdef :
  1561. floatdef_rtti(tfloatdef(def));
  1562. setdef :
  1563. setdef_rtti(tsetdef(def));
  1564. procvardef :
  1565. procvardef_rtti(tprocvardef(def));
  1566. arraydef :
  1567. begin
  1568. if ado_IsBitPacked in tarraydef(def).arrayoptions then
  1569. unknown_rtti(tstoreddef(def))
  1570. else
  1571. arraydef_rtti(tarraydef(def));
  1572. end;
  1573. recorddef :
  1574. begin
  1575. if trecorddef(def).is_packed then
  1576. unknown_rtti(tstoreddef(def))
  1577. else
  1578. recorddef_rtti(trecorddef(def));
  1579. end;
  1580. objectdef :
  1581. objectdef_rtti(tobjectdef(def));
  1582. classrefdef :
  1583. classrefdef_rtti(tclassrefdef(def));
  1584. pointerdef :
  1585. pointerdef_rtti(tpointerdef(def));
  1586. else
  1587. unknown_rtti(tstoreddef(def));
  1588. end;
  1589. end;
  1590. procedure TRTTIWriter.write_attribute_data(tcb:ttai_typedconstbuilder;attr_list:trtti_attribute_list);
  1591. procedure write_args(tbltcb:ttai_typedconstbuilder;attr:trtti_attribute);
  1592. var
  1593. argtcb : ttai_typedconstbuilder;
  1594. arglab : tasmlabel;
  1595. argdef : tdef;
  1596. i : sizeint;
  1597. arglen : word;
  1598. begin
  1599. if length(attr.paras)=0 then
  1600. begin
  1601. tbltcb.emit_tai(tai_const.Create_16bit(0),u16inttype);
  1602. tbltcb.emit_tai(tai_const.Create_nil_dataptr,voidpointertype);
  1603. end
  1604. else
  1605. begin
  1606. current_asmdata.getglobaldatalabel(arglab);
  1607. argtcb:=ctai_typedconstbuilder.create([tcalo_is_lab,tcalo_make_dead_strippable,tcalo_apply_constalign]);
  1608. argtcb.begin_anonymous_record('',defaultpacking,min(reqalign,SizeOf(PInt)),
  1609. targetinfos[target_info.system]^.alignment.recordalignmin,
  1610. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1611. arglen:=0;
  1612. for i:=0 to High(attr.paras) do
  1613. begin
  1614. case attr.paras[i].nodetype of
  1615. niln,
  1616. ordconstn,
  1617. realconstn,
  1618. stringconstn,
  1619. pointerconstn,
  1620. guidconstn:
  1621. inc(arglen,tconstnode(attr.paras[i]).emit_data(argtcb));
  1622. setconstn:
  1623. inc(arglen,tsetconstnode(attr.paras[i]).emit_data(argtcb));
  1624. else
  1625. internalerror(2019070803);
  1626. end;
  1627. end;
  1628. argdef:=argtcb.end_anonymous_record;
  1629. current_asmdata.asmlists[al_rtti].concatlist(
  1630. argtcb.get_final_asmlist(arglab,argdef,sec_rodata,arglab.name,const_align(sizeof(pint)))
  1631. );
  1632. argtcb.free;
  1633. { write argument size and the reference to the argument entry }
  1634. tbltcb.emit_ord_const(arglen,u16inttype);
  1635. tbltcb.emit_tai(Tai_const.Create_sym(arglab),voidpointertype);
  1636. end;
  1637. end;
  1638. var
  1639. count,i,len: word;
  1640. attr : trtti_attribute;
  1641. tbltcb : ttai_typedconstbuilder;
  1642. tbllab : tasmlabel;
  1643. tbldef : tdef;
  1644. begin
  1645. if assigned(attr_list) then
  1646. count:=attr_list.get_attribute_count
  1647. else
  1648. count:=0;
  1649. if count=0 then
  1650. begin
  1651. { write a Nil reference }
  1652. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1653. exit;
  1654. end;
  1655. { first write the attribute list as a separate table }
  1656. current_asmdata.getglobaldatalabel(tbllab);
  1657. tbltcb:=ctai_typedconstbuilder.create([tcalo_is_lab,tcalo_make_dead_strippable,tcalo_apply_constalign]);
  1658. tbltcb.begin_anonymous_record(
  1659. internaltypeprefixName[itp_rtti_attr_list]+tostr(count),
  1660. defaultpacking,min(reqalign,SizeOf(PInt)),
  1661. targetinfos[target_info.system]^.alignment.recordalignmin,
  1662. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1663. tbltcb.emit_ord_const(count,u16inttype);
  1664. for i:=0 to count-1 do
  1665. begin
  1666. tbltcb.begin_anonymous_record(internaltypeprefixName[itp_rtti_attr_entry],defaultpacking,min(reqalign,SizeOf(PInt)),
  1667. targetinfos[target_info.system]^.alignment.recordalignmin,
  1668. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1669. attr:=trtti_attribute(attr_list.rtti_attributes[i]);
  1670. write_rtti_reference(tbltcb,ttypesym(attr.typesym).typedef,fullrtti);
  1671. tbltcb.emit_procdef_const(tprocdef(tcallnode(attr.constructorcall).procdefinition));
  1672. tbltcb.emit_tai(tai_const.Createname(attr.symbolname,AT_DATA_FORCEINDIRECT,0),cpointerdef.getreusable(ttypesym(attr.typesym).typedef));
  1673. write_args(tbltcb,attr);
  1674. tbltcb.end_anonymous_record;
  1675. end;
  1676. tbldef:=tbltcb.end_anonymous_record;
  1677. current_asmdata.asmlists[al_rtti].concatlist(
  1678. tbltcb.get_final_asmlist(tbllab,tbldef,sec_rodata,tbllab.name,const_align(sizeof(pint)))
  1679. );
  1680. tbltcb.free;
  1681. { write the reference to the attribute table }
  1682. tcb.emit_tai(Tai_const.Create_sym(tbllab),voidpointertype);
  1683. end;
  1684. function enumsym_compare_name(item1, item2: pointer): Integer;
  1685. var
  1686. enum1: tenumsym absolute item1;
  1687. enum2: tenumsym absolute item2;
  1688. begin
  1689. if enum1=enum2 then
  1690. result:=0
  1691. else if enum1.name>enum2.name then
  1692. result:=1
  1693. else
  1694. { there can't be equal names, identifiers are unique }
  1695. result:=-1;
  1696. end;
  1697. function enumsym_compare_value(item1, item2: pointer): Integer;
  1698. var
  1699. enum1: tenumsym absolute item1;
  1700. enum2: tenumsym absolute item2;
  1701. begin
  1702. if enum1.value>enum2.value then
  1703. result:=1
  1704. else if enum1.value<enum2.value then
  1705. result:=-1
  1706. else
  1707. result:=0;
  1708. end;
  1709. procedure TRTTIWriter.write_rtti_extrasyms(def:Tdef;rt:Trttitype;mainrtti:Tasmsymbol);
  1710. type Penumsym = ^Tenumsym;
  1711. { Writes a helper table for accelerated conversion of ordinal enum values to strings.
  1712. If you change something in this method, make sure to adapt the corresponding code
  1713. in sstrings.inc. }
  1714. procedure enumdef_rtti_ord2stringindex(rttidef: trecorddef; const syms: tfplist);
  1715. var rttilab:Tasmsymbol;
  1716. h,i,o,prev_value:longint;
  1717. mode:(lookup,search); {Modify with care, ordinal value of enum is written.}
  1718. r:single; {Must be real type because of integer overflow risk.}
  1719. tcb: ttai_typedconstbuilder;
  1720. sym_count: integer;
  1721. tabledef: tdef;
  1722. begin
  1723. {Decide wether a lookup array is size efficient.}
  1724. mode:=lookup;
  1725. sym_count:=syms.count;
  1726. if sym_count>0 then
  1727. begin
  1728. i:=1;
  1729. r:=0;
  1730. h:=tenumsym(syms[0]).value; {Next expected enum value is min.}
  1731. { set prev_value for the first iteration to a value that is
  1732. different from the first one without risking overflow (it's used
  1733. to detect whether two enum values are the same) }
  1734. if h=0 then
  1735. prev_value:=1
  1736. else
  1737. prev_value:=0;
  1738. while i<sym_count do
  1739. begin
  1740. { if two enum values are the same, we have to create a table }
  1741. if (prev_value=h) then
  1742. begin
  1743. mode:=search;
  1744. break;
  1745. end;
  1746. {Calculate size of hole between values. Avoid integer overflows.}
  1747. r:=r+(single(tenumsym(syms[i]).value)-single(h))-1;
  1748. prev_value:=h;
  1749. h:=tenumsym(syms[i]).value;
  1750. inc(i);
  1751. end;
  1752. if r>sym_count then
  1753. mode:=search; {Don't waste more than 50% space.}
  1754. end;
  1755. { write rtti data; make sure that the alignment matches the corresponding data structure
  1756. in the code that uses it (if alignment is required). }
  1757. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_data_force_indirect]);
  1758. { use TConstPtrUInt packrecords to ensure good alignment }
  1759. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1760. targetinfos[target_info.system]^.alignment.recordalignmin,
  1761. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1762. { now emit the data: first the mode }
  1763. tcb.emit_tai(Tai_const.create_32bit(longint(mode)),u32inttype);
  1764. { align }
  1765. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,sizeof(PInt)),
  1766. targetinfos[target_info.system]^.alignment.recordalignmin,
  1767. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1768. if mode=lookup then
  1769. begin
  1770. o:=tenumsym(syms[0]).value; {Start with min value.}
  1771. for i:=0 to sym_count-1 do
  1772. begin
  1773. while o<tenumsym(syms[i]).value do
  1774. begin
  1775. tcb.emit_tai(Tai_const.create_nil_dataptr,ptruinttype);
  1776. inc(o);
  1777. end;
  1778. inc(o);
  1779. tcb.queue_init(voidpointertype);
  1780. tcb.queue_subscriptn_multiple_by_name(rttidef,
  1781. ['size_start_rec',
  1782. 'min_max_rec',
  1783. 'basetype_array_rec',
  1784. tsym(syms[i]).Name]
  1785. );
  1786. tcb.queue_emit_asmsym(mainrtti,rttidef);
  1787. end;
  1788. end
  1789. else
  1790. begin
  1791. tcb.emit_ord_const(sym_count,u32inttype);
  1792. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,sizeof(PInt)),
  1793. targetinfos[target_info.system]^.alignment.recordalignmin,
  1794. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1795. for i:=0 to sym_count-1 do
  1796. begin
  1797. tcb.emit_ord_const(tenumsym(syms[i]).value,s32inttype);
  1798. tcb.queue_init(voidpointertype);
  1799. tcb.queue_subscriptn_multiple_by_name(rttidef,
  1800. ['size_start_rec',
  1801. 'min_max_rec',
  1802. 'basetype_array_rec',
  1803. tsym(syms[i]).Name]
  1804. );
  1805. tcb.queue_emit_asmsym(mainrtti,rttidef);
  1806. end;
  1807. tcb.end_anonymous_record;
  1808. end;
  1809. tcb.end_anonymous_record;
  1810. tabledef:=tcb.end_anonymous_record;
  1811. rttilab:=current_asmdata.DefineAsmSymbol(Tstoreddef(def).rtti_mangledname(rt)+'_o2s',AB_GLOBAL,AT_DATA_NOINDIRECT,tabledef);
  1812. current_asmdata.asmlists[al_rtti].concatlist(tcb.get_final_asmlist(
  1813. rttilab,tabledef,sec_rodata,
  1814. rttilab.name,sizeof(PInt)));
  1815. tcb.free;
  1816. current_module.add_public_asmsym(rttilab);
  1817. end;
  1818. { Writes a helper table for accelerated conversion of string to ordinal enum values.
  1819. If you change something in this method, make sure to adapt the corresponding code
  1820. in sstrings.inc. }
  1821. procedure enumdef_rtti_string2ordindex(rttidef: trecorddef; const syms: tfplist);
  1822. var
  1823. tcb: ttai_typedconstbuilder;
  1824. rttilab: Tasmsymbol;
  1825. i:longint;
  1826. tabledef: tdef;
  1827. begin
  1828. { write rtti data }
  1829. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_data_force_indirect]);
  1830. { begin of Tstring_to_ord }
  1831. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,sizeof(PInt)),
  1832. targetinfos[target_info.system]^.alignment.recordalignmin,
  1833. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1834. tcb.emit_ord_const(syms.count,s32inttype);
  1835. { begin of "data" array in Tstring_to_ord }
  1836. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,sizeof(PInt)),
  1837. targetinfos[target_info.system]^.alignment.recordalignmin,
  1838. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1839. for i:=0 to syms.count-1 do
  1840. begin
  1841. tcb.emit_ord_const(tenumsym(syms[i]).value,s32inttype);
  1842. { alignment of pointer value handled by enclosing record already }
  1843. tcb.queue_init(voidpointertype);
  1844. tcb.queue_subscriptn_multiple_by_name(rttidef,
  1845. ['size_start_rec',
  1846. 'min_max_rec',
  1847. 'basetype_array_rec',
  1848. tsym(syms[i]).Name]
  1849. );
  1850. tcb.queue_emit_asmsym(mainrtti,rttidef);
  1851. end;
  1852. tcb.end_anonymous_record;
  1853. tabledef:=tcb.end_anonymous_record;
  1854. rttilab:=current_asmdata.DefineAsmSymbol(Tstoreddef(def).rtti_mangledname(rt)+'_s2o',AB_GLOBAL,AT_DATA_NOINDIRECT,tabledef);
  1855. current_asmdata.asmlists[al_rtti].concatlist(tcb.get_final_asmlist(
  1856. rttilab,tabledef,sec_rodata,
  1857. rttilab.name,sizeof(PInt)));
  1858. tcb.free;
  1859. current_module.add_public_asmsym(rttilab);
  1860. end;
  1861. procedure enumdef_rtti_extrasyms(def:Tenumdef);
  1862. var
  1863. t:Tenumsym;
  1864. syms:tfplist;
  1865. i:longint;
  1866. rttitypesym: ttypesym;
  1867. rttidef: trecorddef;
  1868. begin
  1869. { collect enumsyms belonging to this enum type (could be a subsection
  1870. in case of a subrange type) }
  1871. syms:=tfplist.create;
  1872. for i := 0 to def.symtable.SymList.Count - 1 do
  1873. begin
  1874. t:=tenumsym(def.symtable.SymList[i]);
  1875. if t.value<def.minval then
  1876. continue
  1877. else
  1878. if t.value>def.maxval then
  1879. break;
  1880. syms.add(t);
  1881. end;
  1882. { sort the syms by enum name }
  1883. syms.sort(@enumsym_compare_name);
  1884. rttitypesym:=try_search_current_module_type(internaltypeprefixName[itp_rttidef]+def.rtti_mangledname(fullrtti));
  1885. if not assigned(rttitypesym) or
  1886. (ttypesym(rttitypesym).typedef.typ<>recorddef) then
  1887. internalerror(2015071402);
  1888. rttidef:=trecorddef(ttypesym(rttitypesym).typedef);
  1889. enumdef_rtti_string2ordindex(rttidef,syms);
  1890. { sort the syms by enum value }
  1891. syms.sort(@enumsym_compare_value);
  1892. enumdef_rtti_ord2stringindex(rttidef,syms);
  1893. syms.free;
  1894. end;
  1895. begin
  1896. case def.typ of
  1897. enumdef:
  1898. if rt=fullrtti then
  1899. begin
  1900. enumdef_rtti_extrasyms(Tenumdef(def));
  1901. end;
  1902. else
  1903. ;
  1904. end;
  1905. end;
  1906. procedure TRTTIWriter.write_child_rtti_data(def:tdef;rt:trttitype);
  1907. begin
  1908. case def.typ of
  1909. enumdef :
  1910. if assigned(tenumdef(def).basedef) then
  1911. write_rtti(tenumdef(def).basedef,rt);
  1912. setdef :
  1913. write_rtti(tsetdef(def).elementdef,rt);
  1914. arraydef :
  1915. begin
  1916. write_rtti(tarraydef(def).rangedef,rt);
  1917. write_rtti(tarraydef(def).elementdef,rt);
  1918. end;
  1919. recorddef :
  1920. begin
  1921. { guarantee initrtti for any record for RTTI purposes
  1922. also for fpc_initialize, fpc_finalize }
  1923. if (rt=fullrtti) then
  1924. begin
  1925. include(def.defstates,ds_init_table_used);
  1926. write_rtti(def, initrtti);
  1927. end;
  1928. fields_write_rtti(trecorddef(def).symtable,rt);
  1929. end;
  1930. objectdef :
  1931. begin
  1932. if assigned(tobjectdef(def).childof) then
  1933. write_rtti(tobjectdef(def).childof,rt);
  1934. if (rt=initrtti) or (tobjectdef(def).objecttype=odt_object) then
  1935. fields_write_rtti(tobjectdef(def).symtable,rt)
  1936. else
  1937. published_write_rtti(tobjectdef(def).symtable,rt);
  1938. if (rt=fullrtti) then
  1939. begin
  1940. { guarantee initrtti for any object for RTTI purposes
  1941. also for fpc_initialize, fpc_finalize }
  1942. if (tobjectdef(def).objecttype=odt_object) then
  1943. begin
  1944. include(def.defstates,ds_init_table_used);
  1945. write_rtti(def,initrtti);
  1946. end;
  1947. if (is_interface(def) or is_dispinterface(def))
  1948. and (oo_can_have_published in tobjectdef(def).objectoptions) then
  1949. methods_write_rtti(tobjectdef(def).symtable,rt,[vis_published],true);
  1950. end;
  1951. end;
  1952. classrefdef,
  1953. pointerdef:
  1954. if not is_objc_class_or_protocol(tabstractpointerdef(def).pointeddef) then
  1955. write_rtti(tabstractpointerdef(def).pointeddef,rt);
  1956. procvardef:
  1957. params_write_rtti(tabstractprocdef(def),rt,false);
  1958. else
  1959. ;
  1960. end;
  1961. end;
  1962. procedure TRTTIWriter.write_rtti_reference(tcb: ttai_typedconstbuilder; def: tdef; rt: trttitype);
  1963. begin
  1964. { we don't care about the real type here, because
  1965. a) we don't index into these elements
  1966. b) we may not have the rtti type available at the point that we
  1967. are emitting this data, because of forward definitions etc
  1968. c) if the rtti is emitted in another unit, we won't have the type
  1969. available at all
  1970. For the cases where the type is emitted in the current unit and hence
  1971. the underlying system will detect and complain about symbol def
  1972. mismatches, type conversions will have to be inserted afterwards (like
  1973. in llvm/llvmtype)
  1974. }
  1975. if not assigned(def) or is_void(def) or ((rt<>initrtti) and is_objc_class_or_protocol(def)) then
  1976. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype)
  1977. else
  1978. tcb.emit_tai(Tai_const.Create_sym(get_rtti_label(def,rt,true)),voidpointertype);
  1979. end;
  1980. function TRTTIWriter.ref_rtti(def:tdef;rt:trttitype;indirect:boolean;suffix:tsymstr):tasmsymbol;
  1981. var
  1982. s : tsymstr;
  1983. begin
  1984. s:=def.rtti_mangledname(rt)+suffix;
  1985. result:=current_asmdata.RefAsmSymbol(s,AT_DATA,indirect);
  1986. if def.owner.moduleid<>current_module.moduleid then
  1987. current_module.add_extern_asmsym(s,AB_EXTERNAL,AT_DATA);
  1988. end;
  1989. procedure TRTTIWriter.write_rtti(def:tdef;rt:trttitype);
  1990. var
  1991. tcb: ttai_typedconstbuilder;
  1992. rttilab: tasmsymbol;
  1993. rttidef: tdef;
  1994. begin
  1995. { only write rtti of definitions from the current module }
  1996. if not findunitsymtable(def.owner).iscurrentunit then
  1997. exit;
  1998. { check if separate initrtti is actually needed }
  1999. if (rt=initrtti) and (not def.needs_separate_initrtti) then
  2000. rt:=fullrtti;
  2001. { prevent recursion }
  2002. if rttidefstate[rt] in def.defstates then
  2003. exit;
  2004. include(def.defstates,rttidefstate[rt]);
  2005. { write first all dependencies }
  2006. write_child_rtti_data(def,rt);
  2007. { write rtti data }
  2008. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_data_force_indirect]);
  2009. tcb.begin_anonymous_record(
  2010. internaltypeprefixName[itp_rttidef]+tstoreddef(def).rtti_mangledname(rt),
  2011. defaultpacking,reqalign,
  2012. targetinfos[target_info.system]^.alignment.recordalignmin,
  2013. targetinfos[target_info.system]^.alignment.maxCrecordalign
  2014. );
  2015. write_rtti_data(tcb,def,rt);
  2016. rttidef:=tcb.end_anonymous_record;
  2017. rttilab:=current_asmdata.DefineAsmSymbol(tstoreddef(def).rtti_mangledname(rt),AB_GLOBAL,AT_DATA_NOINDIRECT,rttidef);
  2018. current_asmdata.AsmLists[al_rtti].concatList(
  2019. tcb.get_final_asmlist(rttilab,rttidef,sec_rodata,rttilab.name,min(target_info.alignment.maxCrecordalign,SizeOf(QWord))));
  2020. tcb.free;
  2021. current_module.add_public_asmsym(rttilab);
  2022. { write additional data }
  2023. write_rtti_extrasyms(def,rt,rttilab);
  2024. end;
  2025. constructor TRTTIWriter.create;
  2026. begin
  2027. if tf_requires_proper_alignment in target_info.flags then
  2028. begin
  2029. reqalign:=min(sizeof(QWord),target_info.alignment.maxCrecordalign);
  2030. defaultpacking:=C_alignment;
  2031. end
  2032. else
  2033. begin
  2034. reqalign:=1;
  2035. defaultpacking:=1;
  2036. end;
  2037. end;
  2038. end.