nobj.pas 65 KB

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