nobj.pas 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Routines for the code generation of data structures
  4. like VMT, Messages, VTables, Interfaces descs
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit nobj;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cutils,cclasses,
  23. globtype,
  24. symdef,symsym,
  25. aasmbase,aasmtai,aasmdata
  26. ;
  27. type
  28. TVMTBuilder=class
  29. private
  30. _Class : tobjectdef;
  31. handledprotocols: tfpobjectlist;
  32. function is_new_vmt_entry(pd:tprocdef; out overridesclasshelper: boolean):boolean;
  33. procedure add_new_vmt_entry(pd:tprocdef; allowoverridingmethod: boolean);
  34. function check_msg_str(vmtpd, pd: tprocdef):boolean;
  35. function intf_search_procdef_by_name(proc: tprocdef;const name: string): tprocdef;
  36. procedure intf_get_procdefs(ImplIntf:TImplementedInterface;IntfDef:TObjectDef);
  37. procedure intf_get_procdefs_recursive(ImplIntf:TImplementedInterface;IntfDef:TObjectDef);
  38. procedure prot_get_procdefs_recursive(ImplProt:TImplementedInterface;ProtDef:TObjectDef);
  39. procedure intf_optimize_vtbls;
  40. procedure intf_allocate_vtbls;
  41. public
  42. constructor create(c:tobjectdef);
  43. procedure generate_vmt;
  44. procedure build_interface_mappings;
  45. end;
  46. type
  47. pprocdeftree = ^tprocdeftree;
  48. tprocdeftree = record
  49. data : tprocdef;
  50. nl : tasmlabel;
  51. l,r : pprocdeftree;
  52. end;
  53. TVMTWriter=class
  54. private
  55. _Class : tobjectdef;
  56. { message tables }
  57. root : pprocdeftree;
  58. procedure disposeprocdeftree(p : pprocdeftree);
  59. procedure insertmsgint(p:TObject;arg:pointer);
  60. procedure insertmsgstr(p:TObject;arg:pointer);
  61. procedure insertint(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  62. procedure insertstr(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  63. procedure writenames(list : TAsmList;p : pprocdeftree);
  64. procedure writeintentry(list : TAsmList;p : pprocdeftree);
  65. procedure writestrentry(list : TAsmList;p : pprocdeftree);
  66. {$ifdef WITHDMT}
  67. { dmt }
  68. procedure insertdmtentry(p:TObject;arg:pointer);
  69. procedure writedmtindexentry(p : pprocdeftree);
  70. procedure writedmtaddressentry(p : pprocdeftree);
  71. {$endif}
  72. { published methods }
  73. procedure do_count_published_methods(p:TObject;arg:pointer);
  74. procedure do_gen_published_methods(p:TObject;arg:pointer);
  75. { virtual methods }
  76. procedure writevirtualmethods(List:TAsmList);
  77. { interface tables }
  78. function intf_get_vtbl_name(AImplIntf:TImplementedInterface): string;
  79. procedure intf_create_vtbl(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  80. procedure intf_gen_intf_ref(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  81. function intf_write_table(list : TAsmList):TAsmLabel;
  82. { generates the message tables for a class }
  83. function genstrmsgtab(list : TAsmList) : tasmlabel;
  84. function genintmsgtab(list : TAsmList) : tasmlabel;
  85. function genpublishedmethodstable(list : TAsmList) : tasmlabel;
  86. function generate_field_table(list : TAsmList) : tasmlabel;
  87. {$ifdef WITHDMT}
  88. { generates a DMT for _class }
  89. function gendmt : tasmlabel;
  90. {$endif WITHDMT}
  91. public
  92. constructor create(c:tobjectdef);
  93. { write the VMT to al_globals }
  94. procedure writevmt;
  95. procedure writeinterfaceids;
  96. end;
  97. implementation
  98. uses
  99. SysUtils,
  100. globals,verbose,systems,
  101. node,
  102. symbase,symtable,symconst,symtype,defcmp,
  103. dbgbase,
  104. ncgrtti,
  105. wpobase
  106. ;
  107. {*****************************************************************************
  108. TVMTBuilder
  109. *****************************************************************************}
  110. constructor TVMTBuilder.create(c:tobjectdef);
  111. begin
  112. inherited Create;
  113. _Class:=c;
  114. end;
  115. procedure TVMTBuilder.add_new_vmt_entry(pd:tprocdef; allowoverridingmethod: boolean);
  116. var
  117. i : longint;
  118. vmtentry : pvmtentry;
  119. vmtpd : tprocdef;
  120. begin
  121. { new entry is needed, override was not possible }
  122. { Allowed when overriding a category method for a parent class in a
  123. descendent Objective-C class }
  124. if not allowoverridingmethod and
  125. (po_overridingmethod in pd.procoptions) then
  126. MessagePos1(pd.fileinfo,parser_e_nothing_to_be_overridden,pd.fullprocname(false));
  127. { check that all methods have overload directive }
  128. if not(m_fpc in current_settings.modeswitches) then
  129. begin
  130. for i:=0 to _class.vmtentries.count-1 do
  131. begin
  132. vmtentry:=pvmtentry(_class.vmtentries[i]);
  133. vmtpd:=tprocdef(vmtentry^.procdef);
  134. if (vmtpd.procsym=pd.procsym) and
  135. (not(po_overload in pd.procoptions) or
  136. not(po_overload in vmtpd.procoptions)) then
  137. begin
  138. MessagePos1(pd.fileinfo,parser_e_no_overload_for_all_procs,pd.procsym.realname);
  139. { recover }
  140. include(vmtpd.procoptions,po_overload);
  141. include(pd.procoptions,po_overload);
  142. end;
  143. end;
  144. end;
  145. { Register virtual method and give it a number }
  146. if (po_virtualmethod in pd.procoptions) then
  147. begin
  148. { store vmt entry number in procdef }
  149. if (pd.extnumber<>$ffff) and
  150. (pd.extnumber<>_class.VMTEntries.Count) then
  151. internalerror(200810283);
  152. pd.extnumber:=_class.VMTEntries.Count;
  153. new(vmtentry);
  154. vmtentry^.procdef:=pd;
  155. vmtentry^.procdefderef.reset;
  156. vmtentry^.visibility:=pd.visibility;
  157. _class.VMTEntries.Add(vmtentry);
  158. end;
  159. end;
  160. function TVMTBuilder.check_msg_str(vmtpd, pd: tprocdef): boolean;
  161. begin
  162. result:=true;
  163. if not(is_objc_class_or_protocol(_class)) then
  164. begin
  165. { the only requirement for normal methods is that both either
  166. have a message string or not (the value is irrelevant) }
  167. if ((pd.procoptions * [po_msgstr]) <> (vmtpd.procoptions * [po_msgstr])) then
  168. begin
  169. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  170. tprocsym(vmtpd.procsym).write_parameter_lists(pd);
  171. result:=false;
  172. end
  173. end
  174. else
  175. begin
  176. { the compiler should have ensured that the protocol or parent
  177. class method has a message name specified }
  178. if not(po_msgstr in vmtpd.procoptions) then
  179. internalerror(2009070601);
  180. if not(po_msgstr in pd.procoptions) then
  181. begin
  182. { copy the protocol's/parent class' message name to the one in
  183. the class if none has been specified there }
  184. include(pd.procoptions,po_msgstr);
  185. pd.messageinf.str:=stringdup(vmtpd.messageinf.str^);
  186. end
  187. else
  188. begin
  189. { if both have a message name, make sure they are equal }
  190. if (vmtpd.messageinf.str^<>pd.messageinf.str^) then
  191. begin
  192. MessagePos2(pd.fileinfo,parser_e_objc_message_name_changed,vmtpd.messageinf.str^,pd.messageinf.str^);
  193. result:=false;
  194. end;
  195. end;
  196. end;
  197. end;
  198. function TVMTBuilder.is_new_vmt_entry(pd:tprocdef; out overridesclasshelper: boolean):boolean;
  199. const
  200. po_comp = [po_classmethod,po_virtualmethod,po_staticmethod,po_interrupt,po_iocheck,po_msgint,
  201. po_exports,po_varargs,po_explicitparaloc,po_nostackframe];
  202. var
  203. i : longint;
  204. hasequalpara,
  205. hasoverloads,
  206. pdoverload : boolean;
  207. srsym : tsym;
  208. st : tsymtable;
  209. // returns true if we can stop checking, false if we have to continue
  210. function found_entry(var vmtpd: tprocdef; var vmtentryvis: tvisibility; updatevalues: boolean): boolean;
  211. {$ifdef jvm}
  212. var
  213. javanewtreeok: boolean;
  214. {$endif jvm}
  215. begin
  216. result:=false;
  217. { ignore hidden entries (e.g. virtual overridden by a static) that are not visible anymore }
  218. if vmtentryvis=vis_hidden then
  219. exit;
  220. { ignore different names }
  221. if vmtpd.procsym.name<>pd.procsym.name then
  222. exit;
  223. { hide private methods that are not visible anymore. For this check we
  224. must override the visibility with the highest value in the override chain.
  225. This is required for case (see tw3292) with protected-private-protected where the
  226. same vmtentry is used (PFV) }
  227. if not is_visible_for_object(vmtpd.owner,vmtentryvis,_class) then
  228. exit;
  229. { inherit overload }
  230. if (po_overload in vmtpd.procoptions) then
  231. begin
  232. include(pd.procoptions,po_overload);
  233. pdoverload:=true;
  234. end;
  235. { compare parameter types only, no specifiers yet }
  236. hasequalpara:=(compare_paras(vmtpd.paras,pd.paras,cp_none,[cpo_ignoreuniv,cpo_ignorehidden])>=te_equal);
  237. { check that we are not trying to override a final method }
  238. { in Java, new virtual inheritance trees can never be started ->
  239. treat all methods as "overriding" in the context of this check
  240. (Java does check whether the mangled names are identical, so if they
  241. are not we can stil get away with it) }
  242. if (po_finalmethod in vmtpd.procoptions) and
  243. hasequalpara and
  244. ((po_overridingmethod in pd.procoptions) or
  245. (is_javaclass(_class) and
  246. (pd.mangledname=vmtpd.mangledname))) and
  247. (is_class(_class) or is_objectpascal_helper(_class) or is_javaclass(_class)) then
  248. MessagePos1(pd.fileinfo,parser_e_final_can_no_be_overridden,pd.fullprocname(false))
  249. else
  250. { old definition has virtual
  251. new definition has no virtual or override }
  252. if (po_virtualmethod in vmtpd.procoptions) and
  253. (
  254. not(po_virtualmethod in pd.procoptions) or
  255. (
  256. { new one does not have reintroduce in case of an objccategory }
  257. (is_objccategory(_class) and
  258. not(po_reintroduce in pd.procoptions)) or
  259. { new one does not have override in case of objpas/objc/java class/intf/proto }
  260. ((is_class_or_interface_or_objc_or_java(_class) or is_objectpascal_helper(_class)) and
  261. not is_objccategory(_class) and
  262. not(po_overridingmethod in pd.procoptions)
  263. )
  264. )
  265. ) then
  266. begin
  267. if (
  268. not(pdoverload or hasoverloads) or
  269. hasequalpara
  270. ) then
  271. begin
  272. {$ifdef jvm}
  273. { if the mangled names are different, the inheritance trees
  274. are different too in Java; exception: when the parent method
  275. is a virtual class method or virtual constructor, because
  276. those are looked up dynamicall by name }
  277. javanewtreeok:=
  278. is_java_class_or_interface(_class) and
  279. (pd.jvmmangledbasename(false)<>vmtpd.jvmmangledbasename(false)) and
  280. ((vmtpd.proctypeoption<>potype_constructor) and
  281. not(po_staticmethod in vmtpd.procoptions));
  282. {$endif}
  283. if not(po_reintroduce in pd.procoptions) and
  284. not(po_java_nonvirtual in vmtpd.procoptions) then
  285. if not(is_objc_class_or_protocol(_class))
  286. {$ifdef jvm}
  287. and (not is_java_class_or_interface(_class) or
  288. javanewtreeok)
  289. {$endif jvm}
  290. then
  291. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false))
  292. else
  293. begin
  294. { In Objective-C, you cannot create a new VMT entry to
  295. start a new inheritance tree. We therefore give an
  296. error when the class is implemented in Pascal, to
  297. avoid confusion due to things working differently
  298. with Object Pascal classes.
  299. In case of external classes, we only give a hint,
  300. because requiring override everywhere may make
  301. automated header translation tools too complex.
  302. The same goes for Java. }
  303. if not(oo_is_external in _class.objectoptions) then
  304. if not is_objccategory(_class) then
  305. MessagePos1(pd.fileinfo,parser_e_must_use_override,FullTypeName(tdef(vmtpd.owner.defowner),nil))
  306. else
  307. MessagePos1(pd.fileinfo,parser_e_must_use_reintroduce_objc,FullTypeName(tdef(vmtpd.owner.defowner),nil))
  308. { there may be a lot of these in auto-translated
  309. headers, so only calculate the fulltypename if
  310. the hint will be shown }
  311. else if CheckVerbosity(V_Hint) then
  312. if not is_objccategory(_class) then
  313. MessagePos1(pd.fileinfo,parser_h_should_use_override,FullTypeName(tdef(vmtpd.owner.defowner),nil))
  314. else
  315. MessagePos1(pd.fileinfo,parser_h_should_use_reintroduce_objc,FullTypeName(tdef(vmtpd.owner.defowner),nil));
  316. { no new entry, but copy the message name if any from
  317. the procdef in the parent class }
  318. check_msg_str(vmtpd,pd);
  319. if updatevalues then
  320. begin
  321. { in case of Java, copy the real name from the parent,
  322. since overriding "Destroy" with "destroy" is not
  323. going to work very well }
  324. if is_java_class_or_interface(_class) and
  325. (pd.procsym.realname<>vmtpd.procsym.realname) then
  326. pd.procsym.realname:=vmtpd.procsym.realname;
  327. { in case we are overriding an abstract method,
  328. decrease the number of abstract methods in this class }
  329. if (po_abstractmethod in vmtpd.procoptions) then
  330. dec(tobjectdef(pd.owner.defowner).abstractcnt);
  331. if (vmtpd.extnumber<>i) then
  332. internalerror(2011083101);
  333. pd.extnumber:=vmtpd.extnumber;
  334. vmtpd:=pd;
  335. end;
  336. result:=true;
  337. exit;
  338. {$ifdef jvm}
  339. end
  340. else
  341. if not javanewtreeok and
  342. is_java_class_or_interface(_class) then
  343. begin
  344. { mangled names are the same -> can only override }
  345. MessagePos1(pd.fileinfo,parser_e_must_use_override,FullTypeName(tdef(vmtpd.owner.defowner),nil))
  346. {$endif jvm}
  347. end;
  348. { disable/hide old VMT entry }
  349. if updatevalues then
  350. vmtentryvis:=vis_hidden;
  351. end;
  352. end
  353. { both are virtual? }
  354. else if (po_virtualmethod in pd.procoptions) and
  355. (po_virtualmethod in vmtpd.procoptions) then
  356. begin
  357. { same parameter and return types (parameter specifiers will be checked below) }
  358. if hasequalpara and
  359. compatible_childmethod_resultdef(vmtpd.returndef,pd.returndef) then
  360. begin
  361. { inherite calling convention when it was explicit and the
  362. current definition has none explicit set }
  363. if (po_hascallingconvention in vmtpd.procoptions) and
  364. not(po_hascallingconvention in pd.procoptions) then
  365. begin
  366. pd.proccalloption:=vmtpd.proccalloption;
  367. include(pd.procoptions,po_hascallingconvention);
  368. end;
  369. { All parameter specifiers and some procedure the flags have to match
  370. except abstract and override }
  371. if (compare_paras(vmtpd.paras,pd.paras,cp_all,[cpo_ignoreuniv,cpo_ignorehidden])<te_equal) or
  372. (vmtpd.proccalloption<>pd.proccalloption) or
  373. (vmtpd.proctypeoption<>pd.proctypeoption) or
  374. ((vmtpd.procoptions*po_comp)<>(pd.procoptions*po_comp)) then
  375. begin
  376. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  377. tprocsym(vmtpd.procsym).write_parameter_lists(pd);
  378. end;
  379. check_msg_str(vmtpd,pd);
  380. { Give a note if the new visibility is lower. For a higher
  381. visibility update the vmt info }
  382. if vmtentryvis>pd.visibility then
  383. {$ifdef jvm}
  384. MessagePos4(pd.fileinfo,parser_e_method_lower_visibility,
  385. {$else jvm}
  386. MessagePos4(pd.fileinfo,parser_n_ignore_lower_visibility,
  387. {$endif jvm}
  388. pd.fullprocname(false),
  389. visibilityname[pd.visibility],tobjectdef(vmtpd.owner.defowner).objrealname^,visibilityname[vmtentryvis])
  390. else if pd.visibility>vmtentryvis then
  391. begin
  392. if updatevalues then
  393. vmtentryvis:=pd.visibility;
  394. end;
  395. { override old virtual method in VMT }
  396. if updatevalues then
  397. begin
  398. { in case we are overriding an abstract method,
  399. decrease the number of abstract methods in this class }
  400. if (po_overridingmethod in pd.procoptions) and
  401. (po_abstractmethod in vmtpd.procoptions) then
  402. dec(tobjectdef(pd.owner.defowner).abstractcnt);
  403. if (vmtpd.extnumber<>i) then
  404. internalerror(200611084);
  405. pd.extnumber:=vmtpd.extnumber;
  406. { in case of Java, copy the real name from the parent,
  407. since overriding "Destroy" with "destroy" is not
  408. going to work very well }
  409. if is_java_class_or_interface(_class) and
  410. (pd.procsym.realname<>vmtpd.procsym.realname) then
  411. pd.procsym.realname:=vmtpd.procsym.realname;
  412. vmtpd:=pd;
  413. end;
  414. result:=true;
  415. exit;
  416. end
  417. { different parameters }
  418. else
  419. begin
  420. { when we got an override directive then can search futher for
  421. the procedure to override.
  422. If we are starting a new virtual tree then hide the old tree }
  423. if not(po_overridingmethod in pd.procoptions) and
  424. not(pdoverload or hasoverloads) then
  425. begin
  426. if not(po_reintroduce in pd.procoptions) then
  427. begin
  428. if not is_object(_class) and
  429. not is_objc_class_or_protocol(_class) and
  430. not is_java_class_or_interface(_class) then
  431. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false))
  432. else
  433. { objects don't allow starting a new virtual tree
  434. and neither do Objective-C or Java }
  435. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,vmtpd.fullprocname(false));
  436. end;
  437. { disable/hide old VMT entry }
  438. if updatevalues then
  439. vmtentryvis:=vis_hidden;
  440. end;
  441. end;
  442. end;
  443. end;
  444. function found_category_method(st: tsymtable): boolean;
  445. var
  446. entrycount: longint;
  447. cat: tobjectdef;
  448. vmtpd: tprocdef;
  449. vmtvis: tvisibility;
  450. begin
  451. result:=false;
  452. if is_objccategory(tdef(st.defowner)) then
  453. begin
  454. cat:=tobjectdef(st.defowner);
  455. { go through all of the category's methods to find the
  456. vmtentry corresponding to the procdef we are handling }
  457. for entrycount:=0 to cat.vmtentries.Count-1 do
  458. begin
  459. vmtpd:=pvmtentry(cat.vmtentries[entrycount])^.procdef;
  460. vmtvis:=pvmtentry(cat.vmtentries[entrycount])^.visibility;
  461. { don't change the vmtentry of the category }
  462. if found_entry(vmtpd,vmtvis,false) then
  463. begin
  464. result:=true;
  465. exit;
  466. end;
  467. end;
  468. end;
  469. end;
  470. begin
  471. result:=false;
  472. overridesclasshelper:=false;
  473. { Load other values for easier readability }
  474. hasoverloads:=(tprocsym(pd.procsym).ProcdefList.Count>1);
  475. pdoverload:=(po_overload in pd.procoptions);
  476. { compare with all stored definitions }
  477. for i:=0 to _class.vmtentries.Count-1 do
  478. begin
  479. if found_entry(pvmtentry(_class.vmtentries[i])^.procdef, pvmtentry(_class.vmtentries[i])^.visibility,true) then
  480. exit;
  481. end;
  482. { in case of Objective-C, also check the categories that apply to this
  483. class' *parent* for methods to override (don't allow class X to
  484. "override" a method added by a category to class X itself, since in
  485. that case the category method will in fact replace class X'
  486. "overriding" method }
  487. if is_objcclass(_class) and
  488. assigned(_class.childof) and
  489. search_objc_helper(_class.childof,pd.procsym.name,srsym,st) then
  490. begin
  491. overridesclasshelper:=found_category_method(st);
  492. end;
  493. { No entry found, we need to create a new entry }
  494. result:=true;
  495. end;
  496. function TVMTBuilder.intf_search_procdef_by_name(proc: tprocdef;const name: string): tprocdef;
  497. const
  498. po_comp = [po_classmethod,po_staticmethod,po_interrupt,po_iocheck,po_msgint,
  499. po_exports,po_varargs,po_explicitparaloc,po_nostackframe];
  500. var
  501. implprocdef : Tprocdef;
  502. i: cardinal;
  503. hclass : tobjectdef;
  504. hashedid : THashedIDString;
  505. srsym : tsym;
  506. begin
  507. result:=nil;
  508. hashedid.id:=name;
  509. hclass:=_class;
  510. while assigned(hclass) do
  511. begin
  512. srsym:=tsym(hclass.symtable.FindWithHash(hashedid));
  513. if assigned(srsym) and
  514. (srsym.typ=procsym) then
  515. begin
  516. for i:=0 to Tprocsym(srsym).ProcdefList.Count-1 do
  517. begin
  518. implprocdef:=tprocdef(tprocsym(srsym).ProcdefList[i]);
  519. if (implprocdef.procsym=tprocsym(srsym)) and
  520. (compare_paras(proc.paras,implprocdef.paras,cp_all,[cpo_ignorehidden,cpo_ignoreuniv])>=te_equal) and
  521. (compare_defs(proc.returndef,implprocdef.returndef,nothingn)>=te_equal) and
  522. (proc.proccalloption=implprocdef.proccalloption) and
  523. (proc.proctypeoption=implprocdef.proctypeoption) and
  524. ((proc.procoptions*po_comp)=((implprocdef.procoptions+[po_virtualmethod])*po_comp)) and
  525. check_msg_str(proc,implprocdef) then
  526. begin
  527. { does the interface increase the visibility of the
  528. implementing method? }
  529. if implprocdef.visibility<proc.visibility then
  530. {$ifdef jvm}
  531. MessagePos2(implprocdef.fileinfo,type_e_interface_lower_visibility,proc.fullprocname(false),implprocdef.fullprocname(false));
  532. {$else}
  533. MessagePos2(implprocdef.fileinfo,type_w_interface_lower_visibility,proc.fullprocname(false),implprocdef.fullprocname(false));
  534. {$endif}
  535. result:=implprocdef;
  536. exit;
  537. end;
  538. end;
  539. end;
  540. hclass:=hclass.childof;
  541. end;
  542. end;
  543. procedure TVMTBuilder.intf_get_procdefs(ImplIntf:TImplementedInterface;IntfDef:TObjectDef);
  544. var
  545. i : longint;
  546. def : tdef;
  547. hs,
  548. prefix,
  549. mappedname: string;
  550. implprocdef: tprocdef;
  551. begin
  552. prefix:=ImplIntf.IntfDef.symtable.name^+'.';
  553. for i:=0 to IntfDef.symtable.DefList.Count-1 do
  554. begin
  555. def:=tdef(IntfDef.symtable.DefList[i]);
  556. if assigned(def) and
  557. (def.typ=procdef) then
  558. begin
  559. { Find implementing procdef
  560. 1. Check for mapped name
  561. 2. Use symbol name, but only if there's no mapping,
  562. or we're processing ancestor of interface.
  563. When modifying this code, ensure that webtbs/tw11862, webtbs/tw4950
  564. and webtbf/tw19591 stay correct. }
  565. implprocdef:=nil;
  566. hs:=prefix+tprocdef(def).procsym.name;
  567. mappedname:=ImplIntf.GetMapping(hs);
  568. if mappedname<>'' then
  569. implprocdef:=intf_search_procdef_by_name(tprocdef(def),mappedname);
  570. if not assigned(implprocdef) then
  571. if (mappedname='') or (ImplIntf.IntfDef<>IntfDef) then
  572. implprocdef:=intf_search_procdef_by_name(tprocdef(def),tprocdef(def).procsym.name);
  573. { Add procdef to the implemented interface }
  574. if assigned(implprocdef) then
  575. begin
  576. if (tobjectdef(implprocdef.struct).objecttype<>odt_objcclass) then
  577. begin
  578. { in case of Java, copy the real name from the parent,
  579. since overriding "Destroy" with "destroy" is not
  580. going to work very well }
  581. if is_javaclass(implprocdef.struct) and
  582. (implprocdef.procsym.realname<>tprocdef(def).procsym.realname) then
  583. implprocdef.procsym.realname:=tprocdef(def).procsym.realname;
  584. ImplIntf.AddImplProc(implprocdef);
  585. end
  586. else
  587. begin
  588. { If no message name has been specified for the method
  589. in the objcclass, copy it from the protocol
  590. definition. }
  591. if not(po_msgstr in tprocdef(def).procoptions) then
  592. begin
  593. include(tprocdef(def).procoptions,po_msgstr);
  594. implprocdef.messageinf.str:=stringdup(tprocdef(def).messageinf.str^);
  595. end
  596. else
  597. begin
  598. { If a message name has been specified in the
  599. objcclass, it has to match the message name in the
  600. protocol definition. }
  601. if (implprocdef.messageinf.str^<>tprocdef(def).messageinf.str^) then
  602. MessagePos2(implprocdef.fileinfo,parser_e_objc_message_name_changed,tprocdef(def).messageinf.str^,implprocdef.messageinf.str^);
  603. end;
  604. end;
  605. end
  606. else
  607. if (ImplIntf.IType=etStandard) and
  608. not(po_optional in tprocdef(def).procoptions) then
  609. MessagePos1(_Class.typesym.fileinfo,sym_e_no_matching_implementation_found,tprocdef(def).fullprocname(false));
  610. end;
  611. end;
  612. end;
  613. procedure TVMTBuilder.intf_get_procdefs_recursive(ImplIntf:TImplementedInterface;IntfDef:TObjectDef);
  614. begin
  615. if assigned(IntfDef.childof) then
  616. intf_get_procdefs_recursive(ImplIntf,IntfDef.childof);
  617. intf_get_procdefs(ImplIntf,IntfDef);
  618. end;
  619. procedure TVMTBuilder.prot_get_procdefs_recursive(ImplProt:TImplementedInterface;ProtDef:TObjectDef);
  620. var
  621. i: longint;
  622. begin
  623. { don't check the same protocol twice }
  624. if handledprotocols.IndexOf(ProtDef)<>-1 then
  625. exit;
  626. handledprotocols.add(ProtDef);
  627. for i:=0 to ProtDef.ImplementedInterfaces.count-1 do
  628. prot_get_procdefs_recursive(ImplProt,TImplementedInterface(ProtDef.ImplementedInterfaces[i]).intfdef);
  629. intf_get_procdefs(ImplProt,ProtDef);
  630. end;
  631. procedure TVMTBuilder.intf_optimize_vtbls;
  632. type
  633. tcompintfentry = record
  634. weight: longint;
  635. compintf: longint;
  636. end;
  637. { Max 1000 interface in the class header interfaces it's enough imho }
  638. tcompintfs = array[0..1000] of tcompintfentry;
  639. pcompintfs = ^tcompintfs;
  640. tequals = array[0..1000] of longint;
  641. pequals = ^tequals;
  642. timpls = array[0..1000] of longint;
  643. pimpls = ^timpls;
  644. var
  645. aequals: pequals;
  646. compats: pcompintfs;
  647. impls: pimpls;
  648. ImplIntfCount,
  649. w,i,j,k: longint;
  650. ImplIntfI,
  651. ImplIntfJ : TImplementedInterface;
  652. cij: boolean;
  653. cji: boolean;
  654. begin
  655. ImplIntfCount:=_class.ImplementedInterfaces.count;
  656. if ImplIntfCount>=High(tequals) then
  657. Internalerror(200006135);
  658. getmem(compats,sizeof(tcompintfentry)*ImplIntfCount);
  659. getmem(aequals,sizeof(longint)*ImplIntfCount);
  660. getmem(impls,sizeof(longint)*ImplIntfCount);
  661. filldword(compats^,(sizeof(tcompintfentry) div sizeof(dword))*ImplIntfCount,dword(-1));
  662. filldword(aequals^,ImplIntfCount,dword(-1));
  663. filldword(impls^,ImplIntfCount,dword(-1));
  664. { ismergepossible is a containing relation
  665. meaning of ismergepossible(a,b,w) =
  666. if implementorfunction map of a is contained implementorfunction map of b
  667. imp(a,b) and imp(b,c) => imp(a,c) ; imp(a,b) and imp(b,a) => a == b
  668. }
  669. { the order is very important for correct allocation }
  670. for i:=0 to ImplIntfCount-1 do
  671. begin
  672. for j:=i+1 to ImplIntfCount-1 do
  673. begin
  674. ImplIntfI:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  675. ImplIntfJ:=TImplementedInterface(_class.ImplementedInterfaces[j]);
  676. cij:=ImplIntfI.IsImplMergePossible(ImplIntfJ,w);
  677. cji:=ImplIntfJ.IsImplMergePossible(ImplIntfI,w);
  678. if cij and cji then { i equal j }
  679. begin
  680. { get minimum index of equal }
  681. if aequals^[j]=-1 then
  682. aequals^[j]:=i;
  683. end
  684. else if cij then
  685. begin
  686. { get minimum index of maximum weight }
  687. if compats^[i].weight<w then
  688. begin
  689. compats^[i].weight:=w;
  690. compats^[i].compintf:=j;
  691. end;
  692. end
  693. else if cji then
  694. begin
  695. { get minimum index of maximum weight }
  696. if (compats^[j].weight<w) then
  697. begin
  698. compats^[j].weight:=w;
  699. compats^[j].compintf:=i;
  700. end;
  701. end;
  702. end;
  703. end;
  704. { Reset, no replacements by default }
  705. for i:=0 to ImplIntfCount-1 do
  706. impls^[i]:=i;
  707. { Replace vtbls when equal or compat, repeat
  708. until there are no replacements possible anymore. This is
  709. needed for the cases like:
  710. First loop: 2->3, 3->1
  711. Second loop: 2->1 (because 3 was replaced with 1)
  712. }
  713. repeat
  714. k:=0;
  715. for i:=0 to ImplIntfCount-1 do
  716. begin
  717. if compats^[impls^[i]].compintf<>-1 then
  718. impls^[i]:=compats^[impls^[i]].compintf
  719. else if aequals^[impls^[i]]<>-1 then
  720. impls^[i]:=aequals^[impls^[i]]
  721. else
  722. inc(k);
  723. end;
  724. until k=ImplIntfCount;
  725. { Update the VtblImplIntf }
  726. for i:=0 to ImplIntfCount-1 do
  727. begin
  728. ImplIntfI:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  729. ImplIntfI.VtblImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[impls^[i]]);
  730. end;
  731. freemem(compats);
  732. freemem(aequals);
  733. freemem(impls);
  734. end;
  735. procedure TVMTBuilder.intf_allocate_vtbls;
  736. var
  737. i : longint;
  738. ImplIntf : TImplementedInterface;
  739. begin
  740. { Allocation vtbl space }
  741. for i:=0 to _class.ImplementedInterfaces.count-1 do
  742. begin
  743. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  744. { if it implements itself and if it's not implemented by delegation }
  745. if (ImplIntf.VtblImplIntf=ImplIntf) and (ImplIntf.IType=etStandard) then
  746. begin
  747. { allocate a pointer in the object memory }
  748. with tObjectSymtable(_class.symtable) do
  749. begin
  750. datasize:=align(datasize,sizeof(pint));
  751. ImplIntf.Ioffset:=datasize;
  752. datasize:=datasize+sizeof(pint);
  753. end;
  754. end;
  755. end;
  756. { Update ioffset of current interface with the ioffset from
  757. the interface that is reused to implements this interface }
  758. for i:=0 to _class.ImplementedInterfaces.count-1 do
  759. begin
  760. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  761. if ImplIntf.VtblImplIntf<>ImplIntf then
  762. ImplIntf.IOffset:=ImplIntf.VtblImplIntf.IOffset;
  763. end;
  764. end;
  765. procedure TVMTBuilder.generate_vmt;
  766. var
  767. i : longint;
  768. def : tdef;
  769. old_current_structdef : tabstractrecorddef;
  770. overridesclasshelper : boolean;
  771. begin
  772. old_current_structdef:=current_structdef;
  773. current_structdef:=_class;
  774. _class.resetvmtentries;
  775. { inherit (copy) VMT from parent object }
  776. if assigned(_class.childof) then
  777. begin
  778. if not assigned(_class.childof.vmtentries) then
  779. internalerror(200810281);
  780. _class.copyvmtentries(_class.childof);
  781. end;
  782. { process all procdefs, we must process the defs to
  783. keep the same order as that is written in the source
  784. to be compatible with the indexes in the interface vtable (PFV) }
  785. for i:=0 to _class.symtable.DefList.Count-1 do
  786. begin
  787. def:=tdef(_class.symtable.DefList[i]);
  788. if def.typ=procdef then
  789. begin
  790. { VMT entry }
  791. if is_new_vmt_entry(tprocdef(def),overridesclasshelper) then
  792. add_new_vmt_entry(tprocdef(def),overridesclasshelper);
  793. end;
  794. end;
  795. build_interface_mappings;
  796. if assigned(_class.ImplementedInterfaces) and
  797. not(is_objc_class_or_protocol(_class)) and
  798. not(is_java_class_or_interface(_class)) then
  799. begin
  800. { Optimize interface tables to reuse wrappers }
  801. intf_optimize_vtbls;
  802. { Allocate interface tables }
  803. intf_allocate_vtbls;
  804. end;
  805. current_structdef:=old_current_structdef;
  806. end;
  807. procedure TVMTBuilder.build_interface_mappings;
  808. var
  809. ImplIntf : TImplementedInterface;
  810. i: longint;
  811. begin
  812. { Find Procdefs implementing the interfaces (both Objective-C protocols
  813. and Java interfaces can have multiple parent interfaces, but in that
  814. case obviously no implementations are required) }
  815. if assigned(_class.ImplementedInterfaces) and
  816. not(_class.objecttype in [odt_objcprotocol,odt_interfacejava]) and
  817. // abstract java classes do not have to implement all interface
  818. // methods. todo: check that non-abstract descendents do!
  819. not((_class.objecttype=odt_javaclass) and (oo_is_abstract in _class.objectoptions)) then
  820. begin
  821. { Collect implementor functions into the tImplementedInterface.procdefs }
  822. case _class.objecttype of
  823. odt_class:
  824. begin
  825. for i:=0 to _class.ImplementedInterfaces.count-1 do
  826. begin
  827. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  828. intf_get_procdefs_recursive(ImplIntf,ImplIntf.IntfDef)
  829. end;
  830. end;
  831. odt_objcclass,
  832. odt_javaclass:
  833. begin
  834. { Object Pascal interfaces are afterwards optimized via the
  835. intf_optimize_vtbls() method, but we can't do this for
  836. protocols/Java interfaces -> check for duplicates here
  837. already. }
  838. handledprotocols:=tfpobjectlist.create(false);
  839. for i:=0 to _class.ImplementedInterfaces.count-1 do
  840. begin
  841. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  842. prot_get_procdefs_recursive(ImplIntf,ImplIntf.IntfDef);
  843. end;
  844. handledprotocols.free;
  845. end
  846. else
  847. internalerror(2009091801);
  848. end
  849. end;
  850. end;
  851. {*****************************************************************************
  852. TVMTWriter
  853. *****************************************************************************}
  854. constructor TVMTWriter.create(c:tobjectdef);
  855. begin
  856. inherited Create;
  857. _Class:=c;
  858. end;
  859. {**************************************
  860. Message Tables
  861. **************************************}
  862. procedure TVMTWriter.disposeprocdeftree(p : pprocdeftree);
  863. begin
  864. if assigned(p^.l) then
  865. disposeprocdeftree(p^.l);
  866. if assigned(p^.r) then
  867. disposeprocdeftree(p^.r);
  868. dispose(p);
  869. end;
  870. procedure TVMTWriter.insertint(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  871. begin
  872. if at=nil then
  873. begin
  874. at:=p;
  875. inc(count);
  876. end
  877. else
  878. begin
  879. if p^.data.messageinf.i<at^.data.messageinf.i then
  880. insertint(p,at^.l,count)
  881. else if p^.data.messageinf.i>at^.data.messageinf.i then
  882. insertint(p,at^.r,count)
  883. else
  884. Message1(parser_e_duplicate_message_label,tostr(p^.data.messageinf.i));
  885. end;
  886. end;
  887. procedure TVMTWriter.insertstr(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  888. var
  889. i : integer;
  890. begin
  891. if at=nil then
  892. begin
  893. at:=p;
  894. inc(count);
  895. end
  896. else
  897. begin
  898. i:=CompareStr(p^.data.messageinf.str^,at^.data.messageinf.str^);
  899. if i<0 then
  900. insertstr(p,at^.l,count)
  901. else if i>0 then
  902. insertstr(p,at^.r,count)
  903. else
  904. Message1(parser_e_duplicate_message_label,p^.data.messageinf.str^);
  905. end;
  906. end;
  907. procedure TVMTWriter.insertmsgint(p:TObject;arg:pointer);
  908. var
  909. i : longint;
  910. pd : Tprocdef;
  911. pt : pprocdeftree;
  912. begin
  913. if tsym(p).typ<>procsym then
  914. exit;
  915. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  916. begin
  917. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  918. if po_msgint in pd.procoptions then
  919. begin
  920. new(pt);
  921. pt^.data:=pd;
  922. pt^.l:=nil;
  923. pt^.r:=nil;
  924. insertint(pt,root,plongint(arg)^);
  925. end;
  926. end;
  927. end;
  928. procedure TVMTWriter.insertmsgstr(p:TObject;arg:pointer);
  929. var
  930. i : longint;
  931. pd : Tprocdef;
  932. pt : pprocdeftree;
  933. begin
  934. if tsym(p).typ<>procsym then
  935. exit;
  936. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  937. begin
  938. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  939. if po_msgstr in pd.procoptions then
  940. begin
  941. new(pt);
  942. pt^.data:=pd;
  943. pt^.l:=nil;
  944. pt^.r:=nil;
  945. insertstr(pt,root,plongint(arg)^);
  946. end;
  947. end;
  948. end;
  949. procedure TVMTWriter.writenames(list : TAsmList;p : pprocdeftree);
  950. var
  951. ca : pchar;
  952. len : byte;
  953. begin
  954. current_asmdata.getdatalabel(p^.nl);
  955. if assigned(p^.l) then
  956. writenames(list,p^.l);
  957. list.concat(cai_align.create(const_align(sizeof(pint))));
  958. list.concat(Tai_label.Create(p^.nl));
  959. len:=length(p^.data.messageinf.str^);
  960. list.concat(tai_const.create_8bit(len));
  961. getmem(ca,len+1);
  962. move(p^.data.messageinf.str^[1],ca^,len);
  963. ca[len]:=#0;
  964. list.concat(Tai_string.Create_pchar(ca,len));
  965. if assigned(p^.r) then
  966. writenames(list,p^.r);
  967. end;
  968. procedure TVMTWriter.writestrentry(list : TAsmList;p : pprocdeftree);
  969. begin
  970. if assigned(p^.l) then
  971. writestrentry(list,p^.l);
  972. { write name label }
  973. list.concat(cai_align.create(const_align(sizeof(pint))));
  974. list.concat(Tai_const.Create_sym(p^.nl));
  975. list.concat(cai_align.create(const_align(sizeof(pint))));
  976. list.concat(Tai_const.Createname(p^.data.mangledname,0));
  977. if assigned(p^.r) then
  978. writestrentry(list,p^.r);
  979. end;
  980. function TVMTWriter.genstrmsgtab(list : TAsmList) : tasmlabel;
  981. var
  982. count : longint;
  983. begin
  984. root:=nil;
  985. count:=0;
  986. { insert all message handlers into a tree, sorted by name }
  987. _class.symtable.SymList.ForEachCall(@insertmsgstr,@count);
  988. { write all names }
  989. if assigned(root) then
  990. writenames(list,root);
  991. { now start writing of the message string table }
  992. current_asmdata.getlabel(result,alt_data);
  993. list.concat(cai_align.create(const_align(sizeof(pint))));
  994. list.concat(Tai_label.Create(result));
  995. list.concat(cai_align.create(const_align(sizeof(longint))));
  996. list.concat(Tai_const.Create_32bit(count));
  997. list.concat(cai_align.create(const_align(sizeof(pint))));
  998. if assigned(root) then
  999. begin
  1000. writestrentry(list,root);
  1001. disposeprocdeftree(root);
  1002. end;
  1003. end;
  1004. procedure TVMTWriter.writeintentry(list : TAsmList;p : pprocdeftree);
  1005. begin
  1006. if assigned(p^.l) then
  1007. writeintentry(list,p^.l);
  1008. { write name label }
  1009. list.concat(cai_align.create(const_align(sizeof(longint))));
  1010. list.concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  1011. list.concat(cai_align.create(const_align(sizeof(pint))));
  1012. list.concat(Tai_const.Createname(p^.data.mangledname,0));
  1013. if assigned(p^.r) then
  1014. writeintentry(list,p^.r);
  1015. end;
  1016. function TVMTWriter.genintmsgtab(list : TAsmList) : tasmlabel;
  1017. var
  1018. r : tasmlabel;
  1019. count : longint;
  1020. begin
  1021. root:=nil;
  1022. count:=0;
  1023. { insert all message handlers into a tree, sorted by name }
  1024. _class.symtable.SymList.ForEachCall(@insertmsgint,@count);
  1025. { now start writing of the message string table }
  1026. current_asmdata.getlabel(r,alt_data);
  1027. list.concat(cai_align.create(const_align(sizeof(pint))));
  1028. list.concat(Tai_label.Create(r));
  1029. genintmsgtab:=r;
  1030. list.concat(cai_align.create(const_align(sizeof(longint))));
  1031. list.concat(Tai_const.Create_32bit(count));
  1032. list.concat(cai_align.create(const_align(sizeof(pint))));
  1033. if assigned(root) then
  1034. begin
  1035. writeintentry(list,root);
  1036. disposeprocdeftree(root);
  1037. end;
  1038. end;
  1039. {$ifdef WITHDMT}
  1040. {**************************************
  1041. DMT
  1042. **************************************}
  1043. procedure TVMTWriter.insertdmtentry(p:TObject;arg:pointer);
  1044. var
  1045. hp : tprocdef;
  1046. pt : pprocdeftree;
  1047. begin
  1048. if tsym(p).typ=procsym then
  1049. begin
  1050. hp:=tprocsym(p).definition;
  1051. while assigned(hp) do
  1052. begin
  1053. if (po_msgint in hp.procoptions) then
  1054. begin
  1055. new(pt);
  1056. pt^.p:=hp;
  1057. pt^.l:=nil;
  1058. pt^.r:=nil;
  1059. insertint(pt,root);
  1060. end;
  1061. hp:=hp.nextoverloaded;
  1062. end;
  1063. end;
  1064. end;
  1065. procedure TVMTWriter.writedmtindexentry(p : pprocdeftree);
  1066. begin
  1067. if assigned(p^.l) then
  1068. writedmtindexentry(p^.l);
  1069. al_globals.concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  1070. if assigned(p^.r) then
  1071. writedmtindexentry(p^.r);
  1072. end;
  1073. procedure TVMTWriter.writedmtaddressentry(p : pprocdeftree);
  1074. begin
  1075. if assigned(p^.l) then
  1076. writedmtaddressentry(p^.l);
  1077. al_globals.concat(Tai_const_symbol.Createname(p^.data.mangledname,0));
  1078. if assigned(p^.r) then
  1079. writedmtaddressentry(p^.r);
  1080. end;
  1081. function TVMTWriter.gendmt : tasmlabel;
  1082. var
  1083. r : tasmlabel;
  1084. begin
  1085. root:=nil;
  1086. count:=0;
  1087. gendmt:=nil;
  1088. { insert all message handlers into a tree, sorted by number }
  1089. _class.symtable.SymList.ForEachCall(insertdmtentry);
  1090. if count>0 then
  1091. begin
  1092. current_asmdata.getdatalabel(r);
  1093. gendmt:=r;
  1094. al_globals.concat(cai_align.create(const_align(sizeof(pint))));
  1095. al_globals.concat(Tai_label.Create(r));
  1096. { entries for caching }
  1097. al_globals.concat(Tai_const.Create_ptr(0));
  1098. al_globals.concat(Tai_const.Create_ptr(0));
  1099. al_globals.concat(Tai_const.Create_32bit(count));
  1100. if assigned(root) then
  1101. begin
  1102. writedmtindexentry(root);
  1103. writedmtaddressentry(root);
  1104. disposeprocdeftree(root);
  1105. end;
  1106. end;
  1107. end;
  1108. {$endif WITHDMT}
  1109. {**************************************
  1110. Published Methods
  1111. **************************************}
  1112. procedure TVMTWriter.do_count_published_methods(p:TObject;arg:pointer);
  1113. var
  1114. i : longint;
  1115. pd : tprocdef;
  1116. begin
  1117. if (tsym(p).typ<>procsym) then
  1118. exit;
  1119. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  1120. begin
  1121. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  1122. if (pd.procsym=tsym(p)) and
  1123. (pd.visibility=vis_published) then
  1124. inc(plongint(arg)^);
  1125. end;
  1126. end;
  1127. procedure TVMTWriter.do_gen_published_methods(p:TObject;arg:pointer);
  1128. var
  1129. i : longint;
  1130. l : tasmlabel;
  1131. pd : tprocdef;
  1132. lists: ^TAsmList absolute arg;
  1133. begin
  1134. if (tsym(p).typ<>procsym) then
  1135. exit;
  1136. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  1137. begin
  1138. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  1139. if (pd.procsym=tsym(p)) and
  1140. (pd.visibility=vis_published) then
  1141. begin
  1142. current_asmdata.getlabel(l,alt_data);
  1143. lists[1].concat(cai_align.Create(const_align(sizeof(pint))));
  1144. lists[1].concat(Tai_label.Create(l));
  1145. lists[1].concat(Tai_const.Create_8bit(length(tsym(p).realname)));
  1146. lists[1].concat(Tai_string.Create(tsym(p).realname));
  1147. lists[0].concat(Tai_const.Create_sym(l));
  1148. if po_abstractmethod in pd.procoptions then
  1149. lists[0].concat(Tai_const.Create_sym(nil))
  1150. else
  1151. lists[0].concat(Tai_const.Createname(pd.mangledname,0));
  1152. end;
  1153. end;
  1154. end;
  1155. function TVMTWriter.genpublishedmethodstable(list : TAsmList) : tasmlabel;
  1156. var
  1157. l : tasmlabel;
  1158. count : longint;
  1159. lists : array[0..1] of TAsmList;
  1160. begin
  1161. count:=0;
  1162. _class.symtable.SymList.ForEachCall(@do_count_published_methods,@count);
  1163. if count>0 then
  1164. begin
  1165. lists[0]:=list;
  1166. lists[1]:=TAsmList.Create;
  1167. current_asmdata.getlabel(l,alt_data);
  1168. list.concat(cai_align.create(const_align(sizeof(pint))));
  1169. list.concat(Tai_label.Create(l));
  1170. list.concat(Tai_const.Create_32bit(count));
  1171. _class.symtable.SymList.ForEachCall(@do_gen_published_methods,@lists);
  1172. list.concatlist(lists[1]);
  1173. lists[1].Free;
  1174. genpublishedmethodstable:=l;
  1175. end
  1176. else
  1177. genpublishedmethodstable:=nil;
  1178. end;
  1179. function TVMTWriter.generate_field_table(list : TAsmList) : tasmlabel;
  1180. var
  1181. i : longint;
  1182. sym : tsym;
  1183. fieldtable,
  1184. classtable : tasmlabel;
  1185. classindex,
  1186. fieldcount : longint;
  1187. classtablelist : TFPList;
  1188. begin
  1189. classtablelist:=TFPList.Create;
  1190. { retrieve field info fields }
  1191. fieldcount:=0;
  1192. for i:=0 to _class.symtable.SymList.Count-1 do
  1193. begin
  1194. sym:=tsym(_class.symtable.SymList[i]);
  1195. if (sym.typ=fieldvarsym) and
  1196. (sym.visibility=vis_published) then
  1197. begin
  1198. if tfieldvarsym(sym).vardef.typ<>objectdef then
  1199. internalerror(200611032);
  1200. classindex:=classtablelist.IndexOf(tfieldvarsym(sym).vardef);
  1201. if classindex=-1 then
  1202. classtablelist.Add(tfieldvarsym(sym).vardef);
  1203. inc(fieldcount);
  1204. end;
  1205. end;
  1206. if fieldcount>0 then
  1207. begin
  1208. current_asmdata.getlabel(fieldtable,alt_data);
  1209. current_asmdata.getlabel(classtable,alt_data);
  1210. list.concat(cai_align.create(const_align(sizeof(pint))));
  1211. { write fields }
  1212. list.concat(Tai_label.Create(fieldtable));
  1213. list.concat(Tai_const.Create_16bit(fieldcount));
  1214. if (tf_requires_proper_alignment in target_info.flags) then
  1215. list.concat(cai_align.Create(sizeof(TConstPtrUInt)));
  1216. list.concat(Tai_const.Create_sym(classtable));
  1217. for i:=0 to _class.symtable.SymList.Count-1 do
  1218. begin
  1219. sym:=tsym(_class.symtable.SymList[i]);
  1220. if (sym.typ=fieldvarsym) and
  1221. (sym.visibility=vis_published) then
  1222. begin
  1223. if (tf_requires_proper_alignment in target_info.flags) then
  1224. list.concat(cai_align.Create(sizeof(pint)));
  1225. list.concat(Tai_const.Create_pint(tfieldvarsym(sym).fieldoffset));
  1226. classindex:=classtablelist.IndexOf(tfieldvarsym(sym).vardef);
  1227. if classindex=-1 then
  1228. internalerror(200611033);
  1229. list.concat(Tai_const.Create_16bit(classindex+1));
  1230. list.concat(Tai_const.Create_8bit(length(tfieldvarsym(sym).realname)));
  1231. list.concat(Tai_string.Create(tfieldvarsym(sym).realname));
  1232. end;
  1233. end;
  1234. { generate the class table }
  1235. list.concat(cai_align.create(const_align(sizeof(pint))));
  1236. list.concat(Tai_label.Create(classtable));
  1237. list.concat(Tai_const.Create_16bit(classtablelist.count));
  1238. if (tf_requires_proper_alignment in target_info.flags) then
  1239. list.concat(cai_align.Create(sizeof(TConstPtrUInt)));
  1240. for i:=0 to classtablelist.Count-1 do
  1241. list.concat(Tai_const.Createname(tobjectdef(classtablelist[i]).vmt_mangledname,0));
  1242. result:=fieldtable;
  1243. end
  1244. else
  1245. result:=nil;
  1246. classtablelist.free;
  1247. end;
  1248. {**************************************
  1249. Interface tables
  1250. **************************************}
  1251. function TVMTWriter.intf_get_vtbl_name(AImplIntf:TImplementedInterface): string;
  1252. begin
  1253. result:=make_mangledname('VTBL',_class.owner,_class.objname^+'_$_'+AImplIntf.IntfDef.objname^);
  1254. end;
  1255. procedure TVMTWriter.intf_create_vtbl(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  1256. var
  1257. pd : tprocdef;
  1258. vtblstr,
  1259. hs : string;
  1260. i : longint;
  1261. begin
  1262. vtblstr:=intf_get_vtbl_name(AImplIntf);
  1263. rawdata.concat(cai_align.create(const_align(sizeof(pint))));
  1264. rawdata.concat(tai_symbol.createname(vtblstr,AT_DATA,0));
  1265. if assigned(AImplIntf.procdefs) then
  1266. begin
  1267. for i:=0 to AImplIntf.procdefs.count-1 do
  1268. begin
  1269. pd:=tprocdef(AImplIntf.procdefs[i]);
  1270. hs:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+AImplIntf.IntfDef.objname^+'_$_'+
  1271. tostr(i)+'_$_'+pd.mangledname);
  1272. { create reference }
  1273. rawdata.concat(Tai_const.Createname(hs,0));
  1274. end;
  1275. end;
  1276. rawdata.concat(tai_symbol_end.createname(vtblstr));
  1277. end;
  1278. procedure TVMTWriter.intf_gen_intf_ref(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  1279. var
  1280. pd: tprocdef;
  1281. begin
  1282. { GUID (or nil for Corba interfaces) }
  1283. if AImplIntf.IntfDef.objecttype in [odt_interfacecom] then
  1284. rawdata.concat(Tai_const.CreateName(
  1285. make_mangledname('IID',AImplIntf.IntfDef.owner,AImplIntf.IntfDef.objname^),0))
  1286. else
  1287. rawdata.concat(Tai_const.Create_sym(nil));
  1288. { VTable }
  1289. rawdata.concat(Tai_const.Createname(intf_get_vtbl_name(AImplIntf.VtblImplIntf),0));
  1290. { IOffset field }
  1291. case AImplIntf.VtblImplIntf.IType of
  1292. etFieldValue, etFieldValueClass,
  1293. etStandard:
  1294. rawdata.concat(Tai_const.Create_pint(AImplIntf.VtblImplIntf.IOffset));
  1295. etStaticMethodResult, etStaticMethodClass:
  1296. rawdata.concat(Tai_const.Createname(
  1297. tprocdef(tpropertysym(AImplIntf.ImplementsGetter).propaccesslist[palt_read].procdef).mangledname,
  1298. 0
  1299. ));
  1300. etVirtualMethodResult, etVirtualMethodClass:
  1301. begin
  1302. pd := tprocdef(tpropertysym(AImplIntf.ImplementsGetter).propaccesslist[palt_read].procdef);
  1303. rawdata.concat(Tai_const.Create_pint(tobjectdef(pd.struct).vmtmethodoffset(pd.extnumber)));
  1304. end;
  1305. else
  1306. internalerror(200802162);
  1307. end;
  1308. { IIDStr }
  1309. rawdata.concat(Tai_const.CreateName(
  1310. make_mangledname('IIDSTR',AImplIntf.IntfDef.owner,AImplIntf.IntfDef.objname^),0));
  1311. { IType }
  1312. rawdata.concat(Tai_const.Create_pint(aint(AImplIntf.VtblImplIntf.IType)));
  1313. end;
  1314. function TVMTWriter.intf_write_table(list : TAsmList):TAsmLabel;
  1315. var
  1316. i : longint;
  1317. ImplIntf : TImplementedInterface;
  1318. begin
  1319. current_asmdata.getlabel(result,alt_data);
  1320. list.concat(cai_align.create(const_align(sizeof(pint))));
  1321. list.concat(Tai_label.Create(result));
  1322. list.concat(Tai_const.Create_pint(_class.ImplementedInterfaces.count));
  1323. { Write vtbl references }
  1324. for i:=0 to _class.ImplementedInterfaces.count-1 do
  1325. begin
  1326. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  1327. intf_gen_intf_ref(list,ImplIntf);
  1328. end;
  1329. { Write vtbls }
  1330. for i:=0 to _class.ImplementedInterfaces.count-1 do
  1331. begin
  1332. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  1333. if ImplIntf.VtblImplIntf=ImplIntf then
  1334. intf_create_vtbl(list,ImplIntf);
  1335. end;
  1336. end;
  1337. { Write interface identifiers to the data section }
  1338. procedure TVMTWriter.writeinterfaceids;
  1339. var
  1340. i : longint;
  1341. s : string;
  1342. begin
  1343. if assigned(_class.iidguid) then
  1344. begin
  1345. s:=make_mangledname('IID',_class.owner,_class.objname^);
  1346. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1347. new_section(current_asmdata.asmlists[al_globals],sec_rodata,s,const_align(sizeof(pint)));
  1348. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  1349. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_32bit(longint(_class.iidguid^.D1)));
  1350. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_16bit(_class.iidguid^.D2));
  1351. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_16bit(_class.iidguid^.D3));
  1352. for i:=Low(_class.iidguid^.D4) to High(_class.iidguid^.D4) do
  1353. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_8bit(_class.iidguid^.D4[i]));
  1354. end;
  1355. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1356. s:=make_mangledname('IIDSTR',_class.owner,_class.objname^);
  1357. new_section(current_asmdata.asmlists[al_globals],sec_rodata,s,sizeof(pint));
  1358. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  1359. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_8bit(length(_class.iidstr^)));
  1360. current_asmdata.asmlists[al_globals].concat(Tai_string.Create(_class.iidstr^));
  1361. end;
  1362. procedure TVMTWriter.writevirtualmethods(List:TAsmList);
  1363. var
  1364. vmtpd : tprocdef;
  1365. vmtentry : pvmtentry;
  1366. i : longint;
  1367. procname : string;
  1368. {$ifdef vtentry}
  1369. hs : string;
  1370. {$endif vtentry}
  1371. begin
  1372. if not assigned(_class.VMTEntries) then
  1373. exit;
  1374. for i:=0 to _class.VMTEntries.Count-1 do
  1375. begin
  1376. vmtentry:=pvmtentry(_class.vmtentries[i]);
  1377. vmtpd:=vmtentry^.procdef;
  1378. { safety checks }
  1379. if not(po_virtualmethod in vmtpd.procoptions) then
  1380. internalerror(200611082);
  1381. if vmtpd.extnumber<>i then
  1382. internalerror(200611083);
  1383. if (po_abstractmethod in vmtpd.procoptions) then
  1384. procname:='FPC_ABSTRACTERROR'
  1385. else if not wpoinfomanager.optimized_name_for_vmt(_class,vmtpd,procname) then
  1386. procname:=vmtpd.mangledname;
  1387. List.concat(Tai_const.createname(procname,0));
  1388. {$ifdef vtentry}
  1389. hs:='VTENTRY'+'_'+_class.vmt_mangledname+'$$'+tostr(_class.vmtmethodoffset(i) div sizeof(pint));
  1390. current_asmdata.asmlists[al_globals].concat(tai_symbol.CreateName(hs,AT_DATA,0));
  1391. {$endif vtentry}
  1392. end;
  1393. end;
  1394. procedure TVMTWriter.writevmt;
  1395. var
  1396. methodnametable,intmessagetable,
  1397. strmessagetable,classnamelabel,
  1398. fieldtablelabel : tasmlabel;
  1399. hs: string;
  1400. {$ifdef WITHDMT}
  1401. dmtlabel : tasmlabel;
  1402. {$endif WITHDMT}
  1403. interfacetable : tasmlabel;
  1404. templist : TAsmList;
  1405. begin
  1406. {$ifdef WITHDMT}
  1407. dmtlabel:=gendmt;
  1408. {$endif WITHDMT}
  1409. templist:=TAsmList.Create;
  1410. { write tables for classes, this must be done before the actual
  1411. class is written, because we need the labels defined }
  1412. if is_class(_class) then
  1413. begin
  1414. { write class name }
  1415. current_asmdata.getlabel(classnamelabel,alt_data);
  1416. templist.concat(cai_align.create(const_align(sizeof(pint))));
  1417. templist.concat(Tai_label.Create(classnamelabel));
  1418. hs:=_class.RttiName;
  1419. templist.concat(Tai_const.Create_8bit(length(hs)));
  1420. templist.concat(Tai_string.Create(hs));
  1421. { interface table }
  1422. if _class.ImplementedInterfaces.count>0 then
  1423. interfacetable:=intf_write_table(templist);
  1424. methodnametable:=genpublishedmethodstable(templist);
  1425. fieldtablelabel:=generate_field_table(templist);
  1426. { generate message and dynamic tables }
  1427. if (oo_has_msgstr in _class.objectoptions) then
  1428. strmessagetable:=genstrmsgtab(templist);
  1429. if (oo_has_msgint in _class.objectoptions) then
  1430. intmessagetable:=genintmsgtab(templist);
  1431. end;
  1432. { write debug info }
  1433. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1434. new_section(current_asmdata.asmlists[al_globals],sec_rodata,_class.vmt_mangledname,const_align(sizeof(pint)));
  1435. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(_class.vmt_mangledname,AT_DATA,0));
  1436. { determine the size with symtable.datasize, because }
  1437. { size gives back 4 for classes }
  1438. current_asmdata.asmlists[al_globals].concat(Tai_const.Create(aitconst_ptr,tObjectSymtable(_class.symtable).datasize));
  1439. current_asmdata.asmlists[al_globals].concat(Tai_const.Create(aitconst_ptr,-int64(tObjectSymtable(_class.symtable).datasize)));
  1440. {$ifdef WITHDMT}
  1441. if _class.classtype=ct_object then
  1442. begin
  1443. if assigned(dmtlabel) then
  1444. current_asmdata.asmlists[al_globals].concat(Tai_const_symbol.Create(dmtlabel)))
  1445. else
  1446. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_ptr(0));
  1447. end;
  1448. {$endif WITHDMT}
  1449. { write pointer to parent VMT, this isn't implemented in TP }
  1450. { but this is not used in FPC ? (PM) }
  1451. { it's not used yet, but the delphi-operators as and is need it (FK) }
  1452. { it is not written for parents that don't have any vmt !! }
  1453. if assigned(_class.childof) and
  1454. (oo_has_vmt in _class.childof.objectoptions) then
  1455. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(_class.childof.vmt_mangledname,0))
  1456. else
  1457. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1458. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  1459. if is_class(_class) then
  1460. begin
  1461. { pointer to class name string }
  1462. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(classnamelabel));
  1463. { pointer to dynamic table or nil }
  1464. if (oo_has_msgint in _class.objectoptions) then
  1465. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(intmessagetable))
  1466. else
  1467. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1468. { pointer to method table or nil }
  1469. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(methodnametable));
  1470. { pointer to field table }
  1471. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(fieldtablelabel));
  1472. { pointer to type info of published section }
  1473. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(RTTIWriter.get_rtti_label(_class,fullrtti)));
  1474. { inittable for con-/destruction }
  1475. if _class.members_need_inittable then
  1476. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(RTTIWriter.get_rtti_label(_class,initrtti)))
  1477. else
  1478. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1479. { auto table }
  1480. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1481. { interface table }
  1482. if _class.ImplementedInterfaces.count>0 then
  1483. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(interfacetable))
  1484. else if _class.implements_any_interfaces then
  1485. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil))
  1486. else
  1487. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(current_asmdata.RefAsmSymbol('FPC_EMPTYINTF')));
  1488. { table for string messages }
  1489. if (oo_has_msgstr in _class.objectoptions) then
  1490. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(strmessagetable))
  1491. else
  1492. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1493. end;
  1494. { write virtual methods }
  1495. writevirtualmethods(current_asmdata.asmlists[al_globals]);
  1496. current_asmdata.asmlists[al_globals].concat(Tai_const.create(aitconst_ptr,0));
  1497. { write the size of the VMT }
  1498. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname(_class.vmt_mangledname));
  1499. {$ifdef vtentry}
  1500. { write vtinherit symbol to notify the linker of the class inheritance tree }
  1501. hs:='VTINHERIT'+'_'+_class.vmt_mangledname+'$$';
  1502. if assigned(_class.childof) then
  1503. hs:=hs+_class.childof.vmt_mangledname
  1504. else
  1505. hs:=hs+_class.vmt_mangledname;
  1506. current_asmdata.asmlists[al_globals].concat(tai_symbol.CreateName(hs,AT_DATA,0));
  1507. {$endif vtentry}
  1508. if is_class(_class) then
  1509. current_asmdata.asmlists[al_globals].concatlist(templist);
  1510. templist.Free;
  1511. end;
  1512. end.