nobj.pas 63 KB

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