nobj.pas 61 KB

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