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