nobj.pas 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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. ;
  26. type
  27. TVMTBuilder=class
  28. private
  29. _Class : tobjectdef;
  30. handledprotocols: tfpobjectlist;
  31. function is_new_vmt_entry(pd:tprocdef; out overridesclasshelper: boolean):boolean;
  32. procedure add_new_vmt_entry(pd:tprocdef; allowoverridingmethod: boolean);
  33. function check_msg_str(vmtpd, pd: tprocdef):boolean;
  34. function intf_search_procdef_by_name(proc: tprocdef;const name: string): tprocdef;
  35. procedure intf_get_procdefs(ImplIntf:TImplementedInterface;IntfDef:TObjectDef);
  36. procedure intf_get_procdefs_recursive(ImplIntf:TImplementedInterface;IntfDef:TObjectDef);
  37. procedure prot_get_procdefs_recursive(ImplProt:TImplementedInterface;ProtDef:TObjectDef);
  38. procedure intf_optimize_vtbls;
  39. procedure intf_allocate_vtbls;
  40. public
  41. constructor create(c:tobjectdef);
  42. procedure generate_vmt;
  43. procedure build_interface_mappings;
  44. end;
  45. implementation
  46. uses
  47. SysUtils,
  48. globals,verbose,systems,
  49. node,
  50. symbase,symtable,symconst,symtype,defcmp,
  51. dbgbase,
  52. wpobase
  53. ;
  54. {*****************************************************************************
  55. TVMTBuilder
  56. *****************************************************************************}
  57. constructor TVMTBuilder.create(c:tobjectdef);
  58. begin
  59. inherited Create;
  60. _Class:=c;
  61. end;
  62. procedure TVMTBuilder.add_new_vmt_entry(pd:tprocdef; allowoverridingmethod: boolean);
  63. var
  64. i : longint;
  65. vmtentry : pvmtentry;
  66. vmtpd : tprocdef;
  67. begin
  68. { new entry is needed, override was not possible }
  69. { Allowed when overriding a category method for a parent class in a
  70. descendent Objective-C class }
  71. if not allowoverridingmethod and
  72. (po_overridingmethod in pd.procoptions) then
  73. MessagePos1(pd.fileinfo,parser_e_nothing_to_be_overridden,pd.fullprocname(false));
  74. { check that all methods have overload directive }
  75. if not(m_fpc in current_settings.modeswitches) then
  76. begin
  77. for i:=0 to _class.vmtentries.count-1 do
  78. begin
  79. vmtentry:=pvmtentry(_class.vmtentries[i]);
  80. vmtpd:=tprocdef(vmtentry^.procdef);
  81. if (vmtpd.procsym=pd.procsym) and
  82. (not(po_overload in pd.procoptions) or
  83. not(po_overload in vmtpd.procoptions)) then
  84. begin
  85. MessagePos1(pd.fileinfo,parser_e_no_overload_for_all_procs,pd.procsym.realname);
  86. { recover }
  87. include(vmtpd.procoptions,po_overload);
  88. include(pd.procoptions,po_overload);
  89. end;
  90. end;
  91. end;
  92. { Register virtual method and give it a number }
  93. if (po_virtualmethod in pd.procoptions) then
  94. begin
  95. { store vmt entry number in procdef }
  96. if (pd.extnumber<>$ffff) and
  97. (pd.extnumber<>_class.VMTEntries.Count) then
  98. internalerror(200810283);
  99. pd.extnumber:=_class.VMTEntries.Count;
  100. new(vmtentry);
  101. vmtentry^.procdef:=pd;
  102. vmtentry^.procdefderef.reset;
  103. vmtentry^.visibility:=pd.visibility;
  104. _class.VMTEntries.Add(vmtentry);
  105. end;
  106. end;
  107. function TVMTBuilder.check_msg_str(vmtpd, pd: tprocdef): boolean;
  108. begin
  109. result:=true;
  110. if not(is_objc_class_or_protocol(_class)) then
  111. begin
  112. { the only requirement for normal methods is that both either
  113. have a message string or not (the value is irrelevant) }
  114. if ((pd.procoptions * [po_msgstr]) <> (vmtpd.procoptions * [po_msgstr])) then
  115. begin
  116. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  117. tprocsym(vmtpd.procsym).write_parameter_lists(pd);
  118. result:=false;
  119. end
  120. end
  121. else
  122. begin
  123. { the compiler should have ensured that the protocol or parent
  124. class method has a message name specified }
  125. if not(po_msgstr in vmtpd.procoptions) then
  126. internalerror(2009070601);
  127. if not(po_msgstr in pd.procoptions) then
  128. begin
  129. { copy the protocol's/parent class' message name to the one in
  130. the class if none has been specified there }
  131. include(pd.procoptions,po_msgstr);
  132. pd.messageinf.str:=stringdup(vmtpd.messageinf.str^);
  133. end
  134. else
  135. begin
  136. { if both have a message name, make sure they are equal }
  137. if (vmtpd.messageinf.str^<>pd.messageinf.str^) then
  138. begin
  139. MessagePos2(pd.fileinfo,parser_e_objc_message_name_changed,vmtpd.messageinf.str^,pd.messageinf.str^);
  140. result:=false;
  141. end;
  142. end;
  143. end;
  144. end;
  145. function TVMTBuilder.is_new_vmt_entry(pd:tprocdef; out overridesclasshelper: boolean):boolean;
  146. const
  147. po_comp = [po_classmethod,po_virtualmethod,po_staticmethod,po_interrupt,po_iocheck,po_msgint,
  148. po_exports,po_varargs,po_explicitparaloc,po_nostackframe];
  149. var
  150. i : longint;
  151. hasequalpara,
  152. hasoverloads,
  153. pdoverload : boolean;
  154. srsym : tsym;
  155. st : tsymtable;
  156. // returns true if we can stop checking, false if we have to continue
  157. function found_entry(var vmtpd: tprocdef; var vmtentryvis: tvisibility; updatevalues: boolean): boolean;
  158. {$ifdef jvm}
  159. var
  160. javanewtreeok: boolean;
  161. {$endif jvm}
  162. begin
  163. result:=false;
  164. { ignore hidden entries (e.g. virtual overridden by a static) that are not visible anymore }
  165. if vmtentryvis=vis_hidden then
  166. exit;
  167. { ignore different names }
  168. if vmtpd.procsym.name<>pd.procsym.name then
  169. exit;
  170. { hide private methods that are not visible anymore. For this check we
  171. must override the visibility with the highest value in the override chain.
  172. This is required for case (see tw3292) with protected-private-protected where the
  173. same vmtentry is used (PFV) }
  174. if not is_visible_for_object(vmtpd.owner,vmtentryvis,_class) then
  175. exit;
  176. { inherit overload }
  177. if (po_overload in vmtpd.procoptions) then
  178. begin
  179. include(pd.procoptions,po_overload);
  180. pdoverload:=true;
  181. end;
  182. { compare parameter types only, no specifiers yet }
  183. hasequalpara:=(compare_paras(vmtpd.paras,pd.paras,cp_none,[cpo_ignoreuniv,cpo_ignorehidden])>=te_equal);
  184. { check that we are not trying to override a final method }
  185. { in Java, new virtual inheritance trees can never be started ->
  186. treat all methods as "overriding" in the context of this check
  187. (Java does check whether the mangled names are identical, so if they
  188. are not we can stil get away with it) }
  189. if (po_finalmethod in vmtpd.procoptions) and
  190. hasequalpara and
  191. ((po_overridingmethod in pd.procoptions) or
  192. (is_javaclass(_class) and
  193. (pd.mangledname=vmtpd.mangledname))) and
  194. (is_class(_class) or is_objectpascal_helper(_class) or is_javaclass(_class)) then
  195. MessagePos1(pd.fileinfo,parser_e_final_can_no_be_overridden,pd.fullprocname(false))
  196. else
  197. { old definition has virtual
  198. new definition has no virtual or override }
  199. if (po_virtualmethod in vmtpd.procoptions) and
  200. (
  201. not(po_virtualmethod in pd.procoptions) or
  202. (
  203. { new one does not have reintroduce in case of an objccategory }
  204. (is_objccategory(_class) and
  205. not(po_reintroduce in pd.procoptions)) or
  206. { new one does not have override in case of objpas/objc/java class/intf/proto }
  207. ((is_class_or_interface_or_objc_or_java(_class) or is_objectpascal_helper(_class)) and
  208. not is_objccategory(_class) and
  209. not(po_overridingmethod in pd.procoptions)
  210. )
  211. )
  212. ) then
  213. begin
  214. if (
  215. not(pdoverload or hasoverloads) or
  216. hasequalpara
  217. ) then
  218. begin
  219. {$ifdef jvm}
  220. { if the mangled names are different, the inheritance trees
  221. are different too in Java; exception: when the parent method
  222. is a virtual class method or virtual constructor, because
  223. those are looked up dynamicall by name }
  224. javanewtreeok:=
  225. is_java_class_or_interface(_class) and
  226. (pd.jvmmangledbasename(false)<>vmtpd.jvmmangledbasename(false)) and
  227. ((vmtpd.proctypeoption<>potype_constructor) and
  228. not(po_staticmethod in vmtpd.procoptions));
  229. {$endif}
  230. if not(po_reintroduce in pd.procoptions) and
  231. not(po_java_nonvirtual in vmtpd.procoptions) then
  232. if not(is_objc_class_or_protocol(_class))
  233. {$ifdef jvm}
  234. and (not is_java_class_or_interface(_class) or
  235. javanewtreeok)
  236. {$endif jvm}
  237. then
  238. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false))
  239. else
  240. begin
  241. { In Objective-C, you cannot create a new VMT entry to
  242. start a new inheritance tree. We therefore give an
  243. error when the class is implemented in Pascal, to
  244. avoid confusion due to things working differently
  245. with Object Pascal classes.
  246. In case of external classes, we only give a hint,
  247. because requiring override everywhere may make
  248. automated header translation tools too complex.
  249. The same goes for Java. }
  250. {$ifndef jvm}
  251. if hasequalpara then
  252. {$endif}
  253. begin
  254. if not(oo_is_external in _class.objectoptions) then
  255. if not is_objccategory(_class) then
  256. MessagePos1(pd.fileinfo,parser_e_must_use_override,FullTypeName(tdef(vmtpd.owner.defowner),nil))
  257. else
  258. MessagePos1(pd.fileinfo,parser_e_must_use_reintroduce_objc,FullTypeName(tdef(vmtpd.owner.defowner),nil))
  259. { there may be a lot of these in auto-translated
  260. headers, so only calculate the fulltypename if
  261. the hint will be shown }
  262. else if CheckVerbosity(V_Hint) then
  263. if not is_objccategory(_class) then
  264. MessagePos1(pd.fileinfo,parser_h_should_use_override,FullTypeName(tdef(vmtpd.owner.defowner),nil))
  265. else
  266. MessagePos1(pd.fileinfo,parser_h_should_use_reintroduce_objc,FullTypeName(tdef(vmtpd.owner.defowner),nil));
  267. end;
  268. { no new entry, but copy the message name if any from
  269. the procdef in the parent class }
  270. if not is_objc_class_or_protocol(_class) or
  271. hasequalpara then
  272. check_msg_str(vmtpd,pd);
  273. if updatevalues then
  274. begin
  275. { in case of Java, copy the real name from the parent,
  276. since overriding "Destroy" with "destroy" is not
  277. going to work very well }
  278. if is_java_class_or_interface(_class) and
  279. (pd.procsym.realname<>vmtpd.procsym.realname) then
  280. pd.procsym.realname:=vmtpd.procsym.realname;
  281. { in case we are overriding an abstract method,
  282. decrease the number of abstract methods in this class }
  283. if (po_abstractmethod in vmtpd.procoptions) then
  284. dec(tobjectdef(pd.owner.defowner).abstractcnt);
  285. if (vmtpd.extnumber<>i) then
  286. internalerror(2011083101);
  287. pd.extnumber:=vmtpd.extnumber;
  288. vmtpd:=pd;
  289. end;
  290. result:=true;
  291. exit;
  292. {$ifdef jvm}
  293. end
  294. else
  295. if not javanewtreeok and
  296. is_java_class_or_interface(_class) then
  297. begin
  298. { mangled names are the same -> can only override }
  299. MessagePos1(pd.fileinfo,parser_e_must_use_override,FullTypeName(tdef(vmtpd.owner.defowner),nil))
  300. {$endif jvm}
  301. end;
  302. { disable/hide old VMT entry }
  303. if updatevalues then
  304. vmtentryvis:=vis_hidden;
  305. end;
  306. end
  307. { both are virtual? }
  308. else if (po_virtualmethod in pd.procoptions) and
  309. (po_virtualmethod in vmtpd.procoptions) then
  310. begin
  311. { same parameter and return types (parameter specifiers will be checked below) }
  312. if hasequalpara and
  313. compatible_childmethod_resultdef(vmtpd.returndef,pd.returndef) then
  314. begin
  315. { inherite calling convention when it was explicit and the
  316. current definition has none explicit set }
  317. if (po_hascallingconvention in vmtpd.procoptions) and
  318. not(po_hascallingconvention in pd.procoptions) then
  319. begin
  320. pd.proccalloption:=vmtpd.proccalloption;
  321. include(pd.procoptions,po_hascallingconvention);
  322. end;
  323. { All parameter specifiers and some procedure the flags have to match
  324. except abstract and override }
  325. if (compare_paras(vmtpd.paras,pd.paras,cp_all,[cpo_ignoreuniv,cpo_ignorehidden])<te_equal) or
  326. (vmtpd.proccalloption<>pd.proccalloption) or
  327. (vmtpd.proctypeoption<>pd.proctypeoption) or
  328. ((vmtpd.procoptions*po_comp)<>(pd.procoptions*po_comp)) then
  329. begin
  330. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  331. tprocsym(vmtpd.procsym).write_parameter_lists(pd);
  332. end;
  333. check_msg_str(vmtpd,pd);
  334. { Give a note if the new visibility is lower. For a higher
  335. visibility update the vmt info }
  336. if vmtentryvis>pd.visibility then
  337. begin
  338. if po_auto_raised_visibility in vmtpd.procoptions then
  339. begin
  340. if updatevalues then
  341. begin
  342. pd.visibility:=vmtentryvis;
  343. { this one's visibility is now also auto-raised }
  344. include(pd.procoptions,po_auto_raised_visibility);
  345. end
  346. end
  347. else
  348. {$ifdef jvm}
  349. MessagePos4(pd.fileinfo,parser_e_method_lower_visibility,
  350. {$else jvm}
  351. MessagePos4(pd.fileinfo,parser_n_ignore_lower_visibility,
  352. {$endif jvm}
  353. pd.fullprocname(false),
  354. visibilityname[pd.visibility],tobjectdef(vmtpd.owner.defowner).objrealname^,visibilityname[vmtentryvis])
  355. end
  356. else if pd.visibility>vmtentryvis then
  357. begin
  358. if updatevalues then
  359. vmtentryvis:=pd.visibility;
  360. end;
  361. { override old virtual method in VMT }
  362. if updatevalues then
  363. begin
  364. { in case we are overriding an abstract method,
  365. decrease the number of abstract methods in this class }
  366. if (po_overridingmethod in pd.procoptions) and
  367. (po_abstractmethod in vmtpd.procoptions) then
  368. dec(tobjectdef(pd.owner.defowner).abstractcnt);
  369. if (vmtpd.extnumber<>i) then
  370. internalerror(200611084);
  371. pd.extnumber:=vmtpd.extnumber;
  372. { in case of Java, copy the real name from the parent,
  373. since overriding "Destroy" with "destroy" is not
  374. going to work very well }
  375. if is_java_class_or_interface(_class) and
  376. (pd.procsym.realname<>vmtpd.procsym.realname) then
  377. pd.procsym.realname:=vmtpd.procsym.realname;
  378. vmtpd:=pd;
  379. end;
  380. result:=true;
  381. exit;
  382. end
  383. { different parameters }
  384. else
  385. begin
  386. { when we got an override directive then can search futher for
  387. the procedure to override.
  388. If we are starting a new virtual tree then hide the old tree }
  389. if not(po_overridingmethod in pd.procoptions) and
  390. not(pdoverload or hasoverloads) then
  391. begin
  392. if not(po_reintroduce in pd.procoptions) then
  393. begin
  394. if not is_object(_class) and
  395. not is_objc_class_or_protocol(_class) and
  396. not is_java_class_or_interface(_class) then
  397. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false))
  398. else
  399. { objects don't allow starting a new virtual tree
  400. and neither do Objective-C or Java }
  401. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,vmtpd.fullprocname(false));
  402. end;
  403. { disable/hide old VMT entry }
  404. if updatevalues then
  405. vmtentryvis:=vis_hidden;
  406. end;
  407. end;
  408. end;
  409. end;
  410. function found_category_method(st: tsymtable): boolean;
  411. var
  412. entrycount: longint;
  413. cat: tobjectdef;
  414. vmtpd: tprocdef;
  415. vmtvis: tvisibility;
  416. begin
  417. result:=false;
  418. if is_objccategory(tdef(st.defowner)) then
  419. begin
  420. cat:=tobjectdef(st.defowner);
  421. { go through all of the category's methods to find the
  422. vmtentry corresponding to the procdef we are handling }
  423. for entrycount:=0 to cat.vmtentries.Count-1 do
  424. begin
  425. vmtpd:=pvmtentry(cat.vmtentries[entrycount])^.procdef;
  426. vmtvis:=pvmtentry(cat.vmtentries[entrycount])^.visibility;
  427. { don't change the vmtentry of the category }
  428. if found_entry(vmtpd,vmtvis,false) then
  429. begin
  430. result:=true;
  431. exit;
  432. end;
  433. end;
  434. end;
  435. end;
  436. begin
  437. result:=false;
  438. overridesclasshelper:=false;
  439. { Load other values for easier readability }
  440. hasoverloads:=(tprocsym(pd.procsym).ProcdefList.Count>1);
  441. pdoverload:=(po_overload in pd.procoptions);
  442. { compare with all stored definitions }
  443. for i:=0 to _class.vmtentries.Count-1 do
  444. begin
  445. if found_entry(pvmtentry(_class.vmtentries[i])^.procdef, pvmtentry(_class.vmtentries[i])^.visibility,true) then
  446. exit;
  447. end;
  448. { in case of Objective-C, also check the categories that apply to this
  449. class' *parent* for methods to override (don't allow class X to
  450. "override" a method added by a category to class X itself, since in
  451. that case the category method will in fact replace class X'
  452. "overriding" method }
  453. if is_objcclass(_class) and
  454. assigned(_class.childof) and
  455. search_objc_helper(_class.childof,pd.procsym.name,srsym,st) then
  456. begin
  457. overridesclasshelper:=found_category_method(st);
  458. end;
  459. { No entry found, we need to create a new entry }
  460. result:=true;
  461. end;
  462. function TVMTBuilder.intf_search_procdef_by_name(proc: tprocdef;const name: string): tprocdef;
  463. const
  464. po_comp = [po_classmethod,po_staticmethod,po_interrupt,po_iocheck,po_msgint,
  465. po_exports,po_varargs,po_explicitparaloc,po_nostackframe];
  466. var
  467. implprocdef : Tprocdef;
  468. i: cardinal;
  469. hclass : tobjectdef;
  470. hashedid : THashedIDString;
  471. srsym : tsym;
  472. begin
  473. result:=nil;
  474. hashedid.id:=name;
  475. hclass:=_class;
  476. while assigned(hclass) do
  477. begin
  478. srsym:=tsym(hclass.symtable.FindWithHash(hashedid));
  479. if assigned(srsym) and
  480. (srsym.typ=procsym) then
  481. begin
  482. for i:=0 to Tprocsym(srsym).ProcdefList.Count-1 do
  483. begin
  484. implprocdef:=tprocdef(tprocsym(srsym).ProcdefList[i]);
  485. if (implprocdef.procsym=tprocsym(srsym)) and
  486. (compare_paras(proc.paras,implprocdef.paras,cp_all,[cpo_ignorehidden,cpo_ignoreuniv])>=te_equal) and
  487. (compare_defs(proc.returndef,implprocdef.returndef,nothingn)>=te_equal) and
  488. (proc.proccalloption=implprocdef.proccalloption) and
  489. (proc.proctypeoption=implprocdef.proctypeoption) and
  490. ((proc.procoptions*po_comp)=((implprocdef.procoptions+[po_virtualmethod])*po_comp)) and
  491. check_msg_str(proc,implprocdef) then
  492. begin
  493. { does the interface increase the visibility of the
  494. implementing method? }
  495. if implprocdef.visibility<proc.visibility then
  496. {$ifdef jvm}
  497. MessagePos2(implprocdef.fileinfo,type_e_interface_lower_visibility,proc.fullprocname(false),implprocdef.fullprocname(false));
  498. {$else}
  499. MessagePos2(implprocdef.fileinfo,type_w_interface_lower_visibility,proc.fullprocname(false),implprocdef.fullprocname(false));
  500. {$endif}
  501. result:=implprocdef;
  502. exit;
  503. end;
  504. end;
  505. end;
  506. hclass:=hclass.childof;
  507. end;
  508. end;
  509. procedure TVMTBuilder.intf_get_procdefs(ImplIntf:TImplementedInterface;IntfDef:TObjectDef);
  510. var
  511. i : longint;
  512. def : tdef;
  513. hs,
  514. prefix,
  515. mappedname: string;
  516. implprocdef: tprocdef;
  517. begin
  518. prefix:=ImplIntf.IntfDef.symtable.name^+'.';
  519. for i:=0 to IntfDef.symtable.DefList.Count-1 do
  520. begin
  521. def:=tdef(IntfDef.symtable.DefList[i]);
  522. if assigned(def) and
  523. (def.typ=procdef) then
  524. begin
  525. { Find implementing procdef
  526. 1. Check for mapped name
  527. 2. Use symbol name, but only if there's no mapping,
  528. or we're processing ancestor of interface.
  529. When modifying this code, ensure that webtbs/tw11862, webtbs/tw4950
  530. and webtbf/tw19591 stay correct. }
  531. implprocdef:=nil;
  532. hs:=prefix+tprocdef(def).procsym.name;
  533. mappedname:=ImplIntf.GetMapping(hs);
  534. if mappedname<>'' then
  535. implprocdef:=intf_search_procdef_by_name(tprocdef(def),mappedname);
  536. if not assigned(implprocdef) then
  537. if (mappedname='') or (ImplIntf.IntfDef<>IntfDef) then
  538. implprocdef:=intf_search_procdef_by_name(tprocdef(def),tprocdef(def).procsym.name);
  539. { Add procdef to the implemented interface }
  540. if assigned(implprocdef) then
  541. begin
  542. if (tobjectdef(implprocdef.struct).objecttype<>odt_objcclass) then
  543. begin
  544. { in case of Java, copy the real name from the parent,
  545. since overriding "Destroy" with "destroy" is not
  546. going to work very well }
  547. if is_javaclass(implprocdef.struct) and
  548. (implprocdef.procsym.realname<>tprocdef(def).procsym.realname) then
  549. implprocdef.procsym.realname:=tprocdef(def).procsym.realname;
  550. ImplIntf.AddImplProc(implprocdef);
  551. end
  552. else
  553. begin
  554. { If no message name has been specified for the method
  555. in the objcclass, copy it from the protocol
  556. definition. }
  557. if not(po_msgstr in tprocdef(def).procoptions) then
  558. begin
  559. include(tprocdef(def).procoptions,po_msgstr);
  560. implprocdef.messageinf.str:=stringdup(tprocdef(def).messageinf.str^);
  561. end
  562. else
  563. begin
  564. { If a message name has been specified in the
  565. objcclass, it has to match the message name in the
  566. protocol definition. }
  567. if (implprocdef.messageinf.str^<>tprocdef(def).messageinf.str^) then
  568. MessagePos2(implprocdef.fileinfo,parser_e_objc_message_name_changed,tprocdef(def).messageinf.str^,implprocdef.messageinf.str^);
  569. end;
  570. end;
  571. end
  572. else
  573. if (ImplIntf.IType=etStandard) and
  574. not(po_optional in tprocdef(def).procoptions) then
  575. MessagePos1(_Class.typesym.fileinfo,sym_e_no_matching_implementation_found,tprocdef(def).fullprocname(false));
  576. end;
  577. end;
  578. end;
  579. procedure TVMTBuilder.intf_get_procdefs_recursive(ImplIntf:TImplementedInterface;IntfDef:TObjectDef);
  580. begin
  581. if assigned(IntfDef.childof) then
  582. intf_get_procdefs_recursive(ImplIntf,IntfDef.childof);
  583. intf_get_procdefs(ImplIntf,IntfDef);
  584. end;
  585. procedure TVMTBuilder.prot_get_procdefs_recursive(ImplProt:TImplementedInterface;ProtDef:TObjectDef);
  586. var
  587. i: longint;
  588. begin
  589. { don't check the same protocol twice }
  590. if handledprotocols.IndexOf(ProtDef)<>-1 then
  591. exit;
  592. handledprotocols.add(ProtDef);
  593. for i:=0 to ProtDef.ImplementedInterfaces.count-1 do
  594. prot_get_procdefs_recursive(ImplProt,TImplementedInterface(ProtDef.ImplementedInterfaces[i]).intfdef);
  595. intf_get_procdefs(ImplProt,ProtDef);
  596. end;
  597. procedure TVMTBuilder.intf_optimize_vtbls;
  598. type
  599. tcompintfentry = record
  600. weight: longint;
  601. compintf: longint;
  602. end;
  603. { Max 1000 interface in the class header interfaces it's enough imho }
  604. tcompintfs = array[0..1000] of tcompintfentry;
  605. pcompintfs = ^tcompintfs;
  606. tequals = array[0..1000] of longint;
  607. pequals = ^tequals;
  608. timpls = array[0..1000] of longint;
  609. pimpls = ^timpls;
  610. var
  611. aequals: pequals;
  612. compats: pcompintfs;
  613. impls: pimpls;
  614. ImplIntfCount,
  615. w,i,j,k: longint;
  616. ImplIntfI,
  617. ImplIntfJ : TImplementedInterface;
  618. cij: boolean;
  619. cji: boolean;
  620. begin
  621. ImplIntfCount:=_class.ImplementedInterfaces.count;
  622. if ImplIntfCount>=High(tequals) then
  623. Internalerror(200006135);
  624. getmem(compats,sizeof(tcompintfentry)*ImplIntfCount);
  625. getmem(aequals,sizeof(longint)*ImplIntfCount);
  626. getmem(impls,sizeof(longint)*ImplIntfCount);
  627. filldword(compats^,(sizeof(tcompintfentry) div sizeof(dword))*ImplIntfCount,dword(-1));
  628. filldword(aequals^,ImplIntfCount,dword(-1));
  629. filldword(impls^,ImplIntfCount,dword(-1));
  630. { ismergepossible is a containing relation
  631. meaning of ismergepossible(a,b,w) =
  632. if implementorfunction map of a is contained implementorfunction map of b
  633. imp(a,b) and imp(b,c) => imp(a,c) ; imp(a,b) and imp(b,a) => a == b
  634. }
  635. { the order is very important for correct allocation }
  636. for i:=0 to ImplIntfCount-1 do
  637. begin
  638. for j:=i+1 to ImplIntfCount-1 do
  639. begin
  640. ImplIntfI:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  641. ImplIntfJ:=TImplementedInterface(_class.ImplementedInterfaces[j]);
  642. cij:=ImplIntfI.IsImplMergePossible(ImplIntfJ,w);
  643. cji:=ImplIntfJ.IsImplMergePossible(ImplIntfI,w);
  644. if cij and cji then { i equal j }
  645. begin
  646. { get minimum index of equal }
  647. if aequals^[j]=-1 then
  648. aequals^[j]:=i;
  649. end
  650. else if cij then
  651. begin
  652. { get minimum index of maximum weight }
  653. if compats^[i].weight<w then
  654. begin
  655. compats^[i].weight:=w;
  656. compats^[i].compintf:=j;
  657. end;
  658. end
  659. else if cji then
  660. begin
  661. { get minimum index of maximum weight }
  662. if (compats^[j].weight<w) then
  663. begin
  664. compats^[j].weight:=w;
  665. compats^[j].compintf:=i;
  666. end;
  667. end;
  668. end;
  669. end;
  670. { Reset, no replacements by default }
  671. for i:=0 to ImplIntfCount-1 do
  672. impls^[i]:=i;
  673. { Replace vtbls when equal or compat, repeat
  674. until there are no replacements possible anymore. This is
  675. needed for the cases like:
  676. First loop: 2->3, 3->1
  677. Second loop: 2->1 (because 3 was replaced with 1)
  678. }
  679. repeat
  680. k:=0;
  681. for i:=0 to ImplIntfCount-1 do
  682. begin
  683. if compats^[impls^[i]].compintf<>-1 then
  684. impls^[i]:=compats^[impls^[i]].compintf
  685. else if aequals^[impls^[i]]<>-1 then
  686. impls^[i]:=aequals^[impls^[i]]
  687. else
  688. inc(k);
  689. end;
  690. until k=ImplIntfCount;
  691. { Update the VtblImplIntf }
  692. for i:=0 to ImplIntfCount-1 do
  693. begin
  694. ImplIntfI:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  695. ImplIntfI.VtblImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[impls^[i]]);
  696. end;
  697. freemem(compats);
  698. freemem(aequals);
  699. freemem(impls);
  700. end;
  701. procedure TVMTBuilder.intf_allocate_vtbls;
  702. var
  703. i : longint;
  704. ImplIntf : TImplementedInterface;
  705. begin
  706. { Allocation vtbl space }
  707. for i:=0 to _class.ImplementedInterfaces.count-1 do
  708. begin
  709. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  710. { if it implements itself and if it's not implemented by delegation }
  711. if (ImplIntf.VtblImplIntf=ImplIntf) and (ImplIntf.IType=etStandard) then
  712. begin
  713. { allocate a pointer in the object memory }
  714. with tObjectSymtable(_class.symtable) do
  715. begin
  716. datasize:=align(datasize,sizeof(pint));
  717. ImplIntf.Ioffset:=datasize;
  718. datasize:=datasize+sizeof(pint);
  719. end;
  720. end;
  721. end;
  722. { Update ioffset of current interface with the ioffset from
  723. the interface that is reused to implements this interface }
  724. for i:=0 to _class.ImplementedInterfaces.count-1 do
  725. begin
  726. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  727. if ImplIntf.VtblImplIntf<>ImplIntf then
  728. ImplIntf.IOffset:=ImplIntf.VtblImplIntf.IOffset;
  729. end;
  730. end;
  731. procedure TVMTBuilder.generate_vmt;
  732. var
  733. i : longint;
  734. def : tdef;
  735. old_current_structdef : tabstractrecorddef;
  736. overridesclasshelper : boolean;
  737. begin
  738. old_current_structdef:=current_structdef;
  739. current_structdef:=_class;
  740. _class.resetvmtentries;
  741. { inherit (copy) VMT from parent object }
  742. if assigned(_class.childof) then
  743. begin
  744. if not assigned(_class.childof.vmtentries) then
  745. internalerror(200810281);
  746. _class.copyvmtentries(_class.childof);
  747. end;
  748. { process all procdefs, we must process the defs to
  749. keep the same order as that is written in the source
  750. to be compatible with the indexes in the interface vtable (PFV) }
  751. for i:=0 to _class.symtable.DefList.Count-1 do
  752. begin
  753. def:=tdef(_class.symtable.DefList[i]);
  754. if def.typ=procdef then
  755. begin
  756. { VMT entry }
  757. if is_new_vmt_entry(tprocdef(def),overridesclasshelper) then
  758. add_new_vmt_entry(tprocdef(def),overridesclasshelper);
  759. end;
  760. end;
  761. build_interface_mappings;
  762. if assigned(_class.ImplementedInterfaces) and
  763. not(is_objc_class_or_protocol(_class)) and
  764. not(is_java_class_or_interface(_class)) then
  765. begin
  766. { Optimize interface tables to reuse wrappers }
  767. intf_optimize_vtbls;
  768. { Allocate interface tables }
  769. intf_allocate_vtbls;
  770. end;
  771. current_structdef:=old_current_structdef;
  772. end;
  773. procedure TVMTBuilder.build_interface_mappings;
  774. var
  775. ImplIntf : TImplementedInterface;
  776. i: longint;
  777. begin
  778. { Find Procdefs implementing the interfaces (both Objective-C protocols
  779. and Java interfaces can have multiple parent interfaces, but in that
  780. case obviously no implementations are required) }
  781. if assigned(_class.ImplementedInterfaces) and
  782. not(_class.objecttype in [odt_objcprotocol,odt_interfacejava]) and
  783. // abstract java classes do not have to implement all interface
  784. // methods. todo: check that non-abstract descendents do!
  785. not((_class.objecttype=odt_javaclass) and (oo_is_abstract in _class.objectoptions)) then
  786. begin
  787. { Collect implementor functions into the tImplementedInterface.procdefs }
  788. case _class.objecttype of
  789. odt_class:
  790. begin
  791. for i:=0 to _class.ImplementedInterfaces.count-1 do
  792. begin
  793. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  794. intf_get_procdefs_recursive(ImplIntf,ImplIntf.IntfDef)
  795. end;
  796. end;
  797. odt_objcclass,
  798. odt_javaclass:
  799. begin
  800. { Object Pascal interfaces are afterwards optimized via the
  801. intf_optimize_vtbls() method, but we can't do this for
  802. protocols/Java interfaces -> check for duplicates here
  803. already. }
  804. handledprotocols:=tfpobjectlist.create(false);
  805. for i:=0 to _class.ImplementedInterfaces.count-1 do
  806. begin
  807. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  808. prot_get_procdefs_recursive(ImplIntf,ImplIntf.IntfDef);
  809. end;
  810. handledprotocols.free;
  811. end
  812. else
  813. internalerror(2009091801);
  814. end
  815. end;
  816. end;
  817. end.