nobj.pas 63 KB

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