2
0

nobj.pas 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Routines for the code generation of data structures
  4. like VMT, Messages, VTables, Interfaces descs
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit nobj;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cutils,cclasses,
  23. globtype,
  24. symdef,symsym,
  25. aasmbase,aasmtai,aasmdata
  26. ;
  27. type
  28. TVMTBuilder=class
  29. private
  30. _Class : tobjectdef;
  31. handledprotocols: tfpobjectlist;
  32. function is_new_vmt_entry(pd:tprocdef; out overridesclasshelper: boolean):boolean;
  33. procedure add_new_vmt_entry(pd:tprocdef; allowoverridingmethod: boolean);
  34. function check_msg_str(vmtpd, pd: tprocdef):boolean;
  35. function intf_search_procdef_by_name(proc: tprocdef;const name: string): tprocdef;
  36. procedure intf_get_procdefs(ImplIntf:TImplementedInterface;IntfDef:TObjectDef);
  37. procedure intf_get_procdefs_recursive(ImplIntf:TImplementedInterface;IntfDef:TObjectDef);
  38. procedure prot_get_procdefs_recursive(ImplProt:TImplementedInterface;ProtDef:TObjectDef);
  39. procedure intf_optimize_vtbls;
  40. procedure intf_allocate_vtbls;
  41. public
  42. constructor create(c:tobjectdef);
  43. destructor destroy;override;
  44. procedure generate_vmt;
  45. procedure build_interface_mappings;
  46. end;
  47. type
  48. pprocdeftree = ^tprocdeftree;
  49. tprocdeftree = record
  50. data : tprocdef;
  51. nl : tasmlabel;
  52. l,r : pprocdeftree;
  53. end;
  54. TVMTWriter=class
  55. private
  56. _Class : tobjectdef;
  57. { message tables }
  58. root : pprocdeftree;
  59. procedure disposeprocdeftree(p : pprocdeftree);
  60. procedure insertmsgint(p:TObject;arg:pointer);
  61. procedure insertmsgstr(p:TObject;arg:pointer);
  62. procedure insertint(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  63. procedure insertstr(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  64. procedure writenames(p : pprocdeftree);
  65. procedure writeintentry(p : pprocdeftree);
  66. procedure writestrentry(p : pprocdeftree);
  67. {$ifdef WITHDMT}
  68. { dmt }
  69. procedure insertdmtentry(p:TObject;arg:pointer);
  70. procedure writedmtindexentry(p : pprocdeftree);
  71. procedure writedmtaddressentry(p : pprocdeftree);
  72. {$endif}
  73. { published methods }
  74. procedure do_count_published_methods(p:TObject;arg:pointer);
  75. procedure do_gen_published_methods(p:TObject;arg:pointer);
  76. { virtual methods }
  77. procedure writevirtualmethods(List:TAsmList);
  78. { interface tables }
  79. function intf_get_vtbl_name(AImplIntf:TImplementedInterface): string;
  80. procedure intf_create_vtbl(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  81. procedure intf_gen_intf_ref(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  82. function intf_write_table:TAsmLabel;
  83. { generates the message tables for a class }
  84. function genstrmsgtab : tasmlabel;
  85. function genintmsgtab : tasmlabel;
  86. function genpublishedmethodstable : tasmlabel;
  87. function generate_field_table : tasmlabel;
  88. {$ifdef WITHDMT}
  89. { generates a DMT for _class }
  90. function gendmt : tasmlabel;
  91. {$endif WITHDMT}
  92. public
  93. constructor create(c:tobjectdef);
  94. destructor destroy;override;
  95. { write the VMT to al_globals }
  96. procedure writevmt;
  97. procedure writeinterfaceids;
  98. end;
  99. implementation
  100. uses
  101. SysUtils,
  102. globals,verbose,systems,
  103. node,
  104. symbase,symtable,symconst,symtype,defcmp,
  105. dbgbase,
  106. ncgrtti,
  107. wpobase
  108. ;
  109. {*****************************************************************************
  110. TVMTBuilder
  111. *****************************************************************************}
  112. constructor TVMTBuilder.create(c:tobjectdef);
  113. begin
  114. inherited Create;
  115. _Class:=c;
  116. end;
  117. destructor TVMTBuilder.destroy;
  118. begin
  119. end;
  120. procedure TVMTBuilder.add_new_vmt_entry(pd:tprocdef; allowoverridingmethod: boolean);
  121. var
  122. i : longint;
  123. vmtentry : pvmtentry;
  124. vmtpd : tprocdef;
  125. begin
  126. { new entry is needed, override was not possible }
  127. { Allowed when overriding a category method for a parent class in a
  128. descendent Objective-C class }
  129. if not allowoverridingmethod and
  130. (po_overridingmethod in pd.procoptions) then
  131. MessagePos1(pd.fileinfo,parser_e_nothing_to_be_overridden,pd.fullprocname(false));
  132. { check that all methods have overload directive }
  133. if not(m_fpc in current_settings.modeswitches) then
  134. begin
  135. for i:=0 to _class.vmtentries.count-1 do
  136. begin
  137. vmtentry:=pvmtentry(_class.vmtentries[i]);
  138. vmtpd:=tprocdef(vmtentry^.procdef);
  139. if (vmtpd.procsym=pd.procsym) and
  140. (not(po_overload in pd.procoptions) or
  141. not(po_overload in vmtpd.procoptions)) then
  142. begin
  143. MessagePos1(pd.fileinfo,parser_e_no_overload_for_all_procs,pd.procsym.realname);
  144. { recover }
  145. include(vmtpd.procoptions,po_overload);
  146. include(pd.procoptions,po_overload);
  147. end;
  148. end;
  149. end;
  150. { Register virtual method and give it a number }
  151. if (po_virtualmethod in pd.procoptions) then
  152. begin
  153. { store vmt entry number in procdef }
  154. if (pd.extnumber<>$ffff) and
  155. (pd.extnumber<>_class.VMTEntries.Count) then
  156. internalerror(200810283);
  157. pd.extnumber:=_class.VMTEntries.Count;
  158. new(vmtentry);
  159. vmtentry^.procdef:=pd;
  160. vmtentry^.procdefderef.reset;
  161. vmtentry^.visibility:=pd.visibility;
  162. _class.VMTEntries.Add(vmtentry);
  163. end;
  164. end;
  165. function TVMTBuilder.check_msg_str(vmtpd, pd: tprocdef): boolean;
  166. begin
  167. result:=true;
  168. if not(is_objc_class_or_protocol(_class)) then
  169. begin
  170. { the only requirement for normal methods is that both either
  171. have a message string or not (the value is irrelevant) }
  172. if ((pd.procoptions * [po_msgstr]) <> (vmtpd.procoptions * [po_msgstr])) then
  173. begin
  174. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  175. tprocsym(vmtpd.procsym).write_parameter_lists(pd);
  176. result:=false;
  177. end
  178. end
  179. else
  180. begin
  181. { the compiler should have ensured that the protocol or parent
  182. class method has a message name specified }
  183. if not(po_msgstr in vmtpd.procoptions) then
  184. internalerror(2009070601);
  185. if not(po_msgstr in pd.procoptions) then
  186. begin
  187. { copy the protocol's/parent class' message name to the one in
  188. the class if none has been specified there }
  189. include(pd.procoptions,po_msgstr);
  190. pd.messageinf.str:=stringdup(vmtpd.messageinf.str^);
  191. end
  192. else
  193. begin
  194. { if both have a message name, make sure they are equal }
  195. if (vmtpd.messageinf.str^<>pd.messageinf.str^) then
  196. begin
  197. MessagePos2(pd.fileinfo,parser_e_objc_message_name_changed,vmtpd.messageinf.str^,pd.messageinf.str^);
  198. result:=false;
  199. end;
  200. end;
  201. end;
  202. end;
  203. function TVMTBuilder.is_new_vmt_entry(pd:tprocdef; out overridesclasshelper: boolean):boolean;
  204. const
  205. po_comp = [po_classmethod,po_virtualmethod,po_staticmethod,po_interrupt,po_iocheck,po_msgint,
  206. po_exports,po_varargs,po_explicitparaloc,po_nostackframe];
  207. var
  208. i : longint;
  209. hasequalpara,
  210. hasoverloads,
  211. pdoverload : boolean;
  212. srsym : tsym;
  213. st : tsymtable;
  214. // returns true if we can stop checking, false if we have to continue
  215. function found_entry(var vmtpd: tprocdef; var vmtentryvis: tvisibility; updatevalues: boolean): boolean;
  216. begin
  217. result:=false;
  218. { ignore hidden entries (e.g. virtual overridden by a static) that are not visible anymore }
  219. if vmtentryvis=vis_hidden then
  220. exit;
  221. { ignore different names }
  222. if vmtpd.procsym.name<>pd.procsym.name then
  223. exit;
  224. { hide private methods that are not visible anymore. For this check we
  225. must override the visibility with the highest value in the override chain.
  226. This is required for case (see tw3292) with protected-private-protected where the
  227. same vmtentry is used (PFV) }
  228. if not is_visible_for_object(vmtpd.owner,vmtentryvis,_class) then
  229. exit;
  230. { inherit overload }
  231. if (po_overload in vmtpd.procoptions) then
  232. begin
  233. include(pd.procoptions,po_overload);
  234. pdoverload:=true;
  235. end;
  236. { compare parameter types only, no specifiers yet }
  237. hasequalpara:=(compare_paras(vmtpd.paras,pd.paras,cp_none,[cpo_ignoreuniv,cpo_ignorehidden])>=te_equal);
  238. { check that we are not trying to override a final method }
  239. if (po_finalmethod in vmtpd.procoptions) and
  240. hasequalpara and (po_overridingmethod in pd.procoptions) and
  241. (is_class(_class) or is_objectpascal_helper(_class)) then
  242. MessagePos1(pd.fileinfo,parser_e_final_can_no_be_overridden,pd.fullprocname(false))
  243. else
  244. { old definition has virtual
  245. new definition has no virtual or override }
  246. if (po_virtualmethod in vmtpd.procoptions) and
  247. (
  248. not(po_virtualmethod in pd.procoptions) or
  249. (
  250. { new one does not have reintroduce in case of an objccategory }
  251. (is_objccategory(_class) and not(po_reintroduce in pd.procoptions)) or
  252. { new one does not have override in case of objpas/objc class/helper/intf/proto }
  253. (
  254. (is_class_or_interface_or_objc(_class) or is_objectpascal_helper(_class)) and
  255. not is_objccategory(_class) and not(po_overridingmethod in pd.procoptions)
  256. )
  257. )
  258. ) then
  259. begin
  260. if (
  261. not(pdoverload or hasoverloads) or
  262. hasequalpara
  263. ) then
  264. begin
  265. if not(po_reintroduce in pd.procoptions) then
  266. if not(is_objc_class_or_protocol(_class)) then
  267. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false))
  268. else
  269. begin
  270. { In Objective-C, you cannot create a new VMT entry to
  271. start a new inheritance tree. We therefore give an
  272. error when the class is implemented in Pascal, to
  273. avoid confusion due to things working differently
  274. with Object Pascal classes.
  275. In case of external classes, we only give a hint,
  276. because requiring override everywhere may make
  277. automated header translation tools too complex. }
  278. if not(oo_is_external in _class.objectoptions) then
  279. if not is_objccategory(_class) then
  280. MessagePos1(pd.fileinfo,parser_e_must_use_override_objc,FullTypeName(tdef(vmtpd.owner.defowner),nil))
  281. else
  282. MessagePos1(pd.fileinfo,parser_e_must_use_reintroduce_objc,FullTypeName(tdef(vmtpd.owner.defowner),nil))
  283. { there may be a lot of these in auto-translated
  284. heaeders, so only calculate the fulltypename if
  285. the hint will be shown }
  286. else if CheckVerbosity(V_Hint) then
  287. if not is_objccategory(_class) then
  288. MessagePos1(pd.fileinfo,parser_h_should_use_override_objc,FullTypeName(tdef(vmtpd.owner.defowner),nil))
  289. else
  290. MessagePos1(pd.fileinfo,parser_h_should_use_reintroduce_objc,FullTypeName(tdef(vmtpd.owner.defowner),nil));
  291. { no new entry, but copy the message name if any from
  292. the procdef in the parent class }
  293. check_msg_str(vmtpd,pd);
  294. result:=true;
  295. exit;
  296. end;
  297. { disable/hide old VMT entry }
  298. if updatevalues then
  299. vmtentryvis:=vis_hidden;
  300. end;
  301. end
  302. { both are virtual? }
  303. else if (po_virtualmethod in pd.procoptions) and
  304. (po_virtualmethod in vmtpd.procoptions) then
  305. begin
  306. { same parameter and return types (parameter specifiers will be checked below) }
  307. if hasequalpara and
  308. compatible_childmethod_resultdef(vmtpd.returndef,pd.returndef) then
  309. begin
  310. { inherite calling convention when it was explicit and the
  311. current definition has none explicit set }
  312. if (po_hascallingconvention in vmtpd.procoptions) and
  313. not(po_hascallingconvention in pd.procoptions) then
  314. begin
  315. pd.proccalloption:=vmtpd.proccalloption;
  316. include(pd.procoptions,po_hascallingconvention);
  317. end;
  318. { All parameter specifiers and some procedure the flags have to match
  319. except abstract and override }
  320. if (compare_paras(vmtpd.paras,pd.paras,cp_all,[cpo_ignoreuniv,cpo_ignorehidden])<te_equal) or
  321. (vmtpd.proccalloption<>pd.proccalloption) or
  322. (vmtpd.proctypeoption<>pd.proctypeoption) or
  323. ((vmtpd.procoptions*po_comp)<>(pd.procoptions*po_comp)) then
  324. begin
  325. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  326. tprocsym(vmtpd.procsym).write_parameter_lists(pd);
  327. end;
  328. check_msg_str(vmtpd,pd);
  329. { Give a note if the new visibility is lower. For a higher
  330. visibility update the vmt info }
  331. if vmtentryvis>pd.visibility then
  332. MessagePos4(pd.fileinfo,parser_n_ignore_lower_visibility,pd.fullprocname(false),
  333. visibilityname[pd.visibility],tobjectdef(vmtpd.owner.defowner).objrealname^,visibilityname[vmtentryvis])
  334. else if pd.visibility>vmtentryvis then
  335. begin
  336. if updatevalues then
  337. vmtentryvis:=pd.visibility;
  338. end;
  339. { override old virtual method in VMT }
  340. if updatevalues then
  341. begin
  342. if (vmtpd.extnumber<>i) then
  343. internalerror(200611084);
  344. pd.extnumber:=vmtpd.extnumber;
  345. vmtpd:=pd;
  346. end;
  347. result:=true;
  348. exit;
  349. end
  350. { different parameters }
  351. else
  352. begin
  353. { when we got an override directive then can search futher for
  354. the procedure to override.
  355. If we are starting a new virtual tree then hide the old tree }
  356. if not(po_overridingmethod in pd.procoptions) and
  357. not(pdoverload or hasoverloads) then
  358. begin
  359. if not(po_reintroduce in pd.procoptions) then
  360. begin
  361. if not is_object(_class) and
  362. not is_objc_class_or_protocol(_class) then
  363. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false))
  364. else
  365. { objects don't allow starting a new virtual tree
  366. and neither does Objective-C }
  367. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,vmtpd.fullprocname(false));
  368. end;
  369. { disable/hide old VMT entry }
  370. if updatevalues then
  371. vmtentryvis:=vis_hidden;
  372. end;
  373. end;
  374. end;
  375. end;
  376. function found_category_method(st: tsymtable): boolean;
  377. var
  378. entrycount: longint;
  379. cat: tobjectdef;
  380. vmtpd: tprocdef;
  381. vmtvis: tvisibility;
  382. begin
  383. result:=false;
  384. if is_objccategory(tdef(st.defowner)) then
  385. begin
  386. cat:=tobjectdef(st.defowner);
  387. { go through all of the category's methods to find the
  388. vmtentry corresponding to the procdef we are handling }
  389. for entrycount:=0 to cat.vmtentries.Count-1 do
  390. begin
  391. vmtpd:=pvmtentry(cat.vmtentries[entrycount])^.procdef;
  392. vmtvis:=pvmtentry(cat.vmtentries[entrycount])^.visibility;
  393. { don't change the vmtentry of the category }
  394. if found_entry(vmtpd,vmtvis,false) then
  395. begin
  396. result:=true;
  397. exit;
  398. end;
  399. end;
  400. end;
  401. end;
  402. begin
  403. result:=false;
  404. overridesclasshelper:=false;
  405. { Load other values for easier readability }
  406. hasoverloads:=(tprocsym(pd.procsym).ProcdefList.Count>1);
  407. pdoverload:=(po_overload in pd.procoptions);
  408. { compare with all stored definitions }
  409. for i:=0 to _class.vmtentries.Count-1 do
  410. begin
  411. if found_entry(pvmtentry(_class.vmtentries[i])^.procdef, pvmtentry(_class.vmtentries[i])^.visibility,true) then
  412. exit;
  413. end;
  414. { in case of Objective-C, also check the categories that apply to this
  415. class' *parent* for methods to override (don't allow class X to
  416. "override" a method added by a category to class X itself, since in
  417. that case the category method will in fact replace class X'
  418. "overriding" method }
  419. if is_objcclass(_class) and
  420. assigned(_class.childof) and
  421. search_objc_helper(_class.childof,pd.procsym.name,srsym,st) then
  422. begin
  423. overridesclasshelper:=found_category_method(st);
  424. end;
  425. { No entry found, we need to create a new entry }
  426. result:=true;
  427. end;
  428. function TVMTBuilder.intf_search_procdef_by_name(proc: tprocdef;const name: string): tprocdef;
  429. const
  430. po_comp = [po_classmethod,po_staticmethod,po_interrupt,po_iocheck,po_msgint,
  431. po_exports,po_varargs,po_explicitparaloc,po_nostackframe];
  432. var
  433. implprocdef : Tprocdef;
  434. i: cardinal;
  435. hclass : tobjectdef;
  436. hashedid : THashedIDString;
  437. srsym : tsym;
  438. begin
  439. result:=nil;
  440. hashedid.id:=name;
  441. hclass:=_class;
  442. while assigned(hclass) do
  443. begin
  444. srsym:=tsym(hclass.symtable.FindWithHash(hashedid));
  445. if assigned(srsym) and
  446. (srsym.typ=procsym) then
  447. begin
  448. for i:=0 to Tprocsym(srsym).ProcdefList.Count-1 do
  449. begin
  450. implprocdef:=tprocdef(tprocsym(srsym).ProcdefList[i]);
  451. if (implprocdef.procsym=tprocsym(srsym)) and
  452. (compare_paras(proc.paras,implprocdef.paras,cp_all,[cpo_ignorehidden,cpo_comparedefaultvalue,cpo_ignoreuniv])>=te_equal) and
  453. (compare_defs(proc.returndef,implprocdef.returndef,nothingn)>=te_equal) and
  454. (proc.proccalloption=implprocdef.proccalloption) and
  455. (proc.proctypeoption=implprocdef.proctypeoption) and
  456. ((proc.procoptions*po_comp)=((implprocdef.procoptions+[po_virtualmethod])*po_comp)) and
  457. check_msg_str(proc,implprocdef) then
  458. begin
  459. result:=implprocdef;
  460. exit;
  461. end;
  462. end;
  463. end;
  464. hclass:=hclass.childof;
  465. end;
  466. end;
  467. procedure TVMTBuilder.intf_get_procdefs(ImplIntf:TImplementedInterface;IntfDef:TObjectDef);
  468. var
  469. i : longint;
  470. def : tdef;
  471. hs,
  472. prefix,
  473. mappedname: string;
  474. implprocdef: tprocdef;
  475. begin
  476. prefix:=ImplIntf.IntfDef.symtable.name^+'.';
  477. for i:=0 to IntfDef.symtable.DefList.Count-1 do
  478. begin
  479. def:=tdef(IntfDef.symtable.DefList[i]);
  480. if assigned(def) and
  481. (def.typ=procdef) then
  482. begin
  483. { Find implementing procdef
  484. 1. Check for mapped name
  485. 2. Use symbol name, but only if there's no mapping,
  486. or we're processing ancestor of interface.
  487. When modifying this code, ensure that webtbs/tw11862, webtbs/tw4950
  488. and webtbf/tw19591 stay correct. }
  489. implprocdef:=nil;
  490. hs:=prefix+tprocdef(def).procsym.name;
  491. mappedname:=ImplIntf.GetMapping(hs);
  492. if mappedname<>'' then
  493. implprocdef:=intf_search_procdef_by_name(tprocdef(def),mappedname);
  494. if not assigned(implprocdef) then
  495. if (mappedname='') or (ImplIntf.IntfDef<>IntfDef) then
  496. implprocdef:=intf_search_procdef_by_name(tprocdef(def),tprocdef(def).procsym.name);
  497. { Add procdef to the implemented interface }
  498. if assigned(implprocdef) then
  499. begin
  500. if (tobjectdef(implprocdef.struct).objecttype<>odt_objcclass) then
  501. ImplIntf.AddImplProc(implprocdef)
  502. else
  503. begin
  504. { If no message name has been specified for the method
  505. in the objcclass, copy it from the protocol
  506. definition. }
  507. if not(po_msgstr in tprocdef(def).procoptions) then
  508. begin
  509. include(tprocdef(def).procoptions,po_msgstr);
  510. implprocdef.messageinf.str:=stringdup(tprocdef(def).messageinf.str^);
  511. end
  512. else
  513. begin
  514. { If a message name has been specified in the
  515. objcclass, it has to match the message name in the
  516. protocol definition. }
  517. if (implprocdef.messageinf.str^<>tprocdef(def).messageinf.str^) then
  518. MessagePos2(implprocdef.fileinfo,parser_e_objc_message_name_changed,tprocdef(def).messageinf.str^,implprocdef.messageinf.str^);
  519. end;
  520. end;
  521. end
  522. else
  523. if (ImplIntf.IType=etStandard) and
  524. not(po_optional in tprocdef(def).procoptions) then
  525. MessagePos1(_Class.typesym.fileinfo,sym_e_no_matching_implementation_found,tprocdef(def).fullprocname(false));
  526. end;
  527. end;
  528. end;
  529. procedure TVMTBuilder.intf_get_procdefs_recursive(ImplIntf:TImplementedInterface;IntfDef:TObjectDef);
  530. begin
  531. if assigned(IntfDef.childof) then
  532. intf_get_procdefs_recursive(ImplIntf,IntfDef.childof);
  533. intf_get_procdefs(ImplIntf,IntfDef);
  534. end;
  535. procedure TVMTBuilder.prot_get_procdefs_recursive(ImplProt:TImplementedInterface;ProtDef:TObjectDef);
  536. var
  537. i: longint;
  538. begin
  539. { don't check the same protocol twice }
  540. if handledprotocols.IndexOf(ProtDef)<>-1 then
  541. exit;
  542. handledprotocols.add(ProtDef);
  543. for i:=0 to ProtDef.ImplementedInterfaces.count-1 do
  544. prot_get_procdefs_recursive(ImplProt,TImplementedInterface(ProtDef.ImplementedInterfaces[i]).intfdef);
  545. intf_get_procdefs(ImplProt,ProtDef);
  546. end;
  547. procedure TVMTBuilder.intf_optimize_vtbls;
  548. type
  549. tcompintfentry = record
  550. weight: longint;
  551. compintf: longint;
  552. end;
  553. { Max 1000 interface in the class header interfaces it's enough imho }
  554. tcompintfs = array[0..1000] of tcompintfentry;
  555. pcompintfs = ^tcompintfs;
  556. tequals = array[0..1000] of longint;
  557. pequals = ^tequals;
  558. timpls = array[0..1000] of longint;
  559. pimpls = ^timpls;
  560. var
  561. aequals: pequals;
  562. compats: pcompintfs;
  563. impls: pimpls;
  564. ImplIntfCount,
  565. w,i,j,k: longint;
  566. ImplIntfI,
  567. ImplIntfJ : TImplementedInterface;
  568. cij: boolean;
  569. cji: boolean;
  570. begin
  571. ImplIntfCount:=_class.ImplementedInterfaces.count;
  572. if ImplIntfCount>=High(tequals) then
  573. Internalerror(200006135);
  574. getmem(compats,sizeof(tcompintfentry)*ImplIntfCount);
  575. getmem(aequals,sizeof(longint)*ImplIntfCount);
  576. getmem(impls,sizeof(longint)*ImplIntfCount);
  577. filldword(compats^,(sizeof(tcompintfentry) div sizeof(dword))*ImplIntfCount,dword(-1));
  578. filldword(aequals^,ImplIntfCount,dword(-1));
  579. filldword(impls^,ImplIntfCount,dword(-1));
  580. { ismergepossible is a containing relation
  581. meaning of ismergepossible(a,b,w) =
  582. if implementorfunction map of a is contained implementorfunction map of b
  583. imp(a,b) and imp(b,c) => imp(a,c) ; imp(a,b) and imp(b,a) => a == b
  584. }
  585. { the order is very important for correct allocation }
  586. for i:=0 to ImplIntfCount-1 do
  587. begin
  588. for j:=i+1 to ImplIntfCount-1 do
  589. begin
  590. ImplIntfI:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  591. ImplIntfJ:=TImplementedInterface(_class.ImplementedInterfaces[j]);
  592. cij:=ImplIntfI.IsImplMergePossible(ImplIntfJ,w);
  593. cji:=ImplIntfJ.IsImplMergePossible(ImplIntfI,w);
  594. if cij and cji then { i equal j }
  595. begin
  596. { get minimum index of equal }
  597. if aequals^[j]=-1 then
  598. aequals^[j]:=i;
  599. end
  600. else if cij then
  601. begin
  602. { get minimum index of maximum weight }
  603. if compats^[i].weight<w then
  604. begin
  605. compats^[i].weight:=w;
  606. compats^[i].compintf:=j;
  607. end;
  608. end
  609. else if cji then
  610. begin
  611. { get minimum index of maximum weight }
  612. if (compats^[j].weight<w) then
  613. begin
  614. compats^[j].weight:=w;
  615. compats^[j].compintf:=i;
  616. end;
  617. end;
  618. end;
  619. end;
  620. { Reset, no replacements by default }
  621. for i:=0 to ImplIntfCount-1 do
  622. impls^[i]:=i;
  623. { Replace vtbls when equal or compat, repeat
  624. until there are no replacements possible anymore. This is
  625. needed for the cases like:
  626. First loop: 2->3, 3->1
  627. Second loop: 2->1 (because 3 was replaced with 1)
  628. }
  629. repeat
  630. k:=0;
  631. for i:=0 to ImplIntfCount-1 do
  632. begin
  633. if compats^[impls^[i]].compintf<>-1 then
  634. impls^[i]:=compats^[impls^[i]].compintf
  635. else if aequals^[impls^[i]]<>-1 then
  636. impls^[i]:=aequals^[impls^[i]]
  637. else
  638. inc(k);
  639. end;
  640. until k=ImplIntfCount;
  641. { Update the VtblImplIntf }
  642. for i:=0 to ImplIntfCount-1 do
  643. begin
  644. ImplIntfI:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  645. ImplIntfI.VtblImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[impls^[i]]);
  646. end;
  647. freemem(compats);
  648. freemem(aequals);
  649. freemem(impls);
  650. end;
  651. procedure TVMTBuilder.intf_allocate_vtbls;
  652. var
  653. i : longint;
  654. ImplIntf : TImplementedInterface;
  655. begin
  656. { Allocation vtbl space }
  657. for i:=0 to _class.ImplementedInterfaces.count-1 do
  658. begin
  659. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  660. { if it implements itself and if it's not implemented by delegation }
  661. if (ImplIntf.VtblImplIntf=ImplIntf) and (ImplIntf.IType=etStandard) then
  662. begin
  663. { allocate a pointer in the object memory }
  664. with tObjectSymtable(_class.symtable) do
  665. begin
  666. datasize:=align(datasize,sizeof(pint));
  667. ImplIntf.Ioffset:=datasize;
  668. datasize:=datasize+sizeof(pint);
  669. end;
  670. end;
  671. end;
  672. { Update ioffset of current interface with the ioffset from
  673. the interface that is reused to implements this interface }
  674. for i:=0 to _class.ImplementedInterfaces.count-1 do
  675. begin
  676. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  677. if ImplIntf.VtblImplIntf<>ImplIntf then
  678. ImplIntf.IOffset:=ImplIntf.VtblImplIntf.IOffset;
  679. end;
  680. end;
  681. procedure TVMTBuilder.generate_vmt;
  682. var
  683. i : longint;
  684. def : tdef;
  685. old_current_structdef : tabstractrecorddef;
  686. overridesclasshelper : boolean;
  687. begin
  688. old_current_structdef:=current_structdef;
  689. current_structdef:=_class;
  690. _class.resetvmtentries;
  691. { inherit (copy) VMT from parent object }
  692. if assigned(_class.childof) then
  693. begin
  694. if not assigned(_class.childof.vmtentries) then
  695. internalerror(200810281);
  696. _class.copyvmtentries(_class.childof);
  697. end;
  698. { process all procdefs, we must process the defs to
  699. keep the same order as that is written in the source
  700. to be compatible with the indexes in the interface vtable (PFV) }
  701. for i:=0 to _class.symtable.DefList.Count-1 do
  702. begin
  703. def:=tdef(_class.symtable.DefList[i]);
  704. if def.typ=procdef then
  705. begin
  706. { VMT entry }
  707. if is_new_vmt_entry(tprocdef(def),overridesclasshelper) then
  708. add_new_vmt_entry(tprocdef(def),overridesclasshelper);
  709. end;
  710. end;
  711. build_interface_mappings;
  712. if assigned(_class.ImplementedInterfaces) and
  713. not(is_objc_class_or_protocol(_class)) then
  714. begin
  715. { Optimize interface tables to reuse wrappers }
  716. intf_optimize_vtbls;
  717. { Allocate interface tables }
  718. intf_allocate_vtbls;
  719. end;
  720. current_structdef:=old_current_structdef;
  721. end;
  722. procedure TVMTBuilder.build_interface_mappings;
  723. var
  724. ImplIntf : TImplementedInterface;
  725. i: longint;
  726. begin
  727. { Find Procdefs implementing the interfaces }
  728. if assigned(_class.ImplementedInterfaces) and
  729. (_class.objecttype<>odt_objcprotocol) then
  730. begin
  731. { Collect implementor functions into the tImplementedInterface.procdefs }
  732. case _class.objecttype of
  733. odt_class:
  734. begin
  735. for i:=0 to _class.ImplementedInterfaces.count-1 do
  736. begin
  737. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  738. intf_get_procdefs_recursive(ImplIntf,ImplIntf.IntfDef)
  739. end;
  740. end;
  741. odt_objcclass:
  742. begin
  743. { Object Pascal interfaces are afterwards optimized via the
  744. intf_optimize_vtbls() method, but we can't do this for
  745. protocols -> check for duplicates here already. }
  746. handledprotocols:=tfpobjectlist.create(false);
  747. for i:=0 to _class.ImplementedInterfaces.count-1 do
  748. begin
  749. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  750. prot_get_procdefs_recursive(ImplIntf,ImplIntf.IntfDef);
  751. end;
  752. handledprotocols.free;
  753. end
  754. else
  755. internalerror(2009091801);
  756. end
  757. end;
  758. end;
  759. {*****************************************************************************
  760. TVMTWriter
  761. *****************************************************************************}
  762. constructor TVMTWriter.create(c:tobjectdef);
  763. begin
  764. inherited Create;
  765. _Class:=c;
  766. end;
  767. destructor TVMTWriter.destroy;
  768. begin
  769. end;
  770. {**************************************
  771. Message Tables
  772. **************************************}
  773. procedure TVMTWriter.disposeprocdeftree(p : pprocdeftree);
  774. begin
  775. if assigned(p^.l) then
  776. disposeprocdeftree(p^.l);
  777. if assigned(p^.r) then
  778. disposeprocdeftree(p^.r);
  779. dispose(p);
  780. end;
  781. procedure TVMTWriter.insertint(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  782. begin
  783. if at=nil then
  784. begin
  785. at:=p;
  786. inc(count);
  787. end
  788. else
  789. begin
  790. if p^.data.messageinf.i<at^.data.messageinf.i then
  791. insertint(p,at^.l,count)
  792. else if p^.data.messageinf.i>at^.data.messageinf.i then
  793. insertint(p,at^.r,count)
  794. else
  795. Message1(parser_e_duplicate_message_label,tostr(p^.data.messageinf.i));
  796. end;
  797. end;
  798. procedure TVMTWriter.insertstr(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  799. var
  800. i : integer;
  801. begin
  802. if at=nil then
  803. begin
  804. at:=p;
  805. inc(count);
  806. end
  807. else
  808. begin
  809. i:=CompareStr(p^.data.messageinf.str^,at^.data.messageinf.str^);
  810. if i<0 then
  811. insertstr(p,at^.l,count)
  812. else if i>0 then
  813. insertstr(p,at^.r,count)
  814. else
  815. Message1(parser_e_duplicate_message_label,p^.data.messageinf.str^);
  816. end;
  817. end;
  818. procedure TVMTWriter.insertmsgint(p:TObject;arg:pointer);
  819. var
  820. i : longint;
  821. pd : Tprocdef;
  822. pt : pprocdeftree;
  823. begin
  824. if tsym(p).typ<>procsym then
  825. exit;
  826. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  827. begin
  828. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  829. if po_msgint in pd.procoptions then
  830. begin
  831. new(pt);
  832. pt^.data:=pd;
  833. pt^.l:=nil;
  834. pt^.r:=nil;
  835. insertint(pt,root,plongint(arg)^);
  836. end;
  837. end;
  838. end;
  839. procedure TVMTWriter.insertmsgstr(p:TObject;arg:pointer);
  840. var
  841. i : longint;
  842. pd : Tprocdef;
  843. pt : pprocdeftree;
  844. begin
  845. if tsym(p).typ<>procsym then
  846. exit;
  847. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  848. begin
  849. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  850. if po_msgstr in pd.procoptions then
  851. begin
  852. new(pt);
  853. pt^.data:=pd;
  854. pt^.l:=nil;
  855. pt^.r:=nil;
  856. insertstr(pt,root,plongint(arg)^);
  857. end;
  858. end;
  859. end;
  860. procedure TVMTWriter.writenames(p : pprocdeftree);
  861. var
  862. ca : pchar;
  863. len : byte;
  864. begin
  865. current_asmdata.getdatalabel(p^.nl);
  866. if assigned(p^.l) then
  867. writenames(p^.l);
  868. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  869. current_asmdata.asmlists[al_globals].concat(Tai_label.Create(p^.nl));
  870. len:=length(p^.data.messageinf.str^);
  871. current_asmdata.asmlists[al_globals].concat(tai_const.create_8bit(len));
  872. getmem(ca,len+1);
  873. move(p^.data.messageinf.str^[1],ca^,len);
  874. ca[len]:=#0;
  875. current_asmdata.asmlists[al_globals].concat(Tai_string.Create_pchar(ca,len));
  876. if assigned(p^.r) then
  877. writenames(p^.r);
  878. end;
  879. procedure TVMTWriter.writestrentry(p : pprocdeftree);
  880. begin
  881. if assigned(p^.l) then
  882. writestrentry(p^.l);
  883. { write name label }
  884. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  885. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(p^.nl));
  886. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  887. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(p^.data.mangledname,0));
  888. if assigned(p^.r) then
  889. writestrentry(p^.r);
  890. end;
  891. function TVMTWriter.genstrmsgtab : tasmlabel;
  892. var
  893. count : longint;
  894. begin
  895. root:=nil;
  896. count:=0;
  897. { insert all message handlers into a tree, sorted by name }
  898. _class.symtable.SymList.ForEachCall(@insertmsgstr,@count);
  899. { write all names }
  900. if assigned(root) then
  901. writenames(root);
  902. { now start writing of the message string table }
  903. current_asmdata.getdatalabel(result);
  904. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  905. current_asmdata.asmlists[al_globals].concat(Tai_label.Create(result));
  906. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(longint))));
  907. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_32bit(count));
  908. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  909. if assigned(root) then
  910. begin
  911. writestrentry(root);
  912. disposeprocdeftree(root);
  913. end;
  914. end;
  915. procedure TVMTWriter.writeintentry(p : pprocdeftree);
  916. begin
  917. if assigned(p^.l) then
  918. writeintentry(p^.l);
  919. { write name label }
  920. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(longint))));
  921. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  922. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  923. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(p^.data.mangledname,0));
  924. if assigned(p^.r) then
  925. writeintentry(p^.r);
  926. end;
  927. function TVMTWriter.genintmsgtab : tasmlabel;
  928. var
  929. r : tasmlabel;
  930. count : longint;
  931. begin
  932. root:=nil;
  933. count:=0;
  934. { insert all message handlers into a tree, sorted by name }
  935. _class.symtable.SymList.ForEachCall(@insertmsgint,@count);
  936. { now start writing of the message string table }
  937. current_asmdata.getdatalabel(r);
  938. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  939. current_asmdata.asmlists[al_globals].concat(Tai_label.Create(r));
  940. genintmsgtab:=r;
  941. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(longint))));
  942. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_32bit(count));
  943. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  944. if assigned(root) then
  945. begin
  946. writeintentry(root);
  947. disposeprocdeftree(root);
  948. end;
  949. end;
  950. {$ifdef WITHDMT}
  951. {**************************************
  952. DMT
  953. **************************************}
  954. procedure TVMTWriter.insertdmtentry(p:TObject;arg:pointer);
  955. var
  956. hp : tprocdef;
  957. pt : pprocdeftree;
  958. begin
  959. if tsym(p).typ=procsym then
  960. begin
  961. hp:=tprocsym(p).definition;
  962. while assigned(hp) do
  963. begin
  964. if (po_msgint in hp.procoptions) then
  965. begin
  966. new(pt);
  967. pt^.p:=hp;
  968. pt^.l:=nil;
  969. pt^.r:=nil;
  970. insertint(pt,root);
  971. end;
  972. hp:=hp.nextoverloaded;
  973. end;
  974. end;
  975. end;
  976. procedure TVMTWriter.writedmtindexentry(p : pprocdeftree);
  977. begin
  978. if assigned(p^.l) then
  979. writedmtindexentry(p^.l);
  980. al_globals.concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  981. if assigned(p^.r) then
  982. writedmtindexentry(p^.r);
  983. end;
  984. procedure TVMTWriter.writedmtaddressentry(p : pprocdeftree);
  985. begin
  986. if assigned(p^.l) then
  987. writedmtaddressentry(p^.l);
  988. al_globals.concat(Tai_const_symbol.Createname(p^.data.mangledname,0));
  989. if assigned(p^.r) then
  990. writedmtaddressentry(p^.r);
  991. end;
  992. function TVMTWriter.gendmt : tasmlabel;
  993. var
  994. r : tasmlabel;
  995. begin
  996. root:=nil;
  997. count:=0;
  998. gendmt:=nil;
  999. { insert all message handlers into a tree, sorted by number }
  1000. _class.symtable.SymList.ForEachCall(insertdmtentry);
  1001. if count>0 then
  1002. begin
  1003. current_asmdata.getdatalabel(r);
  1004. gendmt:=r;
  1005. al_globals.concat(cai_align.create(const_align(sizeof(pint))));
  1006. al_globals.concat(Tai_label.Create(r));
  1007. { entries for caching }
  1008. al_globals.concat(Tai_const.Create_ptr(0));
  1009. al_globals.concat(Tai_const.Create_ptr(0));
  1010. al_globals.concat(Tai_const.Create_32bit(count));
  1011. if assigned(root) then
  1012. begin
  1013. writedmtindexentry(root);
  1014. writedmtaddressentry(root);
  1015. disposeprocdeftree(root);
  1016. end;
  1017. end;
  1018. end;
  1019. {$endif WITHDMT}
  1020. {**************************************
  1021. Published Methods
  1022. **************************************}
  1023. procedure TVMTWriter.do_count_published_methods(p:TObject;arg:pointer);
  1024. var
  1025. i : longint;
  1026. pd : tprocdef;
  1027. begin
  1028. if (tsym(p).typ<>procsym) then
  1029. exit;
  1030. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  1031. begin
  1032. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  1033. if (pd.procsym=tsym(p)) and
  1034. (pd.visibility=vis_published) then
  1035. inc(plongint(arg)^);
  1036. end;
  1037. end;
  1038. procedure TVMTWriter.do_gen_published_methods(p:TObject;arg:pointer);
  1039. var
  1040. i : longint;
  1041. l : tasmlabel;
  1042. pd : tprocdef;
  1043. begin
  1044. if (tsym(p).typ<>procsym) then
  1045. exit;
  1046. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  1047. begin
  1048. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  1049. if (pd.procsym=tsym(p)) and
  1050. (pd.visibility=vis_published) then
  1051. begin
  1052. current_asmdata.getdatalabel(l);
  1053. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,l.name,const_align(sizeof(pint)));
  1054. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(l));
  1055. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_8bit(length(tsym(p).realname)));
  1056. current_asmdata.asmlists[al_typedconsts].concat(Tai_string.Create(tsym(p).realname));
  1057. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(l));
  1058. if po_abstractmethod in pd.procoptions then
  1059. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil))
  1060. else
  1061. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(pd.mangledname,0));
  1062. end;
  1063. end;
  1064. end;
  1065. function TVMTWriter.genpublishedmethodstable : tasmlabel;
  1066. var
  1067. l : tasmlabel;
  1068. count : longint;
  1069. begin
  1070. count:=0;
  1071. _class.symtable.SymList.ForEachCall(@do_count_published_methods,@count);
  1072. if count>0 then
  1073. begin
  1074. current_asmdata.getdatalabel(l);
  1075. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  1076. current_asmdata.asmlists[al_globals].concat(Tai_label.Create(l));
  1077. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_32bit(count));
  1078. _class.symtable.SymList.ForEachCall(@do_gen_published_methods,nil);
  1079. genpublishedmethodstable:=l;
  1080. end
  1081. else
  1082. genpublishedmethodstable:=nil;
  1083. end;
  1084. function TVMTWriter.generate_field_table : tasmlabel;
  1085. var
  1086. i : longint;
  1087. sym : tsym;
  1088. fieldtable,
  1089. classtable : tasmlabel;
  1090. classindex,
  1091. fieldcount : longint;
  1092. classtablelist : TFPList;
  1093. begin
  1094. classtablelist:=TFPList.Create;
  1095. current_asmdata.getdatalabel(fieldtable);
  1096. current_asmdata.getdatalabel(classtable);
  1097. maybe_new_object_file(current_asmdata.asmlists[al_rtti]);
  1098. new_section(current_asmdata.asmlists[al_rtti],sec_rodata,classtable.name,const_align(sizeof(pint)));
  1099. { retrieve field info fields }
  1100. fieldcount:=0;
  1101. for i:=0 to _class.symtable.SymList.Count-1 do
  1102. begin
  1103. sym:=tsym(_class.symtable.SymList[i]);
  1104. if (sym.typ=fieldvarsym) and
  1105. (sym.visibility=vis_published) then
  1106. begin
  1107. if tfieldvarsym(sym).vardef.typ<>objectdef then
  1108. internalerror(200611032);
  1109. classindex:=classtablelist.IndexOf(tfieldvarsym(sym).vardef);
  1110. if classindex=-1 then
  1111. classtablelist.Add(tfieldvarsym(sym).vardef);
  1112. inc(fieldcount);
  1113. end;
  1114. end;
  1115. { write fields }
  1116. current_asmdata.asmlists[al_rtti].concat(Tai_label.Create(fieldtable));
  1117. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(fieldcount));
  1118. if (tf_requires_proper_alignment in target_info.flags) then
  1119. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  1120. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(classtable));
  1121. for i:=0 to _class.symtable.SymList.Count-1 do
  1122. begin
  1123. sym:=tsym(_class.symtable.SymList[i]);
  1124. if (sym.typ=fieldvarsym) and
  1125. (sym.visibility=vis_published) then
  1126. begin
  1127. if (tf_requires_proper_alignment in target_info.flags) then
  1128. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(pint)));
  1129. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_pint(tfieldvarsym(sym).fieldoffset));
  1130. classindex:=classtablelist.IndexOf(tfieldvarsym(sym).vardef);
  1131. if classindex=-1 then
  1132. internalerror(200611033);
  1133. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(classindex+1));
  1134. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(tfieldvarsym(sym).realname)));
  1135. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(tfieldvarsym(sym).realname));
  1136. end;
  1137. end;
  1138. { generate the class table }
  1139. current_asmdata.asmlists[al_rtti].concat(cai_align.create(const_align(sizeof(pint))));
  1140. current_asmdata.asmlists[al_rtti].concat(Tai_label.Create(classtable));
  1141. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(classtablelist.count));
  1142. if (tf_requires_proper_alignment in target_info.flags) then
  1143. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  1144. for i:=0 to classtablelist.Count-1 do
  1145. current_asmdata.asmlists[al_rtti].concat(Tai_const.Createname(tobjectdef(classtablelist[i]).vmt_mangledname,0));
  1146. classtablelist.free;
  1147. result:=fieldtable;
  1148. end;
  1149. {**************************************
  1150. Interface tables
  1151. **************************************}
  1152. function TVMTWriter.intf_get_vtbl_name(AImplIntf:TImplementedInterface): string;
  1153. begin
  1154. result:=make_mangledname('VTBL',_class.owner,_class.objname^+'_$_'+AImplIntf.IntfDef.objname^);
  1155. end;
  1156. procedure TVMTWriter.intf_create_vtbl(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  1157. var
  1158. pd : tprocdef;
  1159. vtblstr,
  1160. hs : string;
  1161. i : longint;
  1162. begin
  1163. vtblstr:=intf_get_vtbl_name(AImplIntf);
  1164. section_symbol_start(rawdata,vtblstr,AT_DATA,true,sec_data,const_align(sizeof(pint)));
  1165. if assigned(AImplIntf.procdefs) then
  1166. begin
  1167. for i:=0 to AImplIntf.procdefs.count-1 do
  1168. begin
  1169. pd:=tprocdef(AImplIntf.procdefs[i]);
  1170. hs:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+AImplIntf.IntfDef.objname^+'_$_'+
  1171. tostr(i)+'_$_'+pd.mangledname);
  1172. { create reference }
  1173. rawdata.concat(Tai_const.Createname(hs,0));
  1174. end;
  1175. end;
  1176. section_symbol_end(rawdata,vtblstr);
  1177. end;
  1178. procedure TVMTWriter.intf_gen_intf_ref(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  1179. var
  1180. iidlabel,
  1181. guidlabel : tasmlabel;
  1182. i: longint;
  1183. pd: tprocdef;
  1184. begin
  1185. { GUID }
  1186. if AImplIntf.IntfDef.objecttype in [odt_interfacecom] then
  1187. begin
  1188. { label for GUID }
  1189. current_asmdata.getdatalabel(guidlabel);
  1190. rawdata.concat(cai_align.create(const_align(sizeof(pint))));
  1191. rawdata.concat(Tai_label.Create(guidlabel));
  1192. with AImplIntf.IntfDef.iidguid^ do
  1193. begin
  1194. rawdata.concat(Tai_const.Create_32bit(longint(D1)));
  1195. rawdata.concat(Tai_const.Create_16bit(D2));
  1196. rawdata.concat(Tai_const.Create_16bit(D3));
  1197. for i:=Low(D4) to High(D4) do
  1198. rawdata.concat(Tai_const.Create_8bit(D4[i]));
  1199. end;
  1200. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(guidlabel));
  1201. end
  1202. else
  1203. begin
  1204. { nil for Corba interfaces }
  1205. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1206. end;
  1207. { VTable }
  1208. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(intf_get_vtbl_name(AImplIntf.VtblImplIntf),0));
  1209. { IOffset field }
  1210. case AImplIntf.VtblImplIntf.IType of
  1211. etFieldValue, etFieldValueClass,
  1212. etStandard:
  1213. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_pint(AImplIntf.VtblImplIntf.IOffset));
  1214. etStaticMethodResult, etStaticMethodClass:
  1215. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(
  1216. tprocdef(tpropertysym(AImplIntf.ImplementsGetter).propaccesslist[palt_read].procdef).mangledname,
  1217. 0
  1218. ));
  1219. etVirtualMethodResult, etVirtualMethodClass:
  1220. begin
  1221. pd := tprocdef(tpropertysym(AImplIntf.ImplementsGetter).propaccesslist[palt_read].procdef);
  1222. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_pint(tobjectdef(pd.struct).vmtmethodoffset(pd.extnumber)));
  1223. end;
  1224. else
  1225. internalerror(200802162);
  1226. end;
  1227. { IIDStr }
  1228. current_asmdata.getdatalabel(iidlabel);
  1229. rawdata.concat(cai_align.create(const_align(sizeof(pint))));
  1230. rawdata.concat(Tai_label.Create(iidlabel));
  1231. rawdata.concat(Tai_const.Create_8bit(length(AImplIntf.IntfDef.iidstr^)));
  1232. if AImplIntf.IntfDef.objecttype=odt_interfacecom then
  1233. rawdata.concat(Tai_string.Create(upper(AImplIntf.IntfDef.iidstr^)))
  1234. else
  1235. rawdata.concat(Tai_string.Create(AImplIntf.IntfDef.iidstr^));
  1236. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(iidlabel));
  1237. { IType }
  1238. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_pint(aint(AImplIntf.VtblImplIntf.IType)));
  1239. end;
  1240. function TVMTWriter.intf_write_table:TAsmLabel;
  1241. var
  1242. rawdata : TAsmList;
  1243. i : longint;
  1244. ImplIntf : TImplementedInterface;
  1245. intftablelab : tasmlabel;
  1246. begin
  1247. current_asmdata.getdatalabel(intftablelab);
  1248. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  1249. current_asmdata.asmlists[al_globals].concat(Tai_label.Create(intftablelab));
  1250. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_pint(_class.ImplementedInterfaces.count));
  1251. rawdata:=TAsmList.Create;
  1252. { Write vtbls }
  1253. for i:=0 to _class.ImplementedInterfaces.count-1 do
  1254. begin
  1255. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  1256. if ImplIntf.VtblImplIntf=ImplIntf then
  1257. intf_create_vtbl(rawdata,ImplIntf);
  1258. end;
  1259. { Write vtbl references }
  1260. for i:=0 to _class.ImplementedInterfaces.count-1 do
  1261. begin
  1262. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  1263. intf_gen_intf_ref(rawdata,ImplIntf);
  1264. end;
  1265. { Write interface table }
  1266. current_asmdata.asmlists[al_globals].concatlist(rawdata);
  1267. rawdata.free;
  1268. result:=intftablelab;
  1269. end;
  1270. { Write interface identifiers to the data section }
  1271. procedure TVMTWriter.writeinterfaceids;
  1272. var
  1273. i : longint;
  1274. s : string;
  1275. begin
  1276. if assigned(_class.iidguid) then
  1277. begin
  1278. s:=make_mangledname('IID',_class.owner,_class.objname^);
  1279. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1280. new_section(current_asmdata.asmlists[al_globals],sec_rodata,s,const_align(sizeof(pint)));
  1281. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  1282. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_32bit(longint(_class.iidguid^.D1)));
  1283. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_16bit(_class.iidguid^.D2));
  1284. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_16bit(_class.iidguid^.D3));
  1285. for i:=Low(_class.iidguid^.D4) to High(_class.iidguid^.D4) do
  1286. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_8bit(_class.iidguid^.D4[i]));
  1287. end;
  1288. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1289. s:=make_mangledname('IIDSTR',_class.owner,_class.objname^);
  1290. new_section(current_asmdata.asmlists[al_globals],sec_rodata,s,0);
  1291. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  1292. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_8bit(length(_class.iidstr^)));
  1293. current_asmdata.asmlists[al_globals].concat(Tai_string.Create(_class.iidstr^));
  1294. end;
  1295. procedure TVMTWriter.writevirtualmethods(List:TAsmList);
  1296. var
  1297. vmtpd : tprocdef;
  1298. vmtentry : pvmtentry;
  1299. i : longint;
  1300. procname : string;
  1301. {$ifdef vtentry}
  1302. hs : string;
  1303. {$endif vtentry}
  1304. begin
  1305. if not assigned(_class.VMTEntries) then
  1306. exit;
  1307. for i:=0 to _class.VMTEntries.Count-1 do
  1308. begin
  1309. vmtentry:=pvmtentry(_class.vmtentries[i]);
  1310. vmtpd:=vmtentry^.procdef;
  1311. { safety checks }
  1312. if not(po_virtualmethod in vmtpd.procoptions) then
  1313. internalerror(200611082);
  1314. if vmtpd.extnumber<>i then
  1315. internalerror(200611083);
  1316. if (po_abstractmethod in vmtpd.procoptions) then
  1317. procname:='FPC_ABSTRACTERROR'
  1318. else if not wpoinfomanager.optimized_name_for_vmt(_class,vmtpd,procname) then
  1319. procname:=vmtpd.mangledname;
  1320. List.concat(Tai_const.createname(procname,0));
  1321. {$ifdef vtentry}
  1322. hs:='VTENTRY'+'_'+_class.vmt_mangledname+'$$'+tostr(_class.vmtmethodoffset(i) div sizeof(pint));
  1323. current_asmdata.asmlists[al_globals].concat(tai_symbol.CreateName(hs,AT_DATA,0));
  1324. {$endif vtentry}
  1325. end;
  1326. end;
  1327. procedure TVMTWriter.writevmt;
  1328. var
  1329. methodnametable,intmessagetable,
  1330. strmessagetable,classnamelabel,
  1331. fieldtablelabel : tasmlabel;
  1332. hs: string;
  1333. {$ifdef WITHDMT}
  1334. dmtlabel : tasmlabel;
  1335. {$endif WITHDMT}
  1336. interfacetable : tasmlabel;
  1337. begin
  1338. {$ifdef WITHDMT}
  1339. dmtlabel:=gendmt;
  1340. {$endif WITHDMT}
  1341. { write tables for classes, this must be done before the actual
  1342. class is written, because we need the labels defined }
  1343. if is_class(_class) then
  1344. begin
  1345. current_asmdata.getdatalabel(classnamelabel);
  1346. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1347. new_section(current_asmdata.asmlists[al_globals],sec_rodata,classnamelabel.name,const_align(sizeof(pint)));
  1348. { interface table }
  1349. if _class.ImplementedInterfaces.count>0 then
  1350. interfacetable:=intf_write_table;
  1351. methodnametable:=genpublishedmethodstable;
  1352. fieldtablelabel:=generate_field_table;
  1353. { write class name }
  1354. current_asmdata.asmlists[al_globals].concat(Tai_label.Create(classnamelabel));
  1355. hs:=_class.RttiName;
  1356. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_8bit(length(hs)));
  1357. current_asmdata.asmlists[al_globals].concat(Tai_string.Create(hs));
  1358. { generate message and dynamic tables }
  1359. if (oo_has_msgstr in _class.objectoptions) then
  1360. strmessagetable:=genstrmsgtab;
  1361. if (oo_has_msgint in _class.objectoptions) then
  1362. intmessagetable:=genintmsgtab;
  1363. end;
  1364. { write debug info }
  1365. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1366. new_section(current_asmdata.asmlists[al_globals],sec_rodata,_class.vmt_mangledname,const_align(sizeof(pint)));
  1367. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(_class.vmt_mangledname,AT_DATA,0));
  1368. { determine the size with symtable.datasize, because }
  1369. { size gives back 4 for classes }
  1370. current_asmdata.asmlists[al_globals].concat(Tai_const.Create(aitconst_ptr,tObjectSymtable(_class.symtable).datasize));
  1371. current_asmdata.asmlists[al_globals].concat(Tai_const.Create(aitconst_ptr,-int64(tObjectSymtable(_class.symtable).datasize)));
  1372. {$ifdef WITHDMT}
  1373. if _class.classtype=ct_object then
  1374. begin
  1375. if assigned(dmtlabel) then
  1376. current_asmdata.asmlists[al_globals].concat(Tai_const_symbol.Create(dmtlabel)))
  1377. else
  1378. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_ptr(0));
  1379. end;
  1380. {$endif WITHDMT}
  1381. { write pointer to parent VMT, this isn't implemented in TP }
  1382. { but this is not used in FPC ? (PM) }
  1383. { it's not used yet, but the delphi-operators as and is need it (FK) }
  1384. { it is not written for parents that don't have any vmt !! }
  1385. if assigned(_class.childof) and
  1386. (oo_has_vmt in _class.childof.objectoptions) then
  1387. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(_class.childof.vmt_mangledname,0))
  1388. else
  1389. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1390. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  1391. if is_class(_class) then
  1392. begin
  1393. { pointer to class name string }
  1394. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(classnamelabel));
  1395. { pointer to dynamic table or nil }
  1396. if (oo_has_msgint in _class.objectoptions) then
  1397. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(intmessagetable))
  1398. else
  1399. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1400. { pointer to method table or nil }
  1401. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(methodnametable));
  1402. { pointer to field table }
  1403. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(fieldtablelabel));
  1404. { pointer to type info of published section }
  1405. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(RTTIWriter.get_rtti_label(_class,fullrtti)));
  1406. { inittable for con-/destruction }
  1407. if _class.members_need_inittable then
  1408. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(RTTIWriter.get_rtti_label(_class,initrtti)))
  1409. else
  1410. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1411. { auto table }
  1412. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1413. { interface table }
  1414. if _class.ImplementedInterfaces.count>0 then
  1415. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(interfacetable))
  1416. else if _class.implements_any_interfaces then
  1417. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil))
  1418. else
  1419. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(current_asmdata.RefAsmSymbol('FPC_EMPTYINTF')));
  1420. { table for string messages }
  1421. if (oo_has_msgstr in _class.objectoptions) then
  1422. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(strmessagetable))
  1423. else
  1424. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1425. end;
  1426. { write virtual methods }
  1427. writevirtualmethods(current_asmdata.asmlists[al_globals]);
  1428. current_asmdata.asmlists[al_globals].concat(Tai_const.create(aitconst_ptr,0));
  1429. { write the size of the VMT }
  1430. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname(_class.vmt_mangledname));
  1431. {$ifdef vtentry}
  1432. { write vtinherit symbol to notify the linker of the class inheritance tree }
  1433. hs:='VTINHERIT'+'_'+_class.vmt_mangledname+'$$';
  1434. if assigned(_class.childof) then
  1435. hs:=hs+_class.childof.vmt_mangledname
  1436. else
  1437. hs:=hs+_class.vmt_mangledname;
  1438. current_asmdata.asmlists[al_globals].concat(tai_symbol.CreateName(hs,AT_DATA,0));
  1439. {$endif vtentry}
  1440. end;
  1441. end.