nobj.pas 42 KB

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