ncgrtti.pas 92 KB

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