nobj.pas 65 KB

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