nobj.pas 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  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. destructor destroy;override;
  44. procedure generate_vmt;
  45. procedure build_interface_mappings;
  46. end;
  47. type
  48. pprocdeftree = ^tprocdeftree;
  49. tprocdeftree = record
  50. data : tprocdef;
  51. nl : tasmlabel;
  52. l,r : pprocdeftree;
  53. end;
  54. TVMTWriter=class
  55. private
  56. _Class : tobjectdef;
  57. { message tables }
  58. root : pprocdeftree;
  59. procedure disposeprocdeftree(p : pprocdeftree);
  60. procedure insertmsgint(p:TObject;arg:pointer);
  61. procedure insertmsgstr(p:TObject;arg:pointer);
  62. procedure insertint(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  63. procedure insertstr(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  64. procedure writenames(p : pprocdeftree);
  65. procedure writeintentry(p : pprocdeftree);
  66. procedure writestrentry(p : pprocdeftree);
  67. {$ifdef WITHDMT}
  68. { dmt }
  69. procedure insertdmtentry(p:TObject;arg:pointer);
  70. procedure writedmtindexentry(p : pprocdeftree);
  71. procedure writedmtaddressentry(p : pprocdeftree);
  72. {$endif}
  73. { published methods }
  74. procedure do_count_published_methods(p:TObject;arg:pointer);
  75. procedure do_gen_published_methods(p:TObject;arg:pointer);
  76. { virtual methods }
  77. procedure writevirtualmethods(List:TAsmList);
  78. { interface tables }
  79. function intf_get_vtbl_name(AImplIntf:TImplementedInterface): string;
  80. procedure intf_create_vtbl(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  81. procedure intf_gen_intf_ref(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  82. function intf_write_table:TAsmLabel;
  83. { generates the message tables for a class }
  84. function genstrmsgtab : tasmlabel;
  85. function genintmsgtab : tasmlabel;
  86. function genpublishedmethodstable : tasmlabel;
  87. function generate_field_table : tasmlabel;
  88. {$ifdef WITHDMT}
  89. { generates a DMT for _class }
  90. function gendmt : tasmlabel;
  91. {$endif WITHDMT}
  92. public
  93. constructor create(c:tobjectdef);
  94. destructor destroy;override;
  95. { write the VMT to al_globals }
  96. procedure writevmt;
  97. procedure writeinterfaceids;
  98. end;
  99. implementation
  100. uses
  101. SysUtils,
  102. globals,verbose,systems,
  103. node,
  104. symbase,symtable,symconst,symtype,defcmp,
  105. dbgbase,
  106. ncgrtti,
  107. wpobase
  108. ;
  109. {*****************************************************************************
  110. TVMTBuilder
  111. *****************************************************************************}
  112. constructor TVMTBuilder.create(c:tobjectdef);
  113. begin
  114. inherited Create;
  115. _Class:=c;
  116. end;
  117. destructor TVMTBuilder.destroy;
  118. begin
  119. end;
  120. procedure TVMTBuilder.add_new_vmt_entry(pd:tprocdef; allowoverridingmethod: boolean);
  121. var
  122. i : longint;
  123. vmtentry : pvmtentry;
  124. vmtpd : tprocdef;
  125. begin
  126. { new entry is needed, override was not possible }
  127. { Allowed when overriding a category method for a parent class in a
  128. descendent Objective-C class }
  129. if not allowoverridingmethod and
  130. (po_overridingmethod in pd.procoptions) then
  131. MessagePos1(pd.fileinfo,parser_e_nothing_to_be_overridden,pd.fullprocname(false));
  132. { check that all methods have overload directive }
  133. if not(m_fpc in current_settings.modeswitches) then
  134. begin
  135. for i:=0 to _class.vmtentries.count-1 do
  136. begin
  137. vmtentry:=pvmtentry(_class.vmtentries[i]);
  138. vmtpd:=tprocdef(vmtentry^.procdef);
  139. if (vmtpd.procsym=pd.procsym) and
  140. (not(po_overload in pd.procoptions) or
  141. not(po_overload in vmtpd.procoptions)) then
  142. begin
  143. MessagePos1(pd.fileinfo,parser_e_no_overload_for_all_procs,pd.procsym.realname);
  144. { recover }
  145. include(vmtpd.procoptions,po_overload);
  146. include(pd.procoptions,po_overload);
  147. end;
  148. end;
  149. end;
  150. { Register virtual method and give it a number }
  151. if (po_virtualmethod in pd.procoptions) then
  152. begin
  153. { store vmt entry number in procdef }
  154. if (pd.extnumber<>$ffff) and
  155. (pd.extnumber<>_class.VMTEntries.Count) then
  156. internalerror(200810283);
  157. pd.extnumber:=_class.VMTEntries.Count;
  158. new(vmtentry);
  159. vmtentry^.procdef:=pd;
  160. vmtentry^.procdefderef.reset;
  161. vmtentry^.visibility:=pd.visibility;
  162. _class.VMTEntries.Add(vmtentry);
  163. end;
  164. end;
  165. function TVMTBuilder.check_msg_str(vmtpd, pd: tprocdef): boolean;
  166. begin
  167. result:=true;
  168. if not(is_objc_class_or_protocol(_class)) then
  169. begin
  170. { the only requirement for normal methods is that both either
  171. have a message string or not (the value is irrelevant) }
  172. if ((pd.procoptions * [po_msgstr]) <> (vmtpd.procoptions * [po_msgstr])) then
  173. begin
  174. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  175. tprocsym(vmtpd.procsym).write_parameter_lists(pd);
  176. result:=false;
  177. end
  178. end
  179. else
  180. begin
  181. { the compiler should have ensured that the protocol or parent
  182. class method has a message name specified }
  183. if not(po_msgstr in vmtpd.procoptions) then
  184. internalerror(2009070601);
  185. if not(po_msgstr in pd.procoptions) then
  186. begin
  187. { copy the protocol's/parent class' message name to the one in
  188. the class if none has been specified there }
  189. include(pd.procoptions,po_msgstr);
  190. pd.messageinf.str:=stringdup(vmtpd.messageinf.str^);
  191. end
  192. else
  193. begin
  194. { if both have a message name, make sure they are equal }
  195. if (vmtpd.messageinf.str^<>pd.messageinf.str^) then
  196. begin
  197. MessagePos2(pd.fileinfo,parser_e_objc_message_name_changed,vmtpd.messageinf.str^,pd.messageinf.str^);
  198. result:=false;
  199. end;
  200. end;
  201. end;
  202. end;
  203. function TVMTBuilder.is_new_vmt_entry(pd:tprocdef; out overridesclasshelper: boolean):boolean;
  204. const
  205. po_comp = [po_classmethod,po_virtualmethod,po_staticmethod,po_interrupt,po_iocheck,po_msgint,
  206. po_exports,po_varargs,po_explicitparaloc,po_nostackframe];
  207. var
  208. i : longint;
  209. hasequalpara,
  210. hasoverloads,
  211. pdoverload : boolean;
  212. srsym : tsym;
  213. st : tsymtable;
  214. // returns true if we can stop checking, false if we have to continue
  215. function found_entry(var vmtpd: tprocdef; var vmtentryvis: tvisibility; updatevalues: boolean): boolean;
  216. begin
  217. result:=false;
  218. { ignore hidden entries (e.g. virtual overridden by a static) that are not visible anymore }
  219. if vmtentryvis=vis_hidden then
  220. exit;
  221. { ignore different names }
  222. if vmtpd.procsym.name<>pd.procsym.name then
  223. exit;
  224. { hide private methods that are not visible anymore. For this check we
  225. must override the visibility with the highest value in the override chain.
  226. This is required for case (see tw3292) with protected-private-protected where the
  227. same vmtentry is used (PFV) }
  228. if not is_visible_for_object(vmtpd.owner,vmtentryvis,_class) then
  229. exit;
  230. { inherit overload }
  231. if (po_overload in vmtpd.procoptions) then
  232. begin
  233. include(pd.procoptions,po_overload);
  234. pdoverload:=true;
  235. end;
  236. { compare parameter types only, no specifiers yet }
  237. hasequalpara:=(compare_paras(vmtpd.paras,pd.paras,cp_none,[cpo_ignoreuniv])>=te_equal);
  238. { check that we are not trying to override a final method }
  239. if (po_finalmethod in vmtpd.procoptions) and
  240. hasequalpara and (po_overridingmethod in pd.procoptions) and is_class(_class) then
  241. MessagePos1(pd.fileinfo,parser_e_final_can_no_be_overridden,pd.fullprocname(false))
  242. else
  243. { old definition has virtual
  244. new definition has no virtual or override }
  245. if (po_virtualmethod in vmtpd.procoptions) and
  246. (
  247. not(po_virtualmethod in pd.procoptions) or
  248. (
  249. { new one does not have reintroduce in case of an objccategory }
  250. (is_objccategory(_class) and not(po_reintroduce in pd.procoptions)) or
  251. { new one does not have override in case of objpas/objc class/intf/proto }
  252. (is_class_or_interface_or_objc(_class) and not is_objccategory(_class) and not(po_overridingmethod in pd.procoptions))
  253. )
  254. ) then
  255. begin
  256. if (
  257. not(pdoverload or hasoverloads) or
  258. hasequalpara
  259. ) then
  260. begin
  261. if not(po_reintroduce in pd.procoptions) then
  262. if not(is_objc_class_or_protocol(_class)) then
  263. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false))
  264. else
  265. begin
  266. { In Objective-C, you cannot create a new VMT entry to
  267. start a new inheritance tree. We therefore give an
  268. error when the class is implemented in Pascal, to
  269. avoid confusion due to things working differently
  270. with Object Pascal classes.
  271. In case of external classes, we only give a hint,
  272. because requiring override everywhere may make
  273. automated header translation tools too complex. }
  274. if not(oo_is_external in _class.objectoptions) then
  275. if not is_objccategory(_class) then
  276. MessagePos1(pd.fileinfo,parser_e_must_use_override_objc,FullTypeName(tdef(vmtpd.owner.defowner),nil))
  277. else
  278. MessagePos1(pd.fileinfo,parser_e_must_use_reintroduce_objc,FullTypeName(tdef(vmtpd.owner.defowner),nil))
  279. { there may be a lot of these in auto-translated
  280. heaeders, so only calculate the fulltypename if
  281. the hint will be shown }
  282. else if CheckVerbosity(V_Hint) then
  283. if not is_objccategory(_class) then
  284. MessagePos1(pd.fileinfo,parser_h_should_use_override_objc,FullTypeName(tdef(vmtpd.owner.defowner),nil))
  285. else
  286. MessagePos1(pd.fileinfo,parser_h_should_use_reintroduce_objc,FullTypeName(tdef(vmtpd.owner.defowner),nil));
  287. { no new entry, but copy the message name if any from
  288. the procdef in the parent class }
  289. check_msg_str(vmtpd,pd);
  290. result:=true;
  291. exit;
  292. end;
  293. { disable/hide old VMT entry }
  294. if updatevalues then
  295. vmtentryvis:=vis_hidden;
  296. end;
  297. end
  298. { both are virtual? }
  299. else if (po_virtualmethod in pd.procoptions) and
  300. (po_virtualmethod in vmtpd.procoptions) then
  301. begin
  302. { same parameter and return types (parameter specifiers will be checked below) }
  303. if hasequalpara and
  304. compatible_childmethod_resultdef(vmtpd.returndef,pd.returndef) then
  305. begin
  306. { inherite calling convention when it was explicit and the
  307. current definition has none explicit set }
  308. if (po_hascallingconvention in vmtpd.procoptions) and
  309. not(po_hascallingconvention in pd.procoptions) then
  310. begin
  311. pd.proccalloption:=vmtpd.proccalloption;
  312. include(pd.procoptions,po_hascallingconvention);
  313. end;
  314. { All parameter specifiers and some procedure the flags have to match
  315. except abstract and override }
  316. if (compare_paras(vmtpd.paras,pd.paras,cp_all,[cpo_ignoreuniv])<te_equal) or
  317. (vmtpd.proccalloption<>pd.proccalloption) or
  318. (vmtpd.proctypeoption<>pd.proctypeoption) or
  319. ((vmtpd.procoptions*po_comp)<>(pd.procoptions*po_comp)) then
  320. begin
  321. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  322. tprocsym(vmtpd.procsym).write_parameter_lists(pd);
  323. end;
  324. check_msg_str(vmtpd,pd);
  325. { Give a note if the new visibility is lower. For a higher
  326. visibility update the vmt info }
  327. if vmtentryvis>pd.visibility then
  328. MessagePos4(pd.fileinfo,parser_n_ignore_lower_visibility,pd.fullprocname(false),
  329. visibilityname[pd.visibility],tobjectdef(vmtpd.owner.defowner).objrealname^,visibilityname[vmtentryvis])
  330. else if pd.visibility>vmtentryvis then
  331. begin
  332. if updatevalues then
  333. vmtentryvis:=pd.visibility;
  334. end;
  335. { override old virtual method in VMT }
  336. if updatevalues then
  337. begin
  338. if (vmtpd.extnumber<>i) then
  339. internalerror(200611084);
  340. pd.extnumber:=vmtpd.extnumber;
  341. vmtpd:=pd;
  342. end;
  343. result:=true;
  344. exit;
  345. end
  346. { different parameters }
  347. else
  348. begin
  349. { when we got an override directive then can search futher for
  350. the procedure to override.
  351. If we are starting a new virtual tree then hide the old tree }
  352. if not(po_overridingmethod in pd.procoptions) and
  353. not(pdoverload or hasoverloads) then
  354. begin
  355. if not(po_reintroduce in pd.procoptions) then
  356. begin
  357. if not is_object(_class) and
  358. not is_objc_class_or_protocol(_class) then
  359. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false))
  360. else
  361. { objects don't allow starting a new virtual tree
  362. and neither does Objective-C }
  363. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,vmtpd.fullprocname(false));
  364. end;
  365. { disable/hide old VMT entry }
  366. if updatevalues then
  367. vmtentryvis:=vis_hidden;
  368. end;
  369. end;
  370. end;
  371. end;
  372. function found_category_method(st: tsymtable): boolean;
  373. var
  374. entrycount: longint;
  375. cat: tobjectdef;
  376. vmtpd: tprocdef;
  377. vmtvis: tvisibility;
  378. begin
  379. result:=false;
  380. if is_objccategory(tdef(st.defowner)) then
  381. begin
  382. cat:=tobjectdef(st.defowner);
  383. { go through all of the category's methods to find the
  384. vmtentry corresponding to the procdef we are handling }
  385. for entrycount:=0 to cat.vmtentries.Count-1 do
  386. begin
  387. vmtpd:=pvmtentry(cat.vmtentries[entrycount])^.procdef;
  388. vmtvis:=pvmtentry(cat.vmtentries[entrycount])^.visibility;
  389. { don't change the vmtentry of the category }
  390. if found_entry(vmtpd,vmtvis,false) then
  391. begin
  392. result:=true;
  393. exit;
  394. end;
  395. end;
  396. end;
  397. end;
  398. begin
  399. result:=false;
  400. overridesclasshelper:=false;
  401. { Load other values for easier readability }
  402. hasoverloads:=(tprocsym(pd.procsym).ProcdefList.Count>1);
  403. pdoverload:=(po_overload in pd.procoptions);
  404. { compare with all stored definitions }
  405. for i:=0 to _class.vmtentries.Count-1 do
  406. begin
  407. if found_entry(pvmtentry(_class.vmtentries[i])^.procdef, pvmtentry(_class.vmtentries[i])^.visibility,true) then
  408. exit;
  409. end;
  410. { in case of Objective-C, also check the categories that apply to this
  411. class' *parent* for methods to override (don't allow class X to
  412. "override" a method added by a category to class X itself, since in
  413. that case the category method will in fact replace class X'
  414. "overriding" method }
  415. if is_objcclass(_class) and
  416. assigned(_class.childof) and
  417. search_class_helper(_class.childof,pd.procsym.name,srsym,st) then
  418. begin
  419. overridesclasshelper:=found_category_method(st);
  420. end;
  421. { No entry found, we need to create a new entry }
  422. result:=true;
  423. end;
  424. function TVMTBuilder.intf_search_procdef_by_name(proc: tprocdef;const name: string): tprocdef;
  425. const
  426. po_comp = [po_classmethod,po_staticmethod,po_interrupt,po_iocheck,po_msgint,
  427. po_exports,po_varargs,po_explicitparaloc,po_nostackframe];
  428. var
  429. implprocdef : Tprocdef;
  430. i: cardinal;
  431. hclass : tobjectdef;
  432. hashedid : THashedIDString;
  433. srsym : tsym;
  434. begin
  435. result:=nil;
  436. hashedid.id:=name;
  437. hclass:=_class;
  438. while assigned(hclass) do
  439. begin
  440. srsym:=tsym(hclass.symtable.FindWithHash(hashedid));
  441. if assigned(srsym) and
  442. (srsym.typ=procsym) then
  443. begin
  444. for i:=0 to Tprocsym(srsym).ProcdefList.Count-1 do
  445. begin
  446. implprocdef:=tprocdef(tprocsym(srsym).ProcdefList[i]);
  447. if (implprocdef.procsym=tprocsym(srsym)) and
  448. (compare_paras(proc.paras,implprocdef.paras,cp_all,[cpo_ignorehidden,cpo_comparedefaultvalue,cpo_ignoreuniv])>=te_equal) and
  449. (compare_defs(proc.returndef,implprocdef.returndef,nothingn)>=te_equal) and
  450. (proc.proccalloption=implprocdef.proccalloption) and
  451. (proc.proctypeoption=implprocdef.proctypeoption) and
  452. ((proc.procoptions*po_comp)=((implprocdef.procoptions+[po_virtualmethod])*po_comp)) and
  453. check_msg_str(proc,implprocdef) then
  454. begin
  455. result:=implprocdef;
  456. exit;
  457. end;
  458. end;
  459. end;
  460. hclass:=hclass.childof;
  461. end;
  462. end;
  463. procedure TVMTBuilder.intf_get_procdefs(ImplIntf:TImplementedInterface;IntfDef:TObjectDef);
  464. var
  465. i : longint;
  466. def : tdef;
  467. hs,
  468. prefix,
  469. mappedname: string;
  470. implprocdef: tprocdef;
  471. begin
  472. prefix:=ImplIntf.IntfDef.symtable.name^+'.';
  473. for i:=0 to IntfDef.symtable.DefList.Count-1 do
  474. begin
  475. def:=tdef(IntfDef.symtable.DefList[i]);
  476. if assigned(def) and
  477. (def.typ=procdef) then
  478. begin
  479. { Find implementing procdef
  480. 1. Check for mapped name
  481. 2. Use symbol name }
  482. implprocdef:=nil;
  483. hs:=prefix+tprocdef(def).procsym.name;
  484. mappedname:=ImplIntf.GetMapping(hs);
  485. if mappedname<>'' then
  486. implprocdef:=intf_search_procdef_by_name(tprocdef(def),mappedname);
  487. if not assigned(implprocdef) then
  488. implprocdef:=intf_search_procdef_by_name(tprocdef(def),tprocdef(def).procsym.name);
  489. { Add procdef to the implemented interface }
  490. if assigned(implprocdef) then
  491. begin
  492. if (tobjectdef(implprocdef.struct).objecttype<>odt_objcclass) then
  493. ImplIntf.AddImplProc(implprocdef)
  494. else
  495. begin
  496. { If no message name has been specified for the method
  497. in the objcclass, copy it from the protocol
  498. definition. }
  499. if not(po_msgstr in tprocdef(def).procoptions) then
  500. begin
  501. include(tprocdef(def).procoptions,po_msgstr);
  502. implprocdef.messageinf.str:=stringdup(tprocdef(def).messageinf.str^);
  503. end
  504. else
  505. begin
  506. { If a message name has been specified in the
  507. objcclass, it has to match the message name in the
  508. protocol definition. }
  509. if (implprocdef.messageinf.str^<>tprocdef(def).messageinf.str^) then
  510. MessagePos2(implprocdef.fileinfo,parser_e_objc_message_name_changed,tprocdef(def).messageinf.str^,implprocdef.messageinf.str^);
  511. end;
  512. end;
  513. end
  514. else
  515. if (ImplIntf.IType=etStandard) and
  516. not(po_optional in tprocdef(def).procoptions) then
  517. MessagePos1(_Class.typesym.fileinfo,sym_e_no_matching_implementation_found,tprocdef(def).fullprocname(false));
  518. end;
  519. end;
  520. end;
  521. procedure TVMTBuilder.intf_get_procdefs_recursive(ImplIntf:TImplementedInterface;IntfDef:TObjectDef);
  522. begin
  523. if assigned(IntfDef.childof) then
  524. intf_get_procdefs_recursive(ImplIntf,IntfDef.childof);
  525. intf_get_procdefs(ImplIntf,IntfDef);
  526. end;
  527. procedure TVMTBuilder.prot_get_procdefs_recursive(ImplProt:TImplementedInterface;ProtDef:TObjectDef);
  528. var
  529. i: longint;
  530. begin
  531. { don't check the same protocol twice }
  532. if handledprotocols.IndexOf(ProtDef)<>-1 then
  533. exit;
  534. handledprotocols.add(ProtDef);
  535. for i:=0 to ProtDef.ImplementedInterfaces.count-1 do
  536. prot_get_procdefs_recursive(ImplProt,TImplementedInterface(ProtDef.ImplementedInterfaces[i]).intfdef);
  537. intf_get_procdefs(ImplProt,ProtDef);
  538. end;
  539. procedure TVMTBuilder.intf_optimize_vtbls;
  540. type
  541. tcompintfentry = record
  542. weight: longint;
  543. compintf: longint;
  544. end;
  545. { Max 1000 interface in the class header interfaces it's enough imho }
  546. tcompintfs = array[0..1000] of tcompintfentry;
  547. pcompintfs = ^tcompintfs;
  548. tequals = array[0..1000] of longint;
  549. pequals = ^tequals;
  550. timpls = array[0..1000] of longint;
  551. pimpls = ^timpls;
  552. var
  553. aequals: pequals;
  554. compats: pcompintfs;
  555. impls: pimpls;
  556. ImplIntfCount,
  557. w,i,j,k: longint;
  558. ImplIntfI,
  559. ImplIntfJ : TImplementedInterface;
  560. cij: boolean;
  561. cji: boolean;
  562. begin
  563. ImplIntfCount:=_class.ImplementedInterfaces.count;
  564. if ImplIntfCount>=High(tequals) then
  565. Internalerror(200006135);
  566. getmem(compats,sizeof(tcompintfentry)*ImplIntfCount);
  567. getmem(aequals,sizeof(longint)*ImplIntfCount);
  568. getmem(impls,sizeof(longint)*ImplIntfCount);
  569. filldword(compats^,(sizeof(tcompintfentry) div sizeof(dword))*ImplIntfCount,dword(-1));
  570. filldword(aequals^,ImplIntfCount,dword(-1));
  571. filldword(impls^,ImplIntfCount,dword(-1));
  572. { ismergepossible is a containing relation
  573. meaning of ismergepossible(a,b,w) =
  574. if implementorfunction map of a is contained implementorfunction map of b
  575. imp(a,b) and imp(b,c) => imp(a,c) ; imp(a,b) and imp(b,a) => a == b
  576. }
  577. { the order is very important for correct allocation }
  578. for i:=0 to ImplIntfCount-1 do
  579. begin
  580. for j:=i+1 to ImplIntfCount-1 do
  581. begin
  582. ImplIntfI:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  583. ImplIntfJ:=TImplementedInterface(_class.ImplementedInterfaces[j]);
  584. cij:=ImplIntfI.IsImplMergePossible(ImplIntfJ,w);
  585. cji:=ImplIntfJ.IsImplMergePossible(ImplIntfI,w);
  586. if cij and cji then { i equal j }
  587. begin
  588. { get minimum index of equal }
  589. if aequals^[j]=-1 then
  590. aequals^[j]:=i;
  591. end
  592. else if cij then
  593. begin
  594. { get minimum index of maximum weight }
  595. if compats^[i].weight<w then
  596. begin
  597. compats^[i].weight:=w;
  598. compats^[i].compintf:=j;
  599. end;
  600. end
  601. else if cji then
  602. begin
  603. { get minimum index of maximum weight }
  604. if (compats^[j].weight<w) then
  605. begin
  606. compats^[j].weight:=w;
  607. compats^[j].compintf:=i;
  608. end;
  609. end;
  610. end;
  611. end;
  612. { Reset, no replacements by default }
  613. for i:=0 to ImplIntfCount-1 do
  614. impls^[i]:=i;
  615. { Replace vtbls when equal or compat, repeat
  616. until there are no replacements possible anymore. This is
  617. needed for the cases like:
  618. First loop: 2->3, 3->1
  619. Second loop: 2->1 (because 3 was replaced with 1)
  620. }
  621. repeat
  622. k:=0;
  623. for i:=0 to ImplIntfCount-1 do
  624. begin
  625. if compats^[impls^[i]].compintf<>-1 then
  626. impls^[i]:=compats^[impls^[i]].compintf
  627. else if aequals^[impls^[i]]<>-1 then
  628. impls^[i]:=aequals^[impls^[i]]
  629. else
  630. inc(k);
  631. end;
  632. until k=ImplIntfCount;
  633. { Update the VtblImplIntf }
  634. for i:=0 to ImplIntfCount-1 do
  635. begin
  636. ImplIntfI:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  637. ImplIntfI.VtblImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[impls^[i]]);
  638. end;
  639. freemem(compats);
  640. freemem(aequals);
  641. freemem(impls);
  642. end;
  643. procedure TVMTBuilder.intf_allocate_vtbls;
  644. var
  645. i : longint;
  646. ImplIntf : TImplementedInterface;
  647. begin
  648. { Allocation vtbl space }
  649. for i:=0 to _class.ImplementedInterfaces.count-1 do
  650. begin
  651. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  652. { if it implements itself and if it's not implemented by delegation }
  653. if (ImplIntf.VtblImplIntf=ImplIntf) and (ImplIntf.IType=etStandard) then
  654. begin
  655. { allocate a pointer in the object memory }
  656. with tObjectSymtable(_class.symtable) do
  657. begin
  658. datasize:=align(datasize,sizeof(pint));
  659. ImplIntf.Ioffset:=datasize;
  660. datasize:=datasize+sizeof(pint);
  661. end;
  662. end;
  663. end;
  664. { Update ioffset of current interface with the ioffset from
  665. the interface that is reused to implements this interface }
  666. for i:=0 to _class.ImplementedInterfaces.count-1 do
  667. begin
  668. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  669. if ImplIntf.VtblImplIntf<>ImplIntf then
  670. ImplIntf.IOffset:=ImplIntf.VtblImplIntf.IOffset;
  671. end;
  672. end;
  673. procedure TVMTBuilder.generate_vmt;
  674. var
  675. i : longint;
  676. def : tdef;
  677. old_current_structdef : tabstractrecorddef;
  678. overridesclasshelper : boolean;
  679. begin
  680. old_current_structdef:=current_structdef;
  681. current_structdef:=_class;
  682. _class.resetvmtentries;
  683. { inherit (copy) VMT from parent object }
  684. if assigned(_class.childof) then
  685. begin
  686. if not assigned(_class.childof.vmtentries) then
  687. internalerror(200810281);
  688. _class.copyvmtentries(_class.childof);
  689. end;
  690. { process all procdefs, we must process the defs to
  691. keep the same order as that is written in the source
  692. to be compatible with the indexes in the interface vtable (PFV) }
  693. for i:=0 to _class.symtable.DefList.Count-1 do
  694. begin
  695. def:=tdef(_class.symtable.DefList[i]);
  696. if def.typ=procdef then
  697. begin
  698. { VMT entry }
  699. if is_new_vmt_entry(tprocdef(def),overridesclasshelper) then
  700. add_new_vmt_entry(tprocdef(def),overridesclasshelper);
  701. end;
  702. end;
  703. build_interface_mappings;
  704. if assigned(_class.ImplementedInterfaces) and
  705. not(is_objc_class_or_protocol(_class)) then
  706. begin
  707. { Optimize interface tables to reuse wrappers }
  708. intf_optimize_vtbls;
  709. { Allocate interface tables }
  710. intf_allocate_vtbls;
  711. end;
  712. current_structdef:=old_current_structdef;
  713. end;
  714. procedure TVMTBuilder.build_interface_mappings;
  715. var
  716. ImplIntf : TImplementedInterface;
  717. i: longint;
  718. begin
  719. { Find Procdefs implementing the interfaces }
  720. if assigned(_class.ImplementedInterfaces) and
  721. (_class.objecttype<>odt_objcprotocol) then
  722. begin
  723. { Collect implementor functions into the tImplementedInterface.procdefs }
  724. case _class.objecttype of
  725. odt_class:
  726. begin
  727. for i:=0 to _class.ImplementedInterfaces.count-1 do
  728. begin
  729. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  730. intf_get_procdefs_recursive(ImplIntf,ImplIntf.IntfDef)
  731. end;
  732. end;
  733. odt_objcclass:
  734. begin
  735. { Object Pascal interfaces are afterwards optimized via the
  736. intf_optimize_vtbls() method, but we can't do this for
  737. protocols -> check for duplicates here already. }
  738. handledprotocols:=tfpobjectlist.create(false);
  739. for i:=0 to _class.ImplementedInterfaces.count-1 do
  740. begin
  741. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  742. prot_get_procdefs_recursive(ImplIntf,ImplIntf.IntfDef);
  743. end;
  744. handledprotocols.free;
  745. end
  746. else
  747. internalerror(2009091801);
  748. end
  749. end;
  750. end;
  751. {*****************************************************************************
  752. TVMTWriter
  753. *****************************************************************************}
  754. constructor TVMTWriter.create(c:tobjectdef);
  755. begin
  756. inherited Create;
  757. _Class:=c;
  758. end;
  759. destructor TVMTWriter.destroy;
  760. begin
  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(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(p^.l);
  860. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  861. current_asmdata.asmlists[al_globals].concat(Tai_label.Create(p^.nl));
  862. len:=length(p^.data.messageinf.str^);
  863. current_asmdata.asmlists[al_globals].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. current_asmdata.asmlists[al_globals].concat(Tai_string.Create_pchar(ca,len));
  868. if assigned(p^.r) then
  869. writenames(p^.r);
  870. end;
  871. procedure TVMTWriter.writestrentry(p : pprocdeftree);
  872. begin
  873. if assigned(p^.l) then
  874. writestrentry(p^.l);
  875. { write name label }
  876. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  877. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(p^.nl));
  878. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  879. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(p^.data.mangledname,0));
  880. if assigned(p^.r) then
  881. writestrentry(p^.r);
  882. end;
  883. function TVMTWriter.genstrmsgtab : 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(root);
  894. { now start writing of the message string table }
  895. current_asmdata.getdatalabel(result);
  896. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  897. current_asmdata.asmlists[al_globals].concat(Tai_label.Create(result));
  898. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(longint))));
  899. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_32bit(count));
  900. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  901. if assigned(root) then
  902. begin
  903. writestrentry(root);
  904. disposeprocdeftree(root);
  905. end;
  906. end;
  907. procedure TVMTWriter.writeintentry(p : pprocdeftree);
  908. begin
  909. if assigned(p^.l) then
  910. writeintentry(p^.l);
  911. { write name label }
  912. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(longint))));
  913. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  914. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  915. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(p^.data.mangledname,0));
  916. if assigned(p^.r) then
  917. writeintentry(p^.r);
  918. end;
  919. function TVMTWriter.genintmsgtab : 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.getdatalabel(r);
  930. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  931. current_asmdata.asmlists[al_globals].concat(Tai_label.Create(r));
  932. genintmsgtab:=r;
  933. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(longint))));
  934. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_32bit(count));
  935. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  936. if assigned(root) then
  937. begin
  938. writeintentry(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. begin
  1036. if (tsym(p).typ<>procsym) then
  1037. exit;
  1038. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  1039. begin
  1040. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  1041. if (pd.procsym=tsym(p)) and
  1042. (pd.visibility=vis_published) then
  1043. begin
  1044. current_asmdata.getdatalabel(l);
  1045. current_asmdata.asmlists[al_typedconsts].concat(cai_align.create(const_align(sizeof(pint))));
  1046. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(l));
  1047. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_8bit(length(tsym(p).realname)));
  1048. current_asmdata.asmlists[al_typedconsts].concat(Tai_string.Create(tsym(p).realname));
  1049. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(l));
  1050. if po_abstractmethod in pd.procoptions then
  1051. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil))
  1052. else
  1053. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(pd.mangledname,0));
  1054. end;
  1055. end;
  1056. end;
  1057. function TVMTWriter.genpublishedmethodstable : tasmlabel;
  1058. var
  1059. l : tasmlabel;
  1060. count : longint;
  1061. begin
  1062. count:=0;
  1063. _class.symtable.SymList.ForEachCall(@do_count_published_methods,@count);
  1064. if count>0 then
  1065. begin
  1066. current_asmdata.getdatalabel(l);
  1067. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  1068. current_asmdata.asmlists[al_globals].concat(Tai_label.Create(l));
  1069. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_32bit(count));
  1070. _class.symtable.SymList.ForEachCall(@do_gen_published_methods,nil);
  1071. genpublishedmethodstable:=l;
  1072. end
  1073. else
  1074. genpublishedmethodstable:=nil;
  1075. end;
  1076. function TVMTWriter.generate_field_table : tasmlabel;
  1077. var
  1078. i : longint;
  1079. sym : tsym;
  1080. fieldtable,
  1081. classtable : tasmlabel;
  1082. classindex,
  1083. fieldcount : longint;
  1084. classtablelist : TFPList;
  1085. begin
  1086. classtablelist:=TFPList.Create;
  1087. current_asmdata.getdatalabel(fieldtable);
  1088. current_asmdata.getdatalabel(classtable);
  1089. maybe_new_object_file(current_asmdata.asmlists[al_rtti]);
  1090. new_section(current_asmdata.asmlists[al_rtti],sec_rodata,classtable.name,const_align(sizeof(pint)));
  1091. { retrieve field info fields }
  1092. fieldcount:=0;
  1093. for i:=0 to _class.symtable.SymList.Count-1 do
  1094. begin
  1095. sym:=tsym(_class.symtable.SymList[i]);
  1096. if (sym.typ=fieldvarsym) and
  1097. (sym.visibility=vis_published) then
  1098. begin
  1099. if tfieldvarsym(sym).vardef.typ<>objectdef then
  1100. internalerror(200611032);
  1101. classindex:=classtablelist.IndexOf(tfieldvarsym(sym).vardef);
  1102. if classindex=-1 then
  1103. classtablelist.Add(tfieldvarsym(sym).vardef);
  1104. inc(fieldcount);
  1105. end;
  1106. end;
  1107. { write fields }
  1108. current_asmdata.asmlists[al_rtti].concat(Tai_label.Create(fieldtable));
  1109. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(fieldcount));
  1110. if (tf_requires_proper_alignment in target_info.flags) then
  1111. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  1112. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(classtable));
  1113. for i:=0 to _class.symtable.SymList.Count-1 do
  1114. begin
  1115. sym:=tsym(_class.symtable.SymList[i]);
  1116. if (sym.typ=fieldvarsym) and
  1117. (sym.visibility=vis_published) then
  1118. begin
  1119. if (tf_requires_proper_alignment in target_info.flags) then
  1120. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(pint)));
  1121. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_pint(tfieldvarsym(sym).fieldoffset));
  1122. classindex:=classtablelist.IndexOf(tfieldvarsym(sym).vardef);
  1123. if classindex=-1 then
  1124. internalerror(200611033);
  1125. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(classindex+1));
  1126. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(tfieldvarsym(sym).realname)));
  1127. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(tfieldvarsym(sym).realname));
  1128. end;
  1129. end;
  1130. { generate the class table }
  1131. current_asmdata.asmlists[al_rtti].concat(cai_align.create(const_align(sizeof(pint))));
  1132. current_asmdata.asmlists[al_rtti].concat(Tai_label.Create(classtable));
  1133. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(classtablelist.count));
  1134. if (tf_requires_proper_alignment in target_info.flags) then
  1135. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  1136. for i:=0 to classtablelist.Count-1 do
  1137. current_asmdata.asmlists[al_rtti].concat(Tai_const.Createname(tobjectdef(classtablelist[i]).vmt_mangledname,0));
  1138. classtablelist.free;
  1139. result:=fieldtable;
  1140. end;
  1141. {**************************************
  1142. Interface tables
  1143. **************************************}
  1144. function TVMTWriter.intf_get_vtbl_name(AImplIntf:TImplementedInterface): string;
  1145. begin
  1146. result:=make_mangledname('VTBL',_class.owner,_class.objname^+'_$_'+AImplIntf.IntfDef.objname^);
  1147. end;
  1148. procedure TVMTWriter.intf_create_vtbl(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  1149. var
  1150. pd : tprocdef;
  1151. vtblstr,
  1152. hs : string;
  1153. i : longint;
  1154. begin
  1155. vtblstr:=intf_get_vtbl_name(AImplIntf);
  1156. section_symbol_start(rawdata,vtblstr,AT_DATA,true,sec_data,const_align(sizeof(pint)));
  1157. if assigned(AImplIntf.procdefs) then
  1158. begin
  1159. for i:=0 to AImplIntf.procdefs.count-1 do
  1160. begin
  1161. pd:=tprocdef(AImplIntf.procdefs[i]);
  1162. hs:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+AImplIntf.IntfDef.objname^+'_$_'+
  1163. tostr(i)+'_$_'+pd.mangledname);
  1164. { create reference }
  1165. rawdata.concat(Tai_const.Createname(hs,0));
  1166. end;
  1167. end;
  1168. section_symbol_end(rawdata,vtblstr);
  1169. end;
  1170. procedure TVMTWriter.intf_gen_intf_ref(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  1171. var
  1172. iidlabel,
  1173. guidlabel : tasmlabel;
  1174. i: longint;
  1175. pd: tprocdef;
  1176. begin
  1177. { GUID }
  1178. if AImplIntf.IntfDef.objecttype in [odt_interfacecom] then
  1179. begin
  1180. { label for GUID }
  1181. current_asmdata.getdatalabel(guidlabel);
  1182. rawdata.concat(cai_align.create(const_align(sizeof(pint))));
  1183. rawdata.concat(Tai_label.Create(guidlabel));
  1184. with AImplIntf.IntfDef.iidguid^ do
  1185. begin
  1186. rawdata.concat(Tai_const.Create_32bit(longint(D1)));
  1187. rawdata.concat(Tai_const.Create_16bit(D2));
  1188. rawdata.concat(Tai_const.Create_16bit(D3));
  1189. for i:=Low(D4) to High(D4) do
  1190. rawdata.concat(Tai_const.Create_8bit(D4[i]));
  1191. end;
  1192. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(guidlabel));
  1193. end
  1194. else
  1195. begin
  1196. { nil for Corba interfaces }
  1197. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1198. end;
  1199. { VTable }
  1200. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(intf_get_vtbl_name(AImplIntf.VtblImplIntf),0));
  1201. { IOffset field }
  1202. case AImplIntf.VtblImplIntf.IType of
  1203. etFieldValue, etFieldValueClass,
  1204. etStandard:
  1205. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_pint(AImplIntf.VtblImplIntf.IOffset));
  1206. etStaticMethodResult, etStaticMethodClass:
  1207. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(
  1208. tprocdef(tpropertysym(AImplIntf.ImplementsGetter).propaccesslist[palt_read].procdef).mangledname,
  1209. 0
  1210. ));
  1211. etVirtualMethodResult, etVirtualMethodClass:
  1212. begin
  1213. pd := tprocdef(tpropertysym(AImplIntf.ImplementsGetter).propaccesslist[palt_read].procdef);
  1214. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_pint(tobjectdef(pd.struct).vmtmethodoffset(pd.extnumber)));
  1215. end;
  1216. else
  1217. internalerror(200802162);
  1218. end;
  1219. { IIDStr }
  1220. current_asmdata.getdatalabel(iidlabel);
  1221. rawdata.concat(cai_align.create(const_align(sizeof(pint))));
  1222. rawdata.concat(Tai_label.Create(iidlabel));
  1223. rawdata.concat(Tai_const.Create_8bit(length(AImplIntf.IntfDef.iidstr^)));
  1224. if AImplIntf.IntfDef.objecttype=odt_interfacecom then
  1225. rawdata.concat(Tai_string.Create(upper(AImplIntf.IntfDef.iidstr^)))
  1226. else
  1227. rawdata.concat(Tai_string.Create(AImplIntf.IntfDef.iidstr^));
  1228. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(iidlabel));
  1229. { IType }
  1230. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_pint(aint(AImplIntf.VtblImplIntf.IType)));
  1231. end;
  1232. function TVMTWriter.intf_write_table:TAsmLabel;
  1233. var
  1234. rawdata : TAsmList;
  1235. i : longint;
  1236. ImplIntf : TImplementedInterface;
  1237. intftablelab : tasmlabel;
  1238. begin
  1239. current_asmdata.getdatalabel(intftablelab);
  1240. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  1241. current_asmdata.asmlists[al_globals].concat(Tai_label.Create(intftablelab));
  1242. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_pint(_class.ImplementedInterfaces.count));
  1243. rawdata:=TAsmList.Create;
  1244. { Write vtbls }
  1245. for i:=0 to _class.ImplementedInterfaces.count-1 do
  1246. begin
  1247. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  1248. if ImplIntf.VtblImplIntf=ImplIntf then
  1249. intf_create_vtbl(rawdata,ImplIntf);
  1250. end;
  1251. { Write vtbl references }
  1252. for i:=0 to _class.ImplementedInterfaces.count-1 do
  1253. begin
  1254. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  1255. intf_gen_intf_ref(rawdata,ImplIntf);
  1256. end;
  1257. { Write interface table }
  1258. current_asmdata.asmlists[al_globals].concatlist(rawdata);
  1259. rawdata.free;
  1260. result:=intftablelab;
  1261. end;
  1262. { Write interface identifiers to the data section }
  1263. procedure TVMTWriter.writeinterfaceids;
  1264. var
  1265. i : longint;
  1266. s : string;
  1267. begin
  1268. if assigned(_class.iidguid) then
  1269. begin
  1270. s:=make_mangledname('IID',_class.owner,_class.objname^);
  1271. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1272. new_section(current_asmdata.asmlists[al_globals],sec_rodata,s,const_align(sizeof(pint)));
  1273. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  1274. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_32bit(longint(_class.iidguid^.D1)));
  1275. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_16bit(_class.iidguid^.D2));
  1276. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_16bit(_class.iidguid^.D3));
  1277. for i:=Low(_class.iidguid^.D4) to High(_class.iidguid^.D4) do
  1278. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_8bit(_class.iidguid^.D4[i]));
  1279. end;
  1280. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1281. s:=make_mangledname('IIDSTR',_class.owner,_class.objname^);
  1282. new_section(current_asmdata.asmlists[al_globals],sec_rodata,s,0);
  1283. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  1284. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_8bit(length(_class.iidstr^)));
  1285. current_asmdata.asmlists[al_globals].concat(Tai_string.Create(_class.iidstr^));
  1286. end;
  1287. procedure TVMTWriter.writevirtualmethods(List:TAsmList);
  1288. var
  1289. vmtpd : tprocdef;
  1290. vmtentry : pvmtentry;
  1291. i : longint;
  1292. procname : string;
  1293. {$ifdef vtentry}
  1294. hs : string;
  1295. {$endif vtentry}
  1296. begin
  1297. if not assigned(_class.VMTEntries) then
  1298. exit;
  1299. for i:=0 to _class.VMTEntries.Count-1 do
  1300. begin
  1301. vmtentry:=pvmtentry(_class.vmtentries[i]);
  1302. vmtpd:=vmtentry^.procdef;
  1303. { safety checks }
  1304. if not(po_virtualmethod in vmtpd.procoptions) then
  1305. internalerror(200611082);
  1306. if vmtpd.extnumber<>i then
  1307. internalerror(200611083);
  1308. if (po_abstractmethod in vmtpd.procoptions) then
  1309. procname:='FPC_ABSTRACTERROR'
  1310. else if not wpoinfomanager.optimized_name_for_vmt(_class,vmtpd,procname) then
  1311. procname:=vmtpd.mangledname;
  1312. List.concat(Tai_const.createname(procname,0));
  1313. {$ifdef vtentry}
  1314. hs:='VTENTRY'+'_'+_class.vmt_mangledname+'$$'+tostr(_class.vmtmethodoffset(i) div sizeof(pint));
  1315. current_asmdata.asmlists[al_globals].concat(tai_symbol.CreateName(hs,AT_DATA,0));
  1316. {$endif vtentry}
  1317. end;
  1318. end;
  1319. procedure TVMTWriter.writevmt;
  1320. var
  1321. methodnametable,intmessagetable,
  1322. strmessagetable,classnamelabel,
  1323. fieldtablelabel : tasmlabel;
  1324. hs: string;
  1325. {$ifdef WITHDMT}
  1326. dmtlabel : tasmlabel;
  1327. {$endif WITHDMT}
  1328. interfacetable : tasmlabel;
  1329. begin
  1330. {$ifdef WITHDMT}
  1331. dmtlabel:=gendmt;
  1332. {$endif WITHDMT}
  1333. { write tables for classes, this must be done before the actual
  1334. class is written, because we need the labels defined }
  1335. if is_class(_class) then
  1336. begin
  1337. current_asmdata.getdatalabel(classnamelabel);
  1338. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1339. new_section(current_asmdata.asmlists[al_globals],sec_rodata,classnamelabel.name,const_align(sizeof(pint)));
  1340. { interface table }
  1341. if _class.ImplementedInterfaces.count>0 then
  1342. interfacetable:=intf_write_table;
  1343. methodnametable:=genpublishedmethodstable;
  1344. fieldtablelabel:=generate_field_table;
  1345. { write class name }
  1346. current_asmdata.asmlists[al_globals].concat(Tai_label.Create(classnamelabel));
  1347. hs:=_class.RttiName;
  1348. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_8bit(length(hs)));
  1349. current_asmdata.asmlists[al_globals].concat(Tai_string.Create(hs));
  1350. { generate message and dynamic tables }
  1351. if (oo_has_msgstr in _class.objectoptions) then
  1352. strmessagetable:=genstrmsgtab;
  1353. if (oo_has_msgint in _class.objectoptions) then
  1354. intmessagetable:=genintmsgtab;
  1355. end;
  1356. { write debug info }
  1357. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1358. new_section(current_asmdata.asmlists[al_globals],sec_rodata,_class.vmt_mangledname,const_align(sizeof(pint)));
  1359. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(_class.vmt_mangledname,AT_DATA,0));
  1360. { determine the size with symtable.datasize, because }
  1361. { size gives back 4 for classes }
  1362. current_asmdata.asmlists[al_globals].concat(Tai_const.Create(aitconst_ptr,tObjectSymtable(_class.symtable).datasize));
  1363. current_asmdata.asmlists[al_globals].concat(Tai_const.Create(aitconst_ptr,-int64(tObjectSymtable(_class.symtable).datasize)));
  1364. {$ifdef WITHDMT}
  1365. if _class.classtype=ct_object then
  1366. begin
  1367. if assigned(dmtlabel) then
  1368. current_asmdata.asmlists[al_globals].concat(Tai_const_symbol.Create(dmtlabel)))
  1369. else
  1370. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_ptr(0));
  1371. end;
  1372. {$endif WITHDMT}
  1373. { write pointer to parent VMT, this isn't implemented in TP }
  1374. { but this is not used in FPC ? (PM) }
  1375. { it's not used yet, but the delphi-operators as and is need it (FK) }
  1376. { it is not written for parents that don't have any vmt !! }
  1377. if assigned(_class.childof) and
  1378. (oo_has_vmt in _class.childof.objectoptions) then
  1379. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(_class.childof.vmt_mangledname,0))
  1380. else
  1381. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1382. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  1383. if is_class(_class) then
  1384. begin
  1385. { pointer to class name string }
  1386. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(classnamelabel));
  1387. { pointer to dynamic table or nil }
  1388. if (oo_has_msgint in _class.objectoptions) then
  1389. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(intmessagetable))
  1390. else
  1391. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1392. { pointer to method table or nil }
  1393. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(methodnametable));
  1394. { pointer to field table }
  1395. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(fieldtablelabel));
  1396. { pointer to type info of published section }
  1397. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(RTTIWriter.get_rtti_label(_class,fullrtti)));
  1398. { inittable for con-/destruction }
  1399. if _class.members_need_inittable then
  1400. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(RTTIWriter.get_rtti_label(_class,initrtti)))
  1401. else
  1402. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1403. { auto table }
  1404. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1405. { interface table }
  1406. if _class.ImplementedInterfaces.count>0 then
  1407. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(interfacetable))
  1408. else if _class.implements_any_interfaces then
  1409. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil))
  1410. else
  1411. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(current_asmdata.RefAsmSymbol('FPC_EMPTYINTF')));
  1412. { table for string messages }
  1413. if (oo_has_msgstr in _class.objectoptions) then
  1414. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(strmessagetable))
  1415. else
  1416. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1417. end;
  1418. { write virtual methods }
  1419. writevirtualmethods(current_asmdata.asmlists[al_globals]);
  1420. current_asmdata.asmlists[al_globals].concat(Tai_const.create(aitconst_ptr,0));
  1421. { write the size of the VMT }
  1422. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname(_class.vmt_mangledname));
  1423. {$ifdef vtentry}
  1424. { write vtinherit symbol to notify the linker of the class inheritance tree }
  1425. hs:='VTINHERIT'+'_'+_class.vmt_mangledname+'$$';
  1426. if assigned(_class.childof) then
  1427. hs:=hs+_class.childof.vmt_mangledname
  1428. else
  1429. hs:=hs+_class.vmt_mangledname;
  1430. current_asmdata.asmlists[al_globals].concat(tai_symbol.CreateName(hs,AT_DATA,0));
  1431. {$endif vtentry}
  1432. end;
  1433. end.