nobj.pas 42 KB

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